Things that are not random
Installing the mysql gem… again and again

gem install mysql — —with-mysql-config=/usr/local/mysql-5.1.58-osx10.6-x86_64/bin/mysql_config

Hosts of hosts

Tests using

  • RSpec request host is hardcoded in ActionDispatch::TestRequest to ‘test.host’
  • Capybara set their request host from Capybara.default_host = ‘somehost.com’
  • Rack Test set their request host from:
    module Rack
      module Test
        DEFAULT_HOST='somehost.com'
      end
    end

Todo:

What is Capybara.app_host?

Creating cookies to support Cucumber Tests

There are many ways on the web to create cookies for cucumber tests using capybara and/or rack test. This was the only way that worked for me in my circumstances:

In cucumber’s env.rb:

#based on http://groups.google.com/group/phillyrb/browse_thread/thread/254c96bae8a9060b 
class CookieCutterController < ApplicationController
  def new
    cookies[params[:name]] = params[:value]
    info = "Created A Cookie called '#{params[:name]}' with value '#{params[:value]}' for host '#{request.host}'"
    Rails.logger.info(info)
    render :text => info
  end
end

The above can be improved by creating the cookie from all relevant params, rather than just name and value. To actually hit the above, a route needs to be added. This complex version is only required for Rails3 as it does not allow adding extra routes without wiping everything that has gone before:

#based on http://openhood.com/rails/rails%203/2010/07/20/add-routes-at-runtime-rails-3/
begin
  _routes = Rails::Application.routes
  _routes.disable_clear_and_finalize = true
  _routes.clear!
  Rails::Application.routes_reloader.paths.each{ |path| load(path) }
  _routes.draw do
    # here you can add any route you want
    match 'cookie_cutter/:name/:value' => 'cookie_cutter#new', :as => :cookie_cutter
  end
  ActiveSupport.on_load(:action_controller) { _routes.finalize! }
ensure
  _routes.disable_clear_and_finalize = false
end

Installing the pg PostgreSQL gem on Mac OS X

As seen on http://effectif.com/databases/installing-pg-gem-on-mac, cut-and-paste ready for my setup:

PATH=$PATH:/Library/PostgreSQL/9.0/bin gem install pg

Creating an argument matcher for RSpec

How to create a matcher for RSpec is readily found, but to work out how to create an argument matcher you have to look through the RSpec source. And why can’t they be the same, anyway?

An argument matcher is simply a class that implements ==, as in:

class MyMatcher

  def initialize(expected)
    @expected = expected
  end

  def ==(actual)
    some_function_of(@expected, actual)
  end

  def description
    "An optional description"
  end
end
alias method chain versus super in a module

In a module that will be used as a mixin, use alias method chain rather than super - if you use super, you will end up blatting any method with the same name in the class that you are mixed in to.

Jasmine and JSON

After adding the jasmine gem to my Gemfile, specs started to break. Something to do with the to_json methods being interfered with by the json gem that jasmine requires.

I had added jasmine to the :test group - but all problems disappeared when I moved jasmine to its own group. TODO: Find out how these groups are actually used…

Enter the ruby debugger

require ‘ruby-debug’; debugger

http://bashdb.sourceforge.net/ruby-debug.html  to use

Pagination with WillPaginate and includes

When using includes to speed up finds of objects that have lots of associations, will_paginate (or ActiveRecord) ends up creating a very nasty count query. An easy way around this is to say:

base_scope = MyClassWithLotsOfAssociations.some_scope

result = base_scope.with_includes.paginate(params[:page], :total_entries => base_scope.count)

This will do the count without the count-query destroying effects of the includes

Submission urls for rails forms

If you have a form that needs some additional parameters to work out where to submit, eg.

in routes.rb

map.resources :interesting_form_submissions,   :as => 'about_thing/:id/interesting',  :only => [:create, :new]

you can pass these parameters as follows:

  <% form_for @form_submission, :url => {:id => @thing.id, :action => :create} do |f| %>
    blah blah
  <% end %>

Simple

TODO: Work out why it need the :action => :create on the form_for