How to silence UTF-8 warnings on Rails 2.3 with Ruby 1.9

Rails 2.3.16+ on Ruby 1.9 causes warnings like this:

.../gems/activesupport-2.3.17/lib/active_support/core_ext/string/output_safety.rb:22: warning: regexp match /.../n against to UTF-8 string

Many thanks to grosser for supplying a monkey-patch for Rails 2.3 (Commit f93e3f0ec3 fixed it for Rails 3). Just put it into config/initializers/ to make those warnings go away.

Since we're using RSpec on mos…

Capturing signatures on a touch device

If you need to capture signatures on an IPad or similar device, you can use Thomas J Bradley's excellent Signature Pad plugin for jQuery.

To implement, just follow the steps on the Github page.

The form

If you have a model Signature with name: string, signature: text, you can use it with regular rails form like this:

- form_for @signature, :html => { :class => 'signature_form' } do |form|
      = form...

Fix warning "already initialized constant Mocha" with Rails 3.2

You either have an old version of Mocha and an edge version of Rails 3.2, or you have a new version of Mocha and an old version of Rails. The best solution is to update Mocha to the latest version and switch to Rails edge.

If you are using shoulda-matchers or another gem that locks Mocha to an old version, you are out of luck.
More info with many other workarounds that you do not want to use can be found here. A hack to work around this case is to add the following file to lib/mocha/setup.rb:…

External content

lang/unicode_utils · GitHub

UnicodeUtils implements Unicode algorithms for case conversion, normalization, text segmentation and more in pure Ruby code.

If you don't need the ton of features that UnicodeUtils offers, try stringex.

Edge Rider: Power tools for ActiveRecord scopes

In our continued quest to extract proven code snippets from makandropedia into tested and upgradable gems, we have released Edge Rider.

Edge Rider was created with two intents:

  1. Provides a number of utility methods to facilitate hardcore work with scopes.
  2. Provide a stable API for working with scopes across multiple versions of Rails, since Rails has a tradition of breaking details of its scope API every other release.

The gem bundles multiple patches and initializers we've been using for hard…

CSS: Set content from other attributes

You can use the content CSS attribute to set an element's content – which is especially useful for the :before and :after pseudo elements:

a:before {
  content: 'Click me: ';

The above example would prepend "Click me:" to any link on the page.

Note that you can also refer the contents of other attributes of the element. So, if your links have a helpful title set, you could do this:

a:before {
  content: attr(title) ": ";

There also is a jsFiddle for the examp…

External content

Firefox introduces PDF viewer

Prepare to explain customers what changed if your application renders PDF documents to users.

How to fix: Microphone recording levels are too quiet (or get lowered automatically)

If others on a call (Skype, SIP, …) can not hear you loud enough, your volume levels are probably too low. Also, Skype may be changing your mixer levels.

Set a proper recording volume

  1. Open your mixer software (run pavucontrol).
  2. Switch to input devices.
  3. If you have more than one recording device, find the correct one.
  4. Make a test call to a colleague that can tell you if it's too loud or too quiet.
  5. Drag the volume slider for your input device to an adequate level – for me, 75% (-7.46dB) work fine. 100% is usually way to…
External content

Using CSS counters - CSS | MDN

Counters are an awesome CSS feature you didn't know about. It is supported in all browsers and IE8+.

CSS counters are an implementation of Automatic counters and numbering in CSS 2.1. The value of a counter is manipulated through the use of counter-reset and counter-increment and is displayed on a page using the counter() or counters() function of the content property.

How to fix: RVM does not offer recent Ruby versions

RVM needs to be updated regularly to know of Ruby versions released since installation (or last update).

So if you say rvm install 1.9.3, but get an old version (basically anything below 1.9.3-p385 when writing this card), your RVM is outdated. \
Fix that by saying:

rvm get stable

After that, rvm install 1.9.3 should install the latest 1.9.3 version.

External content

CSS 3D Clouds

This is awesome.

Only ran smoothly in Chrome. Is a slideshow in Firefox.

MySQL: For each group, retrieve a comma-separated list of values in a given column

The technique described in this card has an important caveat: The result of GROUP_CONCAT is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. This will cause horrible, data-destroying bugs in production. For this reason you should probably not use GROUP_CONCAT ever. At least you must set the value of group_concat_max_len to an insanely high value on every database server your application runs on.


A regular expression that will never match

So you have a method returning a regular expression but one case that should not yield a matching Regexp object but still keep the API stable? Just return one that never matches:


Git: How to check out branches that exist on multiple remotes

So you're using multiple remotes that offer the same branch?

$ git branch -a | grep my-branch

And when trying to check out that remote branch, it fails for you with an error like this?

$ git checkout my-branch
error: pathspec 'my-branch' did not match any file(s) known to git.

Git usually guesses the remote branch to check out, but when using more than one remote, it seems like it no longer can do that.
Even if the branch is the same on both remotes, you nee…

External content

rsl/stringex · GitHub

Stringex is a gem that offers some extensions to Ruby's String class. Ruby 1.9 compatible, and knows its way around unicode and fancy characters.

Examples for stringex's String#to_url method:

# A simple prelude
"simple English".to_url => "simple-english"
"it's nothing at all".to_url => "its-nothing-at-all"
"rock & roll".to_url => "rock-and-roll"

# Let's show off
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power"
"10% off if you act now".to_url => "10-percent-off-if-you-act-now"

# You do...

How to fix: "Error Bundler::HTTPError during request to dependency API"

If bundle install shows the following message for you …

Error Bundler::HTTPError during request to dependency API

… upgrade to Bundler ≥ 1.2.4:

gem install bundler

Apparently, it just hides the message.

Prevent double clicks on link_to_remote (simple case)

This works well in the simplified case, when your link disappears after it was clicked.

Let link_to_remote behave as „disabled“ after the first click. Use the :before hook to replace the orignal link with a link that does nothing but looks like the original link:

label = "do_something"
dummy_link = link_to(label)
other_attributes_hash = { :url => …, :method => …, … }

disable_link_option = { :before => "$('your_link_selector').html('#{escape_javascript(dummy_link)}'" } # jquery

= link_to_remote(label, other_att…

External content

5 reasons why I won't steal your idea

Since I'm a software architect and a web developer, I get often approached by people with their new ideas. In most cases, for some quality feedback, and on lucky days, for a rough quote about the costs of such a project. These people are usually very secretive about what they have, making me explain to them that it's far from my interest to steal that idea. (…) Focus on your product, and don't worry about me stealing your idea. I won't. I have at least five reasons not to.

3345 cards