Five Rails tips

Apr 27, 2008 · see comments

Ryan Bates, of the excellent Railscasts, is running a contest to generate a bevy of Rails tips on the web. (And look, Harvest is part of the grand prize package. W00T!)

Here’s my contribution:

Handy test helper to check your layout:

Do you have any layout variability served up in your controllers? Maybe you’re doing some iUI work and want to test that no layout is being served up at all?

def assert_layout(layout)
  assert_equal layout, @response.layout


assert_layout 'layouts/application'
assert_layout nil

Handy test helper to check your format:

In a similar vein, do you have format variability served up in your controllers? Say you are presenting a mobile site that adjusts the format according to subdomain. Well, you may want to test that as well.

def assert_format(format)
  assert_equal format.to_sym, @request.format.to_sym


assert_format :iphone

The flexibility of assert_select:

Did you know you can use regular expressions with assert_select? It’s pretty fun. Here’s a selector to verify a page does not include any table rows with ids of the form “day_entry_#”.

assert_select "tr[id*=?]", /day_entry_\d+/, :count => 0

Massage params when tweaking them for specific iPhone keyboards:

When developing a web app for the iPhone, the only apparent way to specify an entry field use the numeric keyboard is to include the string “zip” in the input name. To get the keypad numeric keyboard, include the string “phone”. What I like to do is append “_zip_keys” as necessary to an entry field’s proper name.

<%= form.text_field(:hours, :name => 'day_entry[hours_zip_keys]') %>

This obviously causes problems when you are depending on Rails magic to build objects from passed parameters. In application.rb I created some massaging functions that rip through the params hash (and its subhashes) renaming the keys that are suffixed “_zip_keys” or “_phone_keys”.

# Some params on iPhone pages may be trailed with _zip_keys or _phone_keys to bring up proper keyboards.
# This method removes this sugar from the keys.
def massage_iphone_params
  return unless iphone_request?

IPHONE_KEYS_REGEX = /(_zip_keys|_phone_keys)$/

def massage_iphone_keys(_hash)
  temp_value = ''
  _hash.each do |key, value|
    massage_iphone_keys(value) and next if value.is_a?(Hash)
    if IPHONE_KEYS_REGEX === key.to_s
      temp_value = _hash.delete(key)
      _hash[key.to_s.sub(IPHONE_KEYS_REGEX, '')] = temp_value

Making use of this key translator is as simple as a before_filter:

before_filter :massage_iphone_params, :only => [:create, :update]

Git meme

Here are my git aliases, which started with those presented in the Peepcode screencast.

# Git
alias gb="git branch"
alias gba="git branch -a"
alias gco="git checkout"    # add branch name to checkout
alias gm="git merge"        # add branch name to merge into current
alias gc="git commit -v"
alias gd="git diff | mate"
alias gl="git pull"
alias glb="git pull origin" # add branch name to the end
alias gp="git push"
alias gpb="git push origin" # add branch name to the end
alias gst="git status"
gcob(){ git branch --track $1 origin/$1; gco $1; }  # add remote root branch name to the end

(Most are self-explanatory. That last one basically creates a local branch named identically to its tracked source, and checks it out.)

Apr 27, 2008 · Subscribe · Archive · Projects · Twitter · GitHub · Flickr