Mailcatcher: An alternative to inaction_mailer

Looks simpler than inaction_mailer:

gem install mailcatcher
mailcatcher

Setup Rails to send mails to 127.0.0.1:1025. Usually you want the following config in config/environments/development.rb and maybe in test.rb or cucumber.rb.

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address => 'localhost',
  :port => 1025
}

Now you can see sent mails in your browser when opening http://127.0.0.1:1080

Note: In order to s...

Properly adding fields with default values to a model

When adding a new field to your model's database table, don't set any defaults in the database.

It makes people wonder why they get such values when reading attributes.\
Why? Because nobody looks at the database layout since such things are part of your application's logic -- and thus they belong into the corresponding model.

How to

Do it like this:

  • In your migration, after adding the field, update all fields to your desired default:

    update "UPDATE users SET locked = #{quoted_false};"
    
  • In your model, set a defau...

Install the Oniguruma gem

Oniguruma is an advanced regular expression engine for Ruby.

Install Oniguruma with binary dependencies like this:

sudo apt-get install libonig2 libonig-dev
sudo gem install oniguruma

On the Mac do:

brew install oniguruma
sudo gem install oniguruma

Change / Update SSL certificate for Amazon Elastic Load Balancer

There is a new card about how to do this with the new AWS Command Line Interface


At first you need the IAM Cli Tools.
-------------------------------------------------------------------------------------------------------------...

How to fix: Gems are unavailable although they are installed

  • If Rails or Rake are complaining about a missing gem that is listed in your Gemfile.lock and the listed version is properly installed, something is seriously wrong and needs to be fixed.
  • If you accidently executed bundle install some_gem although you wanted bundle update some_gem

What is wrong

Let's say your Gemfile asks for some-gem which you can see when running gem list but bundle show some-gem just gives you an error:

Could not find gem 'some-gem', in any of the sources

Another indicator: Doing a `...

Selenium WebDriver 2.5.0, 2.6.0 fails when selecting options from select boxes

We are consistently having trouble with selenium-webdriver > 2.5.0 where whenever we try to select an option from a <select> Capybara complains:

No such option 'Foo' in this select box. Available options: 'Foo', 'Bar', 'Baz' (Capybara::OptionNotFound)

This seems to happen with both old and new versions of Firefox. Our workaround so far is to freeze the gem at version 0.2.2.

Auto-generate state_machine graphs as PNG images

The state_machine gem comes with a rake task that lets you generate PNG graphs from any model using state_machine.

Install the required dependencies like this:

sudo apt-get install graphviz
sudo gem install ruby-graphviz

You can now generate a graph like this:

rake state_machine:draw CLASS=ModelUsingStateMachine

Replace ModelUsingStateMachine with the name of your model class.


If it the raketask does not exist for you, add to Rakefile (in your pr...

How to install a frozen version of Firefox for your Selenium tests

Whenever Firefox updates, all your Cucumber features that use Selenium break. This is annoying.

In order to remedy this, version 0.5.0 of our geordi gem comes with a script that helps you create an unchanging version of Firefox for your Selenium tests. In particular, this new copy of Firefox will have the following properties:

  • It won't update itself with a newer version
  • It can co-exist with your regular Firefox installation (which you can update at will)
  • It will use a profile separate from the one...

Fix: "undefined method `bytesize' for #<Array>"

I believe that when WEBrick has trouble bringing up your Rails application, the WEBrick component that is supposed to print you a pretty error message has a bug and sometimes fails with this message:

"undefined method `bytesize' for #<Array>"

Starting the application in Passenger gave me a stacktrace in log/development.log that pointed to the actual problem.

Possible causes discovered by looking at the logs
-----------------------------------------------------...

Fix Rubygems warning: Gem.source_index is deprecated, use Specification

After updating Rubygems you see a wall of deprecation warnings like this:

NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from /usr/local/lib/site_ruby/1.8/rubygems/source_index.rb:197.

NOTE: Gem::SourceIndex#add_specs is deprecated with no replacement. It will be removed on or after 2011-11-01.
Gem::SourceIndex#spec_dirs= called from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.14/lib/bundler/rubygems_integration.rb:175

...

Ruby GetText will eval scripts containing ActiveRecord classes

When the Ruby parser module of Ruby-GetText comes across a file in one of its search directories (e.g. lib/scripts/) and finds out that you are defining ActiveRecord classes inside it, it evaluates the whole file. Here is how to avoid that.

What's happening?

Let's say you have the following script which is only run once, manually, via script/runner:

# lib/scripts/doomsday.rb
class User < ActiveRecord::Base; end
User.destroy_all

In that case we ...

Marry Date::Performance and Spreadsheet gems

Date::Performance is a gem that replaces various method in Ruby's Date class with fast C implementations. Unfortunately it doesn't fully implement an internal method (Date.ajd_to_jd) which makes your code blow up when you use it together with the Spreadsheet gem.

A solution is to restore the Ruby implementation of this particular method. To do this, copy the attached file to lib/fix_date_performance.rb to config/initializers.

Resque + God + Capistrano

Attached is a working config to deploy an application with Capistrano that needs to monitor Resque workers with God.

God will run as the deployment user, no need to register it as a system service.

Put this into your config/deploy.rb:

namespace :god do
  def god_is_running
    !capture("#{god_command} status >/dev/null 2>/dev/null || echo 'not running'").start_with?('not running')
  end

  def god_command

...

Rails I18n fallback locales

When you need to create a locale for a language variant (like Austrian for German), you probably don't want to duplicate your entire de.yml file only to change a few minor exceptions for our Austrian friends.

Luckily, the I18n gem used by Rails has a fallback feature where you can make one locale file fall back to another if no translation is available.

In the example above you would have a config/locales/de_DE.yml:

de_DE:
  # hundreds of translations here

... and another...

How to diff two strings in Ruby

When you need to use diff in either some Ruby code or your Rails app, use the differ gem.

puts Differ.diff "foo", "boo"
# => {"boo" >> "foo"}

Usage

There are several variants available, all using the base method diff(to, from, separator = "\n").
You have diff_by_line, diff_by_word, diff_by_char and may of course use your own separator:

puts Differ.diff 'Hauptsatz, und mein Nebensatz.', 'Hauptsatz, und dein Nebensatz.', ','
# => Hauptsatz,{" und dein Nebensatz." >> " un...

Zip files with Ruby

When you need to zip up files in Ruby, use zipruby.

sudo gem install zipruby

You can add existing files, add files from strings and even add directories.
Example usage:

require 'zipruby'
cars = %w[audi bmw mercedes]

zipfile = Tempfile.new('my.zip', 'tmp')
Zip::Archive.open(zipfile.path, Zip::CREATE) do |zip|
  zip.add_file '/tmp/me.txt'
  zip.add_dir 'cars' 

  cars.each do |car|
    zip.add_buffer "cars/#{car}.txt", "This #{car} is mine!" 
  end
end

Credits go to winebarrel for the Ruby bin...

Hide your Selenium browser window with a VNC server

This is now part of geordi. Please don't follow the instructions below, if you use geordi.

Inspired by the recent headless Selenium note, I found yet another solution for the problem to hide your selenium tests away.

This has the advantages
^

  • not to require a gem (so you do not force this on others)
  • to allow you to take a look at the running webdriver if necessary

Simply make a script th...

How to hide your selenium browser window with "headless"

Note: While the solution in this card should still work, we prefer another solution now: Hide your Selenium browser window with a VNC server.


If you would like to hide the annoying selenium browser window that always gets the focus and prevents you from working, you can use the headless gem. This note provides some instructions how you can get it to work with your cucumber accepta...

A few hints when upgrading to Ruby 1.9

Note: If you are currently working with Ruby 1.8.7 or 1.9.3, we recommend to upgrade to Ruby 2.1 first. From our experience this upgrade is much smoother than the jump from 2.1 and 2.2, while still giving your the huge performance gains of Ruby 2. Also, if you're on Ruby 1.8.7, we recommend to skip a troublesome upgrade to 1.9.3 and go straight to 2.1.


When trying to make a Rails app run on Ruby 1.9, you're likely to encounter several issues. Here are a few solutions (obviously not exhaustive):

When running `bundle ...

Shell script to generate a Git commit with Pivotal Tracker story ID and title

We usually generate our commit messages from Pivotal Tracker IDs and titles, like
[#15775609] Index view for conflicts

The geordi command commit automates this. (See: Pretty Commit messages via geordi).

Just run geordi commit and it will connect to PT and let you select from a list of all started and finishes stories. Then it runs git commit with the generated message (i.e. all staged changes will be commited).

When running for the first time, Geordi will request your PT...

Fix "private method `select' called for Capybara::Node::Element

API breakage ahoy. You need to either upgrade your Capybara or downgrade your selenium-webdriver gem.

Alternatively, this could solve your trouble.

Check gem dependencies before installation

With gem dependency it is possible to check the dependencies for your gem before you install it.

Here is an example output for Nokogiri:

Gem nokogiri-1.4.4
  hoe (>= 2.6.2, development)
  minitest (>= 1.6.0, development)
  racc (>= 0, development)
  rake-compiler (>= 0, development)
  rexical (>= 0, development)
  rubyforge (>= 2.0.4, development)

Use look-behind assertions in regular expressions with Ruby 1.8

Regular expressions can have something called "zero-width look-behind assertions". This means that you want a pattern to be preceded by another pattern, but not include the preceding pattern in your match or search cursor. E.g. (?<=x)y matches y in xyz but not in syz. There are also negative look-behind assertions, e.g. (?<!x)y matches y in syz but not in xyz.

Unfortunately look-behind assertions are only available in Ruby 1.9. With Ruby 1.8 you need to use an alternative regular expression library called [Oniguruma](http://...

Installing Rails on a fresh system

  1. Install Ruby from the Ubuntu repository: sudo apt-get install ruby ruby-dev \
    ruby is the meta package. If you want to explicitly install 1.8 or 1.9, install ruby1.8 or ruby1.9 instead (the same applies for ruby-dev).
  2. Do not install RubyGems from the repository but install the version from the webpage instead.
  3. Get Bundler: sudo gem install bundler

Rails and other gems for a project should now be installed via bundle install from the...