Test that a CSS selector is present with Cucumber

Cucumber step for checking whether a CSS selector is present or absent on a page with Capybara or Webrat.

Bash Cheat Sheet (standard Emacs mode)

Handy Bash key bindings and history shortcuts reduce typing and speed up command-line editing, repetition, and reuse of previous arguments.

Rails 3 Remote Links and Forms: A Definitive Guide

Thanks to habits engrained by Rails 2’s link_to_remote and remote_form_for, we expect that Rails 3 would also handle the AJAX response for our remote links and forms. But it doesn’t; it leaves that for you.

Share your Internet connection under Ubuntu

Turn an Ubuntu PC into a simple internet gateway for another computer using a second network interface and connection sharing.

Terminus: a client-side Capybara driver

Terminus is a Capybara driver where most of the driver functions are implemented in client-side JavaScript. It lets you script any browser on any machine using the Capybara API, without any browser plugins or extensions.

Standalone Cucumber Test Suite

Sometimes you inherit a non Rails or non Rack based web app such as PHP, Perl, Java / JEE, etc. I like using cucumber for functional testing so I put together this project structure to use as a starting point for testing non Ruby web based applications.

Show a MySQL table's charset, collation and engine

SHOW CREATE TABLE reveals a table’s storage engine plus character set and collation, useful when comparing schemas or diagnosing encoding issues.

Request a non-HTML format in controller specs

Controller specs can exercise non-HTML responses such as PDF, Excel, XML or JSON by setting :format or the HTTP_ACCEPT header. The format value must be a String, not a Symbol.

Get the current layout's name in a view or partial

Retrieve the active view layout name, including its path, from response.layout; File.basename returns just the layout name when the full path is unnecessary.

Undocumented :inverse_of option for ActiveRecord associations

You can now add an :inverse_of option to has_one, has_many and belongs_to associations.... Without :inverse_of m and f.man would be different instances of the same object (f.man being pulled from the database again). With these new :inverse_of options m and f.man are the same in memory instance.

Enable or disable Gnome desktop icons

Desktop icons can distract during presentations; GNOME/Nautilus can hide or show them through the desktop preferences setting.

Fix LoadError with Rails 3 applications on Passenger

After switching to Rails 3 you may get a LoadError with the following message when trying to use your application via passenger:
no such file to load -- dispatcher

Your Passenger version is most likely out of date.

Update the gem, then install the apache module again:
sudo gem install passenger
sudo passenger-install-apache2-module

Follow any instructions. Update your /etc/apache2/httpd.conf with the lines given at the end of the installation process to use the version you just installed.

Passenger ignores RailsEnv directive for Rails 3 applications

You might find that your Passenger ignores all RailsSomething directives in the vhost for your new Rails 3 application. The culprit is a file config.ru which makes Passenger consider your application a Rack (non-Rails) application.

To fix this you can either use RackEnv in lieu of RailsEnv (it works fine) or delete the config.ru. Unless you have a good reason to do so, go with RackEnv.

Migrating to RSpec 2 from RSpec 1

Rails 3 requires rspec and rspec-rails 2, with renamed namespaces, changed requires, and macro extensions that may break during migration.

Take care when merging with params

Be careful when using params.merge as params is a HashWithIndifferentAccess.

Why?

Usually this should not be an issue but it turns crazy if you try to include associated models deeper than 1 level:
options = params.merge(:include => { :user => :avatar })
Post.paginate options

When inspecting the merged params you will get something like this:
{ :include=> { "user" => :avatar }, :page => 23 }

Here the :user symbol in the hash of inclusions turned into a "user"...

Use form_for without the enclosing form tag

Need form builder helpers without emitting a surrounding <form> tag, for partial updates such as XHR-driven field changes. Rails fields_for yields the same builder without hidden inputs or authenticity token.

Shell script to quickly switch Apache sites

Quickly make one Apache virtual host available at localhost while switching projects. The script disables other sites, can re-enable the default site, and lists available sites without arguments.

On memoizing methods that return a scope

Memoizing a scope-returning method can turn the scope into a loaded array, breaking lazy query chaining; a safer memoizer or manual instance-variable cache avoids it.

Generate a path or URL string from an array of route components

polymorphic_path and polymorphic_url build route strings from arrays of model objects or route components, including :new and :edit prefixes. Nested Active Record namespacing can fail to resolve the correct route.

Adding a Timepicker to jQuery UI Datepicker

The timepicker addon adds a timepicker to jQuery UI Datepicker, thus the datepicker (jQueryUI) is required for using any of these. In addition all datepicker options are still available through the timepicker addon.

Falsehoods Programmers Believe About Names

I’m going to list assumptions your systems probably make about names. All of these assumptions are wrong. Try to make less of them next time you write a system which touches names.

Getting your e-mails back after upgrading Thunderbird to version 3

If you previously used version 2.x of Thunderbird and upgraded to 3.x (for example through an Ubuntu release upgrade) you might notice that Thunderbird will not show any of your old e-mails or settings.

This results from a different directory being used for storing profiles and configuration.

You can replace the blank profile with your old one like this:
cd ~
mv .thunderbird .thunderbird-invalid
cp -R .mozilla-thunderbird .thunderbird

Upon its next start, Thunderbird brings up the migration wizard introducing you to a few vers...

Working around the ancestry gem's way of object destruction

The ancestry gem allows you to easily use tree structures in your Rails application.

There is one somewhat unobvious pitfall to it: its way of applying the orphan_strategy which defines how it handles children of an object going to be destroyed.

What's this all about?

In many cases you might want to disallow destruction if there are any child nodes present. The restrict strategy does the trick but raises an exception when destroy is called:
has_ancestry :orphan_st...