<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>The Chris O Show - Home</title>
  <id>tag:www.thechrisoshow.com,2010:mephisto/</id>
  <generator version="0.7.3" uri="http://mephistoblog.com">Mephisto Noh-Varr</generator>
  <link href="http://www.thechrisoshow.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.thechrisoshow.com/" rel="alternate" type="text/html"/>
  <updated>2010-04-27T08:29:02Z</updated>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2010-04-27:15553</id>
    <published>2010-04-27T08:28:00Z</published>
    <updated>2010-04-27T08:29:02Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2010/4/27/get-real-with-your-specs-punk" rel="alternate" type="text/html"/>
    <title>Get real with your specs, punk</title>
<content type="html">
            In my rspec spec definitions I was finding myself constantly repeating the word &#8216;should&#8217;.
Like:
&lt;pre class='ruby'&gt;
it &quot;should add two numbers together&quot; 
it &quot;should taste like bananas&quot; 
&lt;/pre&gt;

And it&#8217;s not just me that does this, the standard rspec rails generators have &#8220;should&#8221; in them too:
&lt;pre class='ruby'&gt;
it &quot;should create a new instance given valid attributes&quot; do
  Banana.create!(@valid_attributes)
end
&lt;/pre&gt;

This irritated me, why can&#8217;t rspec be like shoulda and include the word &#8220;should&#8221; in the actual spec definition like this:
&lt;pre class='ruby'&gt;
should &quot;taste like bananas&quot; 
&lt;/pre&gt;

	&lt;p&gt;I asked my Twitter followers what I should do, and &lt;a href='http://twitter.com/capitalist'&gt;Joe Martinez&lt;/a&gt; responded:
&lt;a href='http://twitter.com/capitalist/status/9573493146'&gt;&lt;img src='http://thechrisoshow.com/assets/2010/4/27/clint1.png' alt='' /&gt;&lt;/a&gt; 
&lt;a href='http://twitter.com/capitalist/status/9573975146'&gt;&lt;img src='http://thechrisoshow.com/assets/2010/4/27/clint2.png' alt='' /&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Should is just noise!  Eureka!&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://www.twitter.com/l4rk'&gt;Jon Larkowski&lt;/a&gt; from Hashrocket gave a talk at The Scottish Ruby Conference about some of the more idiosynchratic aspects of RSpec, and sure enough in his examples there weren&#8217;t any sign of &#8216;should&#8217; in the examples.  (Read it &lt;a href='http://pure-rspec-scotruby.heroku.com/'&gt;here&lt;/a&gt;)&lt;/p&gt;


	&lt;p&gt;So, now my specs look like this:&lt;/p&gt;


&lt;pre class='ruby'&gt;
it &quot;smells like bananas&quot; 
it &quot;adds two numbers together&quot; 
&lt;/pre&gt;

	&lt;p&gt;This just feels better &#8211; more decisive somehow.  
As my colleague &lt;a href='http://www.twitter.com/dies_el'&gt;Andrew Donaldson&lt;/a&gt; said:&lt;/p&gt;


	&lt;p&gt;&#8220;Gives a rough manly edge to your specs, Clint Eastwood would never say &#8216;should&#8217;&#8221;.&lt;/p&gt;


	&lt;p&gt;So, in order to remove the unnecessary shoulds from my specs I came up with this RSpec formatter:&lt;/p&gt;


&amp;lt;script src='http://gist.github.com/313426.js'&gt;&amp;lt;/script&gt;

	&lt;p&gt;This produces a list of your specs that include the word &#8216;should&#8217;.&lt;/p&gt;


	&lt;p&gt;So be like Clint, and be decisive with your specs, &lt;em&gt;&lt;strong&gt;Punk&lt;/strong&gt;&lt;/em&gt;
&lt;img src='http://thechrisoshow.com/assets/2010/4/27/clint-eastwood-01.jpg' alt='' /&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2010-04-15:15552</id>
    <published>2010-04-15T10:39:00Z</published>
    <updated>2010-04-15T10:40:48Z</updated>
    <link href="http://www.thechrisoshow.com/2010/4/15/an-example-of-the-art-of-instant-feedback" rel="alternate" type="text/html"/>
    <title>An example of the art of instant feedback</title>
<content type="html">
            &lt;p&gt;This morning on Twitter:&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://twitter.com/jasoncale/status/12210991361'&gt;&lt;img src='http://www.thechrisoshow.com/assets/2010/4/15/Twitter__freerange1.png' alt='' /&gt;&lt;/a&gt;
&lt;a href='http://twitter.com/thechrisoshow/status/12213379505'&gt;&lt;img src='http://www.thechrisoshow.com/assets/2010/4/15/Twitter__freerange2.png' alt='' /&gt;&lt;/a&gt;
&lt;a href='http://twitter.com/jasoncale/status/12213529700'&gt;&lt;img src='http://www.thechrisoshow.com/assets/2010/4/15/Twitter__freerange3.png' alt='' /&gt;&lt;/a&gt;
&lt;a href='http://twitter.com/lazyatom/status/12215084996'&gt;&lt;img src='http://www.thechrisoshow.com/assets/2010/4/15/Twitter__freerange4.png' alt='' /&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;I love stuff like this.  Kudos to the &lt;a href='http://gofreerange.com'&gt;freerange&lt;/a&gt; team&lt;/p&gt;


	&lt;p&gt;&lt;img src='http://www.thechrisoshow.com/assets/2008/10/2/rats_off_to_ya.jpg' alt='' /&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2010-01-26:15551</id>
    <published>2010-01-26T11:25:00Z</published>
    <updated>2010-02-15T23:07:08Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2010/1/26/gravatar-default-images-a-better-way" rel="alternate" type="text/html"/>
    <title>Gravatar default images - a better way</title>
<content type="html">
            &lt;p&gt;Hey, so at &lt;a href='http://www.harmonypark.net'&gt;Harmonypark&lt;/a&gt; we decided to put gravatars into one of our applications (which is so easy it&#8217;s not funny) &#8211; and when the time came to select the default images for the gravatar I piped up: &#8220;I know how to fix this!&#8221; and pointed to a blog post I wrote about it long ago: &lt;a href='http://thechrisoshow.com/2008/10/27/adding-gravatars-to-your-rails-apps'&gt;(You can read it here.)&lt;/a&gt;&lt;/p&gt;


My colleague &lt;a href='http://www.twitter.com/ebonical'&gt;Ebony&lt;/a&gt; came up with a great solution:
&amp;lt;quote&gt;
&#8220;Why not use another gravatar as the default image.&#8221; 
&amp;lt;/quote&gt;

	&lt;p&gt;Which is an utterly brilliant idea &#8211; that way the default image gravatar will scale correctly, and we wouldn&#8217;t have to mess around with having different image sizes for the different scaled default images. Here&#8217;s how we did it:&lt;/p&gt;


	&lt;p&gt;1) Go to &lt;a href='http://gravatar.com'&gt;gravatar.com&lt;/a&gt; and add a default gravatar for some email address that you won&#8217;t use.&lt;/p&gt;


2) Install the gravatar plugin
&lt;pre class='ruby'&gt;
script/install http://github.com/woods/gravatar-plugin/
&lt;/pre&gt;

	&lt;p&gt;3) Extend the gravatar plugin to allow for a default email address by putting this code in lib/gravatar_extension.rb (remember to include this file in a initializer).&lt;/p&gt;


&lt;pre class='ruby'&gt;
module GravatarHelper  
  module PublicMethods  

    def gravatar_for_with_default(object, options={})
      options.merge!(:default =&amp;gt; gravatar_url(&quot;your-default-email@example.com&quot;, options))
      gravatar_for_without_default(object, options)  
    end  
    alias_method_chain :gravatar_for, :default  
  end  
end
&lt;/pre&gt;

	&lt;p&gt;4) Now, everytime a user is missing a gravatar, the default image will be your default gravatar, this will handle all the awesome scaling stuff that gravatar does too&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2009-04-20:188</id>
    <published>2009-04-20T11:01:00Z</published>
    <updated>2010-01-28T21:24:04Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2009/4/20/see-what-columns-an-activerecord-model-has-with-what-column" rel="alternate" type="text/html"/>
    <title>What columns does an ActiveRecord model have?</title>
<content type="html">
            &lt;p&gt;Something that bugs me about using ActiveRecord models is that I have to look in schema.rb file to remember what columns a model has.&lt;/p&gt;


	&lt;p&gt;Enter&#8230;&lt;strong&gt;What Column!&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;This is a little plugin I bunged together on the (long) train journey home from Scotland on Rails.&lt;/p&gt;


	&lt;p&gt;What it does is put a comment block in all of your ActiveRecord models detailing what columns this model has.  The beauty is that it changes every time you run &lt;code&gt;rake db:migrate&lt;/code&gt; in development mode &#8211; so once it&#8217;s installed it will always be up to date.  Your models will now look like this:&lt;/p&gt;


&lt;pre class='ruby'&gt;
class User &amp;lt; ActiveRecord::Base

  # === List of columns ===
  #   id         : integer 
  #   name       : string 
  #   created_at : datetime 
  #   updated_at : datetime 
  # =======================

end
&lt;/pre&gt;

	&lt;p&gt;Source code here:&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://github.com/thechrisoshow/what_column/'&gt;http://github.com/thechrisoshow/what_column/&lt;/a&gt;&lt;/p&gt;


	&lt;h3&gt;Installation&lt;/h3&gt;


	&lt;p&gt;Install it like this:&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;script/plugin install git://github.com/thechrisoshow/what_column.git&lt;/code&gt;&lt;/p&gt;


	&lt;p&gt;After you&#8217;ve installed it run this rake task to create the initial comment blocks.&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;rake what_column:add&lt;/code&gt;&lt;/p&gt;


	&lt;p&gt;Every time you run &lt;code&gt;rake db:migrate&lt;/code&gt; from then on the comment blocks will be updated.&lt;/p&gt;


	&lt;h3&gt;Uninstallation&lt;/h3&gt;


	&lt;p&gt;If you get sick of What Column, and you want to uninstall it do this:&lt;/p&gt;


	&lt;p&gt;Run &lt;code&gt;rake what:column:remove&lt;/code&gt;&lt;/p&gt;


	&lt;p&gt;Delete the plugin from the &lt;code&gt;vendor/plugins/what_column&lt;/code&gt; directory&lt;/p&gt;


	&lt;h3&gt;Warning&lt;/h3&gt;


	&lt;p&gt;This file writes stuff into your model files, and it may destroy stuff.  So make sure you use a source control system &#8211; you know, just in case.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2009-02-22:183</id>
    <published>2009-02-22T09:18:00Z</published>
    <updated>2009-03-16T13:56:26Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2009/2/22/refactor-away-your-comments" rel="alternate" type="text/html"/>
    <title>Refactor away your comments</title>
<content type="html">
            &lt;p&gt;Last week I had the good fortune of having my colleague &lt;a href='http://twitter.com/abdels'&gt;Abdel Saleh&lt;/a&gt; go through some rspec specs I had written.&lt;/p&gt;


&#8220;Why do you need this comment?&#8221; he asked for a bit of code like this:
&lt;pre class='ruby'&gt;
before(:all) do 
  # Ensure doesn't actually save to Mogile FS
  MogileFS::MogileFS.any_instance.stubs(:store_content)
end
&lt;/pre&gt;
..and he promptly deleted it..

	&lt;p&gt;&#8220;No wait!&#8221; I said defensively, 
&#8220;That&#8217;s there to prevent the tests from actually hitting mogilefs.  The comment&#8217;s there because the line of code doesn&#8217;t describe the intent&#8221;&lt;/p&gt;


	&lt;p&gt;&#8220;Well, that&#8217;s easily fixed&#8221; says Abdel,
&#8220;Rather than using a comment to describe the intent, put it in &lt;strong&gt;a method with a name that describes the intent&lt;/strong&gt;.&#8221;&lt;/p&gt;


&lt;pre class='ruby'&gt;
before(:all) do 
  ensure_doesnt_save_to_mogile_fs
end

def ensure_doesnt_save_to_mogile_fs
  MogileFS::MogileFS.any_instance.stubs(:store_content)
end
&lt;/pre&gt;

	&lt;p&gt;Why is this better!??&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Comments aren&#8217;t maintainable&lt;/strong&gt;.  They don&#8217;t grow and change with the codebase.  They lay still and live on like strange messages from ancient civilisations.&lt;/p&gt;


	&lt;p&gt;In the preceding example you no longer &lt;span class='caps'&gt;NEED&lt;/span&gt; a comment to describe the intent of your code, your code does it for you!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2009-02-16:184</id>
    <published>2009-02-16T21:07:00Z</published>
    <updated>2009-06-18T11:51:05Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2009/2/16/using-guard-clauses-in-your-ruby-code" rel="alternate" type="text/html"/>
    <title>Prefer Guard Clauses over nested conditionals</title>
<content type="html">
            Ever find yourself wrapping a method with a conditional:
&lt;pre class='ruby'&gt;
  def save_to_file(filename)
    unless filename.blank?
      do_something
      do_something_else
    end
  end
&lt;/pre&gt;

	&lt;p&gt;Or maybe you subscribe to the &lt;strong&gt;one exit from a method&lt;/strong&gt; strategy.&lt;/p&gt;


	&lt;p&gt;I write code that looks like this a lot.  And you know what, I didn&#8217;t think it had a code smell until my colleague &lt;a href='http://www.twitter.com/abdels'&gt;Abdel&lt;/a&gt; looked over it the other day.&lt;/p&gt;


	&lt;p&gt;&#8220;Why don&#8217;t you ever use guard clauses?&#8221; he asked.&lt;/p&gt;


A guard clause is a conditional statement at the top of a function that bails out as soon as it can.  Like this:
&lt;pre class='ruby'&gt;
def save_to_file(filename)
  return false if filename.blank? #&amp;lt;- Guard clause

  do_something
  do_something_else
  File.write...# blah blah
end
&lt;/pre&gt;

	&lt;p&gt;I didn&#8217;t have a good reason for not using Guard Clauses, except an idea in the back of my head that there should only be one exit from a method.  There&#8217;s a bunch of stuff online that promotes this way of writing methods, but I&#8217;ve discovered recently that most of the reasons to support one exit are &lt;strong&gt;baloney&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;Functions with single exit points are handy for languages that require decent memory management like C, but in Ruby where you have a garbage collector this doesn&#8217;t make any sense.&lt;/p&gt;


	&lt;p&gt;Also, if you&#8217;re writing uber long functions, then it makes sense to have a single exit point &#8211; but if you&#8217;re writing uber long functions then &lt;strong&gt;you&#8217;re a terrible programmer&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;Anyway, so &lt;a href='http://www.twitter.com/abdels'&gt;Abdel&lt;/a&gt; refactored my code by adding in some tasty guard clauses, and I&#8217;ve become a die hard fan.&lt;/p&gt;


	&lt;p&gt;PS: Of course, with all refactoring it&#8217;s about the readability and maintainability of your code.  Don&#8217;t blindly adopt guard clauses if it makes your code harder to read.&lt;/p&gt;


	&lt;p&gt;&lt;span class='caps'&gt;PPS&lt;/span&gt;: Read more about guard clauses at &lt;a href='http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html'&gt;refactoring.com&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-12-11:181</id>
    <published>2008-12-11T18:57:00Z</published>
    <updated>2009-10-02T13:01:19Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/12/11/localizing-your-rails-application" rel="alternate" type="text/html"/>
    <title>Localizing your Rails application</title>
<content type="html">
            &lt;div class='note'&gt;
I wrote a better version of this article for the Ruby Advent 2008 site.  Find it here: &lt;a href='http://advent2008.hackruby.com/past/2008/12/16/rails_22_internationalization/'&gt;http://advent2008.hackruby.com/past/2008/12/16/rails_22_internationalization/&lt;/a&gt;
&lt;/div&gt;

	&lt;p&gt;Recently I discovered that most of the visitors to one of my apps (&lt;a href='http://www.whatiwantforxmas'&gt;What I want for Xmas&lt;/a&gt;) were from Italy.  It suddenly seemed like a good time to localize my Rails application, but I had no idea where to start!
I asked my twitter stream for recommendations for a good Rails plugin.&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://www.twitter.com/peterc'&gt;Peter Cooper&lt;/a&gt; pointed me in the right direction:&lt;/p&gt;


	&lt;p&gt;&lt;img title='Peter Cooper sets me straight' src='http://www.thechrisoshow.com/assets/2008/12/11/Twitter___Peter_Cooper___thechrisoshow__Rails_2.2..jpg' alt='Peter Cooper sets me straight' /&gt;&lt;/p&gt;


	&lt;p&gt;Yep!  Rails 2.2 includes nice localization stuff right out of the box!&lt;/p&gt;


	&lt;p&gt;So, after some hard work I localized my app to cater for Italian visitors.  (You can check out the Italian version &lt;a href='http://www.whatiwantforxmas.com/locales/it'&gt;here&lt;/a&gt;).&lt;/p&gt;


	&lt;p&gt;Here&#8217;s how I did it:&lt;/p&gt;


	&lt;h3&gt;Get your Rails app ready for localization&lt;/h3&gt;


	&lt;p&gt;In Rails 2.2 you get a folder that keeps all your locale details: /config/locales.  This is where you&#8217;ll keep all the different locale snippets in files named after the locales (en.yml, it.yml etc)&lt;/p&gt;


You can set the default locale by setting a flag in environment.rb:
&lt;pre class='ruby'&gt;
      config.i18n.default_locale = :en
&lt;/pre&gt;

	&lt;h3&gt;Translate your application into English&lt;/h3&gt;


	&lt;p&gt;Next (oddly enough) you&#8217;ll need to translate your application into English (or whatever language your app is already).  This means finding all the places where you have fragments of English and replacing it with something that looks like this:&lt;/p&gt;


&lt;pre class='ruby'&gt;
t(&quot;main.title&quot;)
&lt;/pre&gt;

The &lt;strong&gt;t* stands for translate, and *main.title&lt;/strong&gt; refers to a key in the locale yaml files.  The yaml file can have nested keys, so, &#8220;main.title&#8221; finds the &lt;em&gt;title&lt;/em&gt; key belonging to the &lt;em&gt;main&lt;/em&gt; key in the yaml file.  e.g.
&lt;pre&gt;
    main:
        title: &quot;Some title&quot; 
&lt;/pre&gt;

You can use string interpolation as well.  So if you have text that looks like this:
&lt;pre class='ruby'&gt;
    &quot;What #{name} wants for Xmas&quot; 
&lt;/pre&gt;
You can simply put the name of the variable within double braces in the yaml line like so:
&lt;pre&gt;
    title: &quot;What {{name}} wants for Xmas&quot; 
&lt;/pre&gt;

And then call on it with the &lt;em&gt;t&lt;/em&gt; method by passing in the variable.  Like this:
&lt;pre class='ruby'&gt;
    t(&quot;title&quot;, :name =&amp;gt; user.first_name)
&lt;/pre&gt;

	&lt;h3&gt;Translate your application into other languages&lt;/h3&gt;


	&lt;p&gt;After you&#8217;ve gone through the &lt;strong&gt;arduous&lt;/strong&gt; process of translating your app to English (yawn!) you can then create translations into other languages!&lt;/p&gt;


	&lt;p&gt;What I did was just grab the yaml file, and send it to an Italian friend to translate.&lt;/p&gt;


Here&#8217;s a portion of an example English yaml file:
&lt;pre&gt;
en:
  main_title: &quot;What I want for Xmas&quot; 
  countdown:
    merry_xmas: &quot;Merry Christmas!&quot; 
    days_until_xmas: &quot;Only {{number_of_days}} until Xmas&quot; 
    days: &quot;days&quot; 
    day: &quot;day&quot; 
&lt;/pre&gt;

And here&#8217;s the translated version:
&lt;pre&gt;
it:
  main_title: &quot;Ciò che voglio per Natale!&quot; 
  countdown:
    merry_xmas: &quot;Buon Natale!&quot; 
    days_until_xmas: &quot;Solo {{number_of_days}} fino a Natale&quot; 
    days: &quot;giorni&quot; 
    day: &quot;giorno&quot; 
&lt;/pre&gt;

	&lt;h3&gt;Provide a way to switch between different locales&lt;/h3&gt;


So, now you&#8217;ll need to add the ability to switch locales.  What I did was put a &lt;code&gt;before_filter&lt;/code&gt; in application.rb that sets the locale depending on:
	&lt;ul&gt;
	&lt;li&gt;whether the user is logged in and has a locale&lt;/li&gt;
		&lt;li&gt;whether the locale is stored in the session&lt;/li&gt;
		&lt;li&gt;failing that, use the default_locale &#8211; although this step could be unnecessary, but makes me feel comfortable.&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre class='ruby'&gt;
    # in Application.rb
    before_filter :set_locale  

    def set_locale
        I18n.locale = (current_user.locale if current_user) || session[:locale] || I18n.default_locale
    end
&lt;/pre&gt;

I also included a locales controller for switching locality.  
&lt;pre class='ruby'&gt;
# locales_controller.rb
class LocalesController &amp;lt; ApplicationController  
  def show
    if current_user # if I'm logged in
      current_user.locale = params[:locale] # change my locality
      current_user.save
    end        
        session[:locale] = params[:locale]               

    redirect_to :back
  end
end

# routes.rb

  map.locales 'locales/:locale', :controller =&amp;gt; 'locales', :action =&amp;gt; 'show'
&lt;/pre&gt;

This enabled me to have links for simply switching languages that look like this:
&lt;pre class='ruby'&gt;
    link_to &quot;English&quot;, locales_path('en')
&lt;/pre&gt;

	&lt;h3&gt;Tips&lt;/h3&gt;


	&lt;p&gt;Localizing your app can be a frustrating experience.  Here&#8217;s some tips to ease the pain:&lt;/p&gt;


	&lt;h2&gt;Repeat yourself&lt;/h2&gt;


	&lt;p&gt;Resist the urge to &lt;span class='caps'&gt;DRY&lt;/span&gt; up your yaml snippets and have them used in several different locations.  Different languages could translate your snippets differently depending on context.&lt;/p&gt;


	&lt;h2&gt;Provide context for your translators&lt;/h2&gt;


	&lt;p&gt;Your translators need to know a little bit about the context that the text represents.  Write comments in your yaml file to make it easier for them.&lt;/p&gt;


	&lt;h2&gt;Avoid the &lt;code&gt;pluralize&lt;/code&gt; method&lt;/h2&gt;


	&lt;p&gt;Right now, you can&#8217;t trust the &lt;code&gt;pluralize&lt;/code&gt; helper method if you&#8217;re using localization. (i.e. it tried to say that the plural of giorno (day) is giornos (it&#8217;s actually giorni)
Instead, provide keys in your translation yaml files asking for the plural and singular words, and write a method for dealing with the plural.&lt;/p&gt;


	&lt;h2&gt;Adopt some sort of convention in your yaml files for links that are embedded in other text.&lt;/h2&gt;


For example, if you wanted to write 
&lt;a href='http://www.whatiwantforxmas.com/signup'&gt;Sign up&lt;/a&gt; if you want to join whatiwantforxmas.com
 I would separate this out in the yaml file like this:
&lt;pre&gt;
    sign_up: &quot;{{link}} if you want to join whatiwantforxmas.com&quot; 
    sign_up_link: &quot;Sign up&quot; # this is the text used in the 'link' variable above
&lt;/pre&gt;

	&lt;p&gt;Just because the &#8220;Sign up&#8221; appears at the front of the sentence in English, doesn&#8217;t make it so in another language.&lt;/p&gt;


	&lt;h2&gt;Remember ActiveRecord validation messages and dates&lt;/h2&gt;


	&lt;p&gt;Rails includes numerous English phrases.  You can translate this quite easily by including the sample code found &lt;a href='http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale'&gt;here&lt;/a&gt; in your yaml files.&lt;/p&gt;


	&lt;h2&gt;What&#8217;s this &lt;em&gt;Yaml&lt;/em&gt; thing?&lt;/h2&gt;


	&lt;p&gt;Ideally you&#8217;ll want to get your translators to translate the yaml file.  Beware though that yaml is a  strange file format to non-technical people.  Let your translators know here do get a file editor that they can use (SciTE for example.  Grab it at &lt;a href='http://prdownloads.sourceforge.net/scintilla/Sc177.exe'&gt;http://prdownloads.sourceforge.net/scintilla/Sc177.exe&lt;/a&gt;)&lt;/p&gt;


	&lt;h2&gt;But is it &lt;span class='caps'&gt;REALLY&lt;/span&gt; right&lt;/h2&gt;


	&lt;p&gt;After getting the translation, walk through the website with the translator to make sure it&#8217;s &lt;span class='caps'&gt;REALLY&lt;/span&gt; right.  Sometimes things translate differently when the context changes.  I also noticed that some special characters sometimes got mixed up, so this is a good opportunity to spot those.&lt;/p&gt;


	&lt;h2&gt;Translate &lt;span class='caps'&gt;ALL&lt;/span&gt; of the snippets&lt;/h2&gt;


	&lt;p&gt;As I write this, if any of your translation files are missing a snippet, then you&#8217;ll get nasty &#8216;span&#8217; classes outputted onto the screen.  Make sure that each and everyone of your snippets are updated in every locale yaml file.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-10-27:179</id>
    <published>2008-10-27T23:49:00Z</published>
    <updated>2008-11-10T09:58:18Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/10/27/adding-gravatars-to-your-rails-apps" rel="alternate" type="text/html"/>
    <title>Add Gravatars to your Rails Application</title>
<content type="html">
            &lt;p&gt;I wanted to add Avatars to my new project &lt;a href='http://www.whatiwantforxmas.com'&gt;whatiwantforxmas.com&lt;/a&gt;  but it felt like too much hard work.  So, I took a leaf out of &lt;a href='http://www.github.com'&gt;githubs&lt;/a&gt; book and used &lt;strong&gt;Gravatars&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;Gravatars are an already pre-packaged avatar system, all bright and shiny and ready to be added to your application.&lt;/p&gt;


&lt;div class='note'&gt;
If you haven&#8217;t got a Gravatar do me a favour, go over to &lt;a href='http://www.gravatar.com'&gt;gravatar.com&lt;/a&gt; and signup
&lt;/div&gt;

	&lt;p&gt;It&#8217;s &lt;strong&gt;ridiculously&lt;/strong&gt; easy to put in your app!&lt;/p&gt;


I use Scott Woods nice little Gravatar plugin &#8211; you can install it in your Rails app thusly:
&lt;pre&gt;
script/plugin install svn://rubyforge.org/var/svn/gravatarplugin/plugins/gravatar
&lt;/pre&gt;

Here&#8217;s how you use it, bung this into your view (where user is an object that has an &lt;strong&gt;email&lt;/strong&gt; attribute):
&lt;pre class='ruby'&gt;
gravatar_for(user)
&lt;/pre&gt;

	&lt;p&gt;And &lt;strong&gt;&lt;span class='caps'&gt;BOO YA&lt;/span&gt;&lt;/strong&gt; &#8211; you&#8217;ve got an instant gravatar image on your page!&lt;/p&gt;


You could also call by passing in an email address (but it&#8217;s not so sexy):
&lt;pre class='ruby'&gt;
gravatar(user.email)
gravatar(&quot;jim@banana.com&quot;)
&lt;/pre&gt;

You can even set which size you want!  (As in this case 50px x 50px)
&lt;pre class='ruby'&gt;
gravatar(user.email, :size =&amp;gt; 50)
&lt;/pre&gt;

	&lt;p&gt;&#8220;But wait!  What if the user doesn&#8217;t &lt;span class='caps'&gt;HAVE&lt;/span&gt; a Gravatar?!&#8221; 
Well, then a default image is used instead.  Typically something nasty like this:&lt;/p&gt;


	&lt;p&gt;&lt;img src='http://www.thechrisoshow.com/assets/2008/10/27/gravatar.jpg' alt='' /&gt;&lt;/p&gt;


But dry your eyes, all is not lost.  You can set your own default Gravatar! 
When you call the Gravatar method just pass in a :default param pointing to the default image you want to use.
&lt;pre class='ruby'&gt;
gravatar_for(user, :size =&amp;gt; 50, :default =&amp;gt; &quot;http://www.whatiwantforxmas.com/images/avatars/santa/santa-50.png&quot;)
&lt;/pre&gt;

	&lt;p&gt;I wasn&#8217;t into the scaling of the default images though, so I wrote this little snippet of code so that you can have a separate default image for each Gravatar size you want.  This ensures that I only use Gravatars that are 25, 50 or 70px (the sizes that I have default images for).&lt;/p&gt;


	&lt;p&gt;Copy this code into a file in your lib folder, and don&#8217;t forget to &#8216;require&#8217; it in an initializer.&lt;/p&gt;


&lt;pre class='ruby'&gt;
module GravatarHelper
  module PublicMethods

    AVATAR_SIZES = [25, 50, 70]

    def gravatar_for_with_default(object, options={})
      options[:size] ||= 25      
      options.merge!(:default =&amp;gt; &quot;http://www.whatiwantforxmas.com/images/avatars/santa/santa-#{options[:size]}.png&quot;)      
      # sizes are 25, 50 and 70
      raise RuntimeError, &quot;Incorrect size for Avatar&quot; unless AVATAR_SIZES.index(options[:size])    
      gravatar_for_without_default(object, options)
    end
    alias_method_chain :gravatar_for, :default
  end
end
&lt;/pre&gt;

And then I just need to call it like this:
&lt;pre class='ruby'&gt;
gravatar_for(user, :size =&amp;gt; 50) # This will use my default avatar
&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;PS&lt;/strong&gt; Yeah, I know I know &#8211; my blog doesn&#8217;t use Gravatars yet&#8230;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-10-02:156</id>
    <published>2008-10-02T19:23:00Z</published>
    <updated>2009-02-23T14:01:12Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/10/2/a-peek-at-the-inner-workings-of-hashrocket" rel="alternate" type="text/html"/>
    <title>A peek at the inner workings of Hashrocket</title>
<content type="html">
            &lt;p&gt;Recently the folk over at &lt;a href='http://www.hashrocket.com/'&gt;Hashrocket&lt;/a&gt; had another one of their famed 3-2-1 Launches - this time inviting as guest stars &lt;strong&gt;Tammer Saleh&lt;/strong&gt; and &lt;strong&gt;Joe Ferris&lt;/strong&gt; of another renowned Rails shop &lt;a href='http://www.thoughtbot.com'&gt;Thoughtbot&lt;/a&gt;.  The application they worked on is called &lt;a href='http://spot.us'&gt;spot.us&lt;/a&gt; and is a community funded reporting tool.&lt;/p&gt;

&lt;p&gt;The interesting thing about this project was that the source is freely available on Github for any Joe Bloggs to pour over (&lt;a href='http://github.com/spot-us/spot-us/tree/master'&gt;link&lt;/a&gt;).  Not only that, but the client taped his reaction upon seeing the application for the first time: (&lt;a href='http://blog.spot.us/2008/09/26/first-look-at-spotus-in-action/'&gt;link&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;I had a quick looksee at the application, and what I saw &lt;strong&gt;blew me away&lt;/strong&gt; - how'd they get so much done in such a short space of time?  &lt;/p&gt;

&lt;p&gt;Here's some of the stuff I noticed:&lt;/p&gt;

&lt;h3&gt;Used a good amount of boilerplate&lt;/h3&gt;

&lt;p&gt;Let's face it, a good chunk of every application is the same as the last application.  Hashrocket have got a pretty decent suite of boilerplate code that they bung into the app to get started.  &lt;/p&gt;

&lt;h3&gt;Upfront design&lt;/h3&gt;

&lt;p&gt;The week before the sprint, a bunch of stuff was checked in to do with user interface. this included:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mocked up views,&lt;/li&gt;
&lt;li&gt;All the images necessary for the design,&lt;/li&gt;
&lt;li&gt;Javascript plugins&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basically, a good solid structure was in place so that the developers could go hard on concentrating on what they need to do, and not worry about big design decisions.&lt;/p&gt;

&lt;p&gt;Not only that, but before any work had done, the application ''looked'' just like the final product, even if it didn't necessarily work yet.&lt;/p&gt;

&lt;h3&gt;Uber frequent commits&lt;/h3&gt;

&lt;p&gt;Take a look at the github network &lt;a href='http://github.com/spot-us/spot-us/network'&gt;tree&lt;/a&gt; on the 24th, 25th and 26th - every 6 or so minutes something gets checked in.  These folks checked in so fast it was as if their lives depended on it.&lt;/p&gt;

&lt;p&gt;This is because they had small, perfectly formed units of work.  Get the issue, work on it, check it in -&gt; rinse and repeat.
Also because of the hard-core frequent commits there were only 2 source code conflicts for the entire stretch!&lt;/p&gt;

&lt;h3&gt;Haml&lt;/h3&gt;

&lt;p&gt;I've studiously avoided haml throughout my Rails career because a niggling sensation at the back of my head doesn't feel comfortable with adding another layer of abstraction between me and the html.  I've been bitten before with all the asp.net stuff Microsoft uses to hide html from .NET developers - and have since loved looking at plain old html since.&lt;/p&gt;

&lt;p&gt;In spot-us all of the views are done in HAML.&lt;/p&gt;

&lt;p&gt;After reading it for a bit, and getting my head around all that white space, I realised that the intent of the code was really easy to comprehend - I didn't have to wade through all that excess html junk we put up with.  Instead the templates read like a page of code, filled with lovely methods and variables.&lt;/p&gt;

&lt;h3&gt;Used JQuery &lt;del&gt;AND&lt;/del&gt; NOT Prototype&lt;/h3&gt;

&lt;p&gt;So, there's a weird little nasty part of me that believes you have to take sides in the great Javascript Framework War.  Historically Rails developers belong to the Prototype/Scriptaculous faction - but all the hip cool kids these days seem to be gravitating towards JQuery.&lt;/p&gt;

&lt;p&gt;&lt;del&gt;Hashrocket instead decided to go with the best bits of both worlds by implementing bits of each.  In their code you can see the old glory (some would say divisive) use of remote_form_for (which generates prototype Javascript) combined with the new hotness of JQuery plugins.&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt; Thanks to Ben Scofield for pointing out in the comments that the Hashrocket team actually use the jRails plugin.  This plugin ensures that all the Rails javascript helper methods spit out JQuery rather than Prototype/Scriptaculous code.  Find out more &lt;a href='http://ennerchi.com/projects/jrails'&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;JQuery has a nice little method:&lt;/p&gt;

&lt;pre class='ruby'&gt;
jQuery.noConflict( );
&lt;/pre&gt;

&lt;p&gt;This helps to make sure that jQuery doesn't conflict with other libraries - pretty sweet.&lt;/p&gt;

&lt;h3&gt;Done away with fixtures and used factory_girl&lt;/h3&gt;

&lt;p&gt;The Geniuses (Geni-i?) at Thoughtbot forego the whole fixtures route in favour of creating custom factories for their tests.  The sweet little plugin that enables all this magic is called Factory Girl.&lt;/p&gt;

&lt;p&gt;Here's an example - instead of calling out to a donation fixture, they just create a factory of a couple and populate it with the relevant attrbutes.&lt;/p&gt;

&lt;pre class='ruby'&gt;
it &quot;isn't valid if there is already a donation for that tip and user&quot; do
  donation = Factory(:donation)
  duplicate = Factory.build(:donation, :pitch =&gt; donation.pitch, :user =&gt; donation.user)
  duplicate.should_not be_valid
  duplicate.should have(1).error_on(:pitch_id)
end
&lt;/pre&gt;

&lt;p&gt;I'm not sure if Hashrocket started using factory_girl in this project because of their special guest stars, but it's not part of the standard boilerplate code.&lt;/p&gt;

&lt;h3&gt;Rspec view tests&lt;/h3&gt;

&lt;p&gt;Something hot and juicy about the hashrocket test suite was the inclusion of &lt;strong&gt;View Specs&lt;/strong&gt;.  RSpec view tests allow you to set expectations of your views in a nice sweet fashion without having to worry about the whole http round trip business.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;pre class='ruby'&gt;
describe 'profiles/show' do

  include ActionView::Helpers::AssetTagHelper

  before do
    assigns[:profile] = Factory(:user)
  end

  it &quot;should display a thumbnail when available&quot; do
    url = '/url/to/file.jpg'
    attachment = mock('attachment', :url =&gt; url)
    assigns[:profile].stub!(:photo).and_return(attachment)
    do_render
    response.should have_tag('img[src = ?]', url)
  end
end
&lt;/pre&gt;

&lt;p&gt;Look ma, no controller!&lt;/p&gt;

&lt;h3&gt;Launch party&lt;/h3&gt;

&lt;p&gt;After a 3-2-1 Launch the hashrocket guys 'n' gals have an awesome big party.  Judging from the tweets, it sounded like a fun time was had by all.&lt;/p&gt;

&lt;p&gt;It got me thinking, most projects I've worked on don't specifically have an end point - they just kinda keep on going until they peter out and another project shows up.  Having a definite line in the sand is a great motivating factor.&lt;/p&gt;

&lt;h3&gt;And finally&lt;/h3&gt;

&lt;p&gt;There's still a bunch of fiddly stuff to do on spot-us - but it's pretty amazing what they came up with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rats off to ya!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src='http://www.thechrisoshow.com/assets/2008/10/2/rats_off_to_ya.jpg' alt='Rats off to ya!' /&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-10-01:157</id>
    <published>2008-10-01T10:48:00Z</published>
    <updated>2008-12-09T22:03:14Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/10/1/using-assert_select-on-strings" rel="alternate" type="text/html"/>
    <title>Using assert_select on strings</title>
<content type="html">
            &lt;h4&gt;Question:&lt;/h4&gt;


	&lt;p&gt;assert_select is awesome for testing dom objects &#8211; but it relies on using the standard rails response cycle.  &lt;strong&gt;How can I use assert_select on to find dom elements in a standard string?&lt;/strong&gt;&lt;/p&gt;


	&lt;h4&gt;Answer:&lt;/h4&gt;


	&lt;p&gt;Stub the response object!&lt;/p&gt;


&lt;pre class='ruby'&gt;
def test_big_selector_helper
  # the response requires a content_type - just set it to &quot;text&quot; 
  @response.stubs(:content_type).returns &quot;text&quot; 

  # big_element_selector is the helper method I'm testing in this instance
  # replace with the string that you want to test
  @response.stubs(:body).returns big_element_selector

  # now you can use assert_select
  assert_select &quot;ul&quot; do
    assert_select &quot;li&quot;, :count =&amp;gt; 3
  end
end
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-09-24:155</id>
    <published>2008-09-24T08:58:00Z</published>
    <updated>2008-09-24T10:05:32Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/9/24/set-the-default-date-format-for-your-rails-application" rel="alternate" type="text/html"/>
    <title>Set the default date format for your Rails application</title>
<content type="html">
            &lt;div class='note'&gt;
(Apologies to who I nicked this from &#8211; I can&#8217;t remember which book or blog post I found this)
&lt;/div&gt;

The default string format for Dates is the &#8216;Database&#8217; format which looks like this:
&lt;pre class='ruby'&gt;
Date.today.to_s =&amp;gt; &quot;2008-09-24&quot; 
&lt;/pre&gt;

	&lt;p&gt;So, this means if you have dates in your views they&#8217;ll default to that unless you force the format.&lt;/p&gt;


	&lt;p&gt;It&#8217;s really easy to set the default format though!  Bung something like this in your environment.rb:&lt;/p&gt;


&lt;pre class='ruby'&gt;
  ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  :default =&amp;gt; '%d %B %Y' )  
&lt;/pre&gt;

	&lt;p&gt;Where :default is the format you want to set.  Now your dates will automatically default to a nice format!&lt;/p&gt;


&lt;pre class='ruby'&gt;
Date.today.to_s =&amp;gt; &quot;24 September 2008&quot; 
&lt;/pre&gt;

	&lt;p&gt;You can do the same with the time format:&lt;/p&gt;


&lt;pre class='ruby'&gt;
  ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
  :default =&amp;gt; '%d %B %Y'
  )  
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-05-07:67</id>
    <published>2008-05-07T08:51:00Z</published>
    <updated>2008-08-26T13:59:42Z</updated>
    <link href="http://www.thechrisoshow.com/2008/5/7/display-your-kml-on-a-google-map-now-using-the-google-app-engine" rel="alternate" type="text/html"/>
    <title>Display your KML on a Google Map (again) - now using Google App Engine</title>
<content type="html">
            &lt;p&gt;&lt;a href='http://en.wikipedia.org/wiki/Keyhole_Markup_Language'&gt;&lt;span class='caps'&gt;KML&lt;/span&gt;&lt;/a&gt; stands for Keyhole Markup Language and is used to create &lt;em&gt;geographically aware&lt;/em&gt; information.&lt;/p&gt;


	&lt;p&gt;It can be tricky to write your own &lt;span class='caps'&gt;KML&lt;/span&gt; by hand- in order to display kml on a Google Map you need to save the markup into a file on a web server that Google has access to.  Google then caches this file in it&#8217;s own mega-web, so it can take a while for any changes you make to filter through.&lt;/p&gt;


	&lt;p&gt;My little application fixes these issues by allowing you to paste the kml into a web form, hit a button, and &lt;span class='caps'&gt;WHAMMO&lt;/span&gt;, KML on a Google Map without any of the stupid stuff.&lt;/p&gt;


	&lt;p&gt;So, almost a year after creating this application with Ruby on Rails I&#8217;ve decided to port this little app to use &lt;a href='http://code.google.com/appengine/'&gt;Google App Engine&lt;/a&gt; &#8211; you shouldn&#8217;t notice any difference, but rest assured that it can now scale to support gabillions of visits.&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;Because everyone&#8217;s gonna wanna use it&lt;/em&gt;&lt;/p&gt;


To try it out by going &lt;a href='http://www.thechrisoshow.com/display_kml'&gt;here&lt;/a&gt; and copy and paste the following &lt;span class='caps'&gt;KML&lt;/span&gt; into the box and click the button.
&lt;pre&gt;
&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;
&amp;lt;kml xmlns=&quot;http://earth.google.com/kml/2.2&quot;&amp;gt;
  &amp;lt;Placemark&amp;gt;
    &amp;lt;name&amp;gt;Invercargill Rocks!&amp;lt;/name&amp;gt;
    &amp;lt;description&amp;gt;Go there and see!&amp;lt;/description&amp;gt;
    &amp;lt;Point&amp;gt;
      &amp;lt;coordinates&amp;gt;168.34693908691406,-46.416322245553296,5&amp;lt;/coordinates&amp;gt;
    &amp;lt;/Point&amp;gt;
  &amp;lt;/Placemark&amp;gt;
&amp;lt;/kml&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;Display &lt;span class='caps'&gt;KML&lt;/span&gt; on a Google Map: &lt;a href='http://www.thechrisoshow.com/display_kml'&gt;http://www.thechrisoshow.com/display_kml&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://code.google.com/apis/kml/documentation/kml_tut.html'&gt;&lt;span class='caps'&gt;KML&lt;/span&gt; Samples&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://code.google.com/apis/kml/documentation/kml_21tutorial.html'&gt;&lt;span class='caps'&gt;KML&lt;/span&gt; Tutorial&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://code.google.com/apis/kml/documentation/kmlreference.html'&gt;&lt;span class='caps'&gt;KML&lt;/span&gt; Reference&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;PS: Because I rewrote the code in Python it might&#8217;ve broken a few things &#8211; let me know in the comments if it doesn&#8217;t work for you.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-05-04:59</id>
    <published>2008-05-04T22:51:00Z</published>
    <updated>2008-08-26T13:59:59Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/5/4/five-tips-for-testing-rails" rel="alternate" type="text/html"/>
    <title>Five tips for testing Rails</title>
<content type="html">
            &lt;h3&gt;Tip One &#8211; Debugging tests&lt;/h3&gt;


	&lt;p&gt;I use RDebug in my apps a lot, but frequently I found myself wanting to debug my tests.  Here&#8217;s how you can do it:&lt;/p&gt;


If you haven&#8217;t done so already, install RDebug
&lt;pre&gt;
  $ gem install ruby-debug
&lt;/pre&gt;

Insert &lt;i&gt;debugger&lt;/i&gt; in your code where you&#8217;d like to breakpoint
&lt;pre class='ruby'&gt;
    def like_bananas?
      # Want to breakpoint here and see what the self.fruit attribute looks like
      debugger
      return self.fruit.likes.include?('bananas')
    end      
&lt;/pre&gt;
And then run the test from the command line using rdebug like this:
&lt;pre&gt;
    rdebug test/unit/bananas_test.rb
&lt;/pre&gt;

	&lt;p&gt;You could even put the &lt;i&gt;debugger&lt;/i&gt; reference in your test code as well.&lt;/p&gt;


	&lt;h3&gt;Tip Two &#8211; Gently ease yourself into &lt;span class='caps'&gt;BDD&lt;/span&gt; with shoulda&lt;/h3&gt;


	&lt;p&gt;Everybody&#8217;s talking about Behaviour Driven Development these days &#8211; and the popular &lt;span class='caps'&gt;BDD&lt;/span&gt; tool for Rails is the mighty RSpec.&lt;/p&gt;


	&lt;p&gt;But, it can seem like quite a bit of a jump to get into RSpec.  Maybe you&#8217;ve got a well established application with a plethora of Test::Unit tests that you can&#8217;t bear to look at.  Or maybe you just wanna try out the whole &lt;span class='caps'&gt;BDD&lt;/span&gt; approach for a while and see how it goes without really committing.&lt;/p&gt;


	&lt;p&gt;Well never fear &#8211; there&#8217;s a really easy way of easing yourself into &lt;span class='caps'&gt;BDD&lt;/span&gt; and it&#8217;s called &lt;a href='http://thoughtbot.com/projects/shoulda'&gt;shoulda&lt;/a&gt;.  This is a great &lt;span class='caps'&gt;BDD&lt;/span&gt; tool made by the crazy freakin&#8217; geniuses (genii?) over at &lt;a href='http://thoughtbot.com/'&gt;thoughtbot&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Basically, shoulda is a &lt;span class='caps'&gt;BDD&lt;/span&gt; framework built &lt;i&gt;on top of&lt;/i&gt; Test::Unit.  It uses all the Test::Unit assertions that we know and love &#8211; adds some exotic delicous syntactic &lt;span class='caps'&gt;BDD&lt;/span&gt; sugar &#8211; and throws some wonderful helper macros in as freebies..&lt;/p&gt;


	&lt;p&gt;The thoughtbot folks rabbit on about it over here: &lt;a href='http://thoughtbot.com/projects/shoulda'&gt;http://thoughtbot.com/projects/shoulda&lt;/a&gt;.&lt;/p&gt;


An example test unit class might look like this:
&lt;pre class='ruby'&gt;
class FruitBowlTests &amp;lt; Test::Unit::TestCase

  def setup
    @fruit_bowl = get_fruit_bowl
  end

  # Check out this BDD coolness
  should &quot;be best before date&quot; do
    assert @fruit_bowl.fresh?
  end

  # But you can still include old skool tests like this
  def test_fruit_bowl_has_three_fruit
    assert_equal 3, @fruit_bowl.fruit.size
  end

end
&lt;/pre&gt;

	&lt;p&gt;So you can try out &lt;span class='caps'&gt;BDD&lt;/span&gt; without messing up your current tests &#8211; hooray!  Shoulda actually provides way more than this though &#8211; but don&#8217;t take my word for it, gem install shoulda today!&lt;/p&gt;


PS: Not only that, but the shoulda guys include a script that modifies your Test::Unit tests and changes them into shoulda style tests.  Run it like this:
&lt;pre&gt;
 $ ./vendor/plugins/shoulda/bin/convert_to_should_syntax [path to test file]
&lt;/pre&gt;

	&lt;h3&gt;Tip Three &#8211; &lt;span class='caps'&gt;DRY&lt;/span&gt; up your tests with define method&lt;/h3&gt;


Ever find yourself writing the same tests over and over again but with slightly different modifications &#8211; like this:
&lt;pre class='ruby'&gt;
  def test_banana_should_be_nil_if_fruit_bowl_rotten
    @fruit_bowl = FruitBowl.new(:rotten =&amp;gt; true)
    assert_nil @fruit_bowl.banana
  end

  def test_apple_should_be_nil_if_fruit_bowl_rotten
    @fruit_bowl = FruitBowl.new(:rotten =&amp;gt; true)
    assert_nil @fruit_bowl.apple
  end

  def test_pear_should_be_nil_if_fruit_bowl_rotten
    @fruit_bowl = FruitBowl.new(:rotten =&amp;gt; true)
    assert_nil @fruit_bowl.pear
  end
&lt;/pre&gt;

	&lt;p&gt;Using the wonderful Ruby methods &lt;i&gt;define_method&lt;/i&gt; and &lt;i&gt;send&lt;/i&gt; you can dry this up really easily like thus:&lt;/p&gt;


&lt;pre class='ruby'&gt;
# Put the different bits into an array
[:banana, :apple, :pear].each do |fruit|
  # Iterate over the array and create a test method for each object.  Remember to prefix the method with 'test' when using Test::Unit
    define_method &quot;test_#{fruit}_should_be_nil_if_fruit_bowl_rotten&quot; do
      @fruit_bowl = FruitBowl.new(:rotten =&amp;gt; true)
      # And use the send method to check the correct attribute
         assert_nil @fruit_bowl.send(fruit)
    end
  end

&lt;/pre&gt; 

	&lt;h3&gt;Tip Four &#8211; Test the objects behaviour&lt;/h3&gt;


	&lt;p&gt;Here&#8217;s a tip that should revolutionise your testing methodology forever&#8230;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;Make sure you test the correct behaviour for an object&lt;/b&gt;&lt;/p&gt;


	&lt;p&gt;What this means is that when testing an object, don&#8217;t create a test that looks outside of its scope.&lt;/p&gt;


	&lt;p&gt;For example:&lt;/p&gt;


	&lt;p&gt;Up until very recently I used to be a big fixtures man.  It wasn&#8217;t uncommon for me to write functional tests like this:&lt;/p&gt;


&lt;pre class='ruby'&gt;
    def test_transfers_heading_should_be_on_page
      get :show, :id =&amp;gt; festivals(:festival_with_transfers_heading)    
      assert_select 'h3#heading', &quot;Transfers&quot; 
    end
&lt;/pre&gt;

	&lt;p&gt;Which was to test that the h3 with the id of &#8216;heading&#8217; displayed the correct text, in this case &#8220;transfers&#8221;.&lt;/p&gt;


	&lt;p&gt;But deep down inside I was relying on this test to do more than what it should be doing.  Whenever this test was run it had to load the festival from the database, and show the correct template.  However, the scope of the test though was just to test that the correct information was shown in the view, anything else was irrelevant to this test.&lt;/p&gt;


A better test could look like this:
&lt;pre class='ruby'&gt;
    def test_transfers_heading_should_be_on_page
      festival = Festival.new(:heading =&amp;gt; 'Transfers')
      Festival.stubs(:find).returns(festival)
      get :show, :id =&amp;gt; 1
      assert_select 'h3#heading', &quot;Transfers&quot; 
    end
&lt;/pre&gt;

	&lt;p&gt;The database doesn&#8217;t get hit because it doesn&#8217;t need to get hit.  All we&#8217;re testing here is that the correct heading is displayed on the page &#8211; that the controller fulfilled its obligation by marshalling the data from the model to the view.&lt;/p&gt;


	&lt;h3&gt;Tip Five &#8211; Expectations are tests too you know!&lt;/h3&gt;


	&lt;p&gt;Mocks Rock &#8211; there I said it!&lt;/p&gt;


	&lt;p&gt;But, you can get so obsessed with stubbing out all manner of stuff for your assertions that you can mess out on all the nice inbuilt assertions built into mock objects &#8211; expectations.&lt;/p&gt;


For example,  you might have an ActiveRecord observer that does some stuff after a model is saved like this:
&lt;pre class='ruby'&gt;
  class FruitObserver &amp;lt; Observer

   def after_create(fruit)
     # Add to all the inventory lists
     InventoryList.find(:all).each do |i|
       i.stock.push fruit
     end    
   end
  end
&lt;/pre&gt;

You could use a mocha expectation to test this without using a single assert_blah like this:
&lt;pre class='ruby'&gt;
  def test_when_creating_banana_observer_is_called
    banana = Fruit.new(:name =&amp;gt; &quot;banana&quot;)
    InventoryList.any_instance.expects(:push).with(banana)
    banana.save
  end
&lt;/pre&gt;

	&lt;p&gt;You don&#8217;t need to have assert_blah in all your tests &#8211; this is a perfectly acceptable test because it includes the expectation     InventoryList.any_instance.expects(:push).with(banana).&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-04-18:65</id>
    <published>2008-04-18T14:51:00Z</published>
    <updated>2008-07-30T10:23:31Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/4/18/bug-in-mephisto-when-adding-comments" rel="alternate" type="text/html"/>
    <title>Is your Mephisto Blog not accepting new comments?</title>
<content type="html">
            &lt;p&gt;This blog uses &lt;a href='http://mephistoblog.com'&gt;Mephisto&lt;/a&gt; &#8211; which is pretty cool and works a treat &#8211;  but I realised to my horror the other day that the commenting system appeared to have stopped working.&lt;/p&gt;


	&lt;p&gt;It drove me bananas!  Why would it work one day, but not the next!  Why would it work on my local box but not on production?!?&lt;/p&gt;


	&lt;p&gt;Turns out there&#8217;s a known bug with Mephisto when used with Apache that prevents additional comments from being added to an article that already has a comment.&lt;/p&gt;


	&lt;p&gt;You can fix this by adding the following line to the bottom of your .htaccess file:&lt;/p&gt;


&lt;pre&gt;
  DirectorySlash Off 
&lt;/pre&gt;

	&lt;p&gt;And voila &#8211; comments will start working again.&lt;/p&gt;


	&lt;p&gt;Thanks to &lt;a href='http://blog.gilluminate.com/2007/2/12/mephisto-comments-breaking-in-apache/'&gt;Jason Gill&lt;/a&gt; for this tip.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-04-13:108</id>
    <published>2008-04-13T08:31:00Z</published>
    <updated>2008-09-24T08:56:30Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/4/13/five-rails-tips" rel="alternate" type="text/html"/>
    <title>Five tips for contributing to Rails</title>
<content type="html">
            &lt;div class='note'&gt;
Note: these tips assume a basic understanding of how to contribute to Rails.  

If you&#8217;ve never contributed before I heartily recommend you check out Josh Sussers presentation &lt;a href='http://blog.hasmanythrough.com/2007/5/24/laying-tracks-slides'&gt;Laying tracks&lt;/a&gt; or Ryan Bates excellent &lt;a href='http://railscasts.com/episodes/50'&gt;railscast&lt;/a&gt; on the topic.

&lt;/div&gt;

	&lt;h3&gt;Tip One &#8211; Test ActiveRecord tests with different database types&lt;/h3&gt;


	&lt;p&gt;When creating a patch for ActiveRecord you need to make sure your patch works with different databases.  I learned this the hard way when I submitted a patch that worked perfectly in mysql and broke in every other database type.&lt;/p&gt;


If you have the database management system set up correctly and also added the required databases (activerecord_unittest and activerecord_unittest2) then you can run the following rake task in the rails/activerecord folder:

&lt;pre&gt;
  $ rake test_[database type]
&lt;/pre&gt;
Where [database type] is the database system you&#8217;re looking to run the tests on.

For example:
&lt;pre&gt;
  $ rake test_mysql
  $ rake test_sqlite
  $ rake test_postgresql
&lt;/pre&gt;
You can also use this rake task to run a specific ActiveRecord test.

For example, to run the base_test.rb tests using mysql:
&lt;pre&gt;
  $ rake test_mysql TEST=test/cases/base_test.rb
&lt;/pre&gt;

	&lt;h3&gt;Tip Two &#8211; Run ActionPack tests in TextMate&lt;/h3&gt;


	&lt;p&gt;When I&#8217;m working on a Rails application I get pretty addicted to running tests in TextMate with Command-R.  I found when working on Rails patches that it wasn&#8217;t quite so tidy.&lt;/p&gt;


	&lt;p&gt;&lt;img src='http://www.thechrisoshow.com/assets/2008/4/17/addresses_render_tes-2.jpg' alt='' /&gt;&lt;/p&gt;


	&lt;p&gt;ActionPack tests typically have require &#8216;abstract_unit&#8217; at the beginning of the file.&lt;/p&gt;


At the top of the test file add:
&lt;pre class='ruby'&gt;
$: &amp;lt;&amp;lt; &quot;..&quot; 
&lt;/pre&gt;

	&lt;p&gt;An explanation of what these strange hieroglyphs mean:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;$:&lt;/strong&gt; is the search path (you can think of it as being an array of directory path names)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;

	&lt;li&gt;&lt;strong&gt;&#8221;..&#8221;&lt;/strong&gt; the parent folder &#8211; you could have &#8217;../tests/blahblah&#8217; etc if you wanted to&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;&amp;lt;&amp;lt;&lt;/strong&gt; pushes an element onto an array&lt;/li&gt;

	&lt;/ul&gt;


	&lt;p&gt;And now it finds &#8216;abstract_unit&#8217; in the parent folder, allowing the tests to run within TextMate &#8211; hooray!
&lt;img src='http://www.thechrisoshow.com/assets/2008/4/17/addresses_render_tes-1.jpg' alt='' /&gt;&lt;/p&gt;


Obviously, if abstract_unit isn&#8217;t in the parent folder, then you should adjust this to search in the right folder for abstract_unit.  For example:
&lt;pre class='ruby'&gt;

$: &amp;lt;&amp;lt; &quot;../tests/helpers&quot; 
&lt;/pre&gt;

&lt;div class='note'&gt;
Note: Make sure you remove the extra line before creating your patch
&lt;/div&gt;

	&lt;h3&gt;Tip Three &#8211; ActiveRecord script/console&lt;/h3&gt;


	&lt;p&gt;Anyone who&#8217;s spent anytime working on an ActiveRecord Rails patch will realise that the included test models can get pretty complicated.  That&#8217;s why I like to create a special &lt;span class='caps'&gt;IRB&lt;/span&gt; console for playing around with the relationships.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Download this file: &lt;a href='http://www.thechrisoshow.com/assets/2008/4/13/script_console_for_rails.zip'&gt;script_console_for_rails.zip&lt;/a&gt; and unzip it into your rails/activerecord/ folder.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;

	&lt;li&gt;Still in the activerecord folder run:&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;
    $ script/console
&lt;/pre&gt;

	&lt;ul&gt;
	&lt;li&gt;This opens a special &lt;span class='caps'&gt;IRB&lt;/span&gt; console that includes all the Rails test models and fixtures &#8211; allowing for cool stuff like this:&lt;/li&gt;

	&lt;/ul&gt;


	&lt;p&gt;&lt;img src='http://www.thechrisoshow.com/assets/2008/4/13/Terminal_—_ruby_—_80×24.jpg' alt='' /&gt;&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;You can open it with different database types like this:&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;
    $ script/console sqlite3
    $ script/console postgresql
    etc
&lt;/pre&gt;

	&lt;h3&gt;Tip Four &#8211; Go through old unloved patches and fix them up&lt;/h3&gt;


	&lt;p&gt;Stuck for ideas on how you can contribute to Rails?  One great way is by going through old Rails patches in Trac and bring them up to speed.&lt;/p&gt;


Some patches work fine, but haven&#8217;t been accepted for a number of reasons:
	&lt;ul&gt;
	&lt;li&gt;They need documentation&lt;/li&gt;
		&lt;li&gt;They need tests&lt;/li&gt;
		&lt;li&gt;The code that the patch applies to has been changed&lt;/li&gt;
	&lt;/ul&gt;

	&lt;p&gt;This &lt;a href='http://dev.rubyonrails.org/report/11'&gt;link&lt;/a&gt; provides a list of patches that are untested, undocumented and incomplete.&lt;/p&gt;


	&lt;p&gt;Or you could just browse through the old open bugs &lt;a href='http://dev.rubyonrails.org/report/1'&gt;here&lt;/a&gt; for patches that work but need some attention.&lt;/p&gt;


	&lt;h3&gt;Tip Five &#8211; Validate other peoples patches&lt;/h3&gt;


	&lt;p&gt;Last year, the Rails Core team made a decision that patches need to be verified by three other contributors before being considered for acceptance.  With this arrangement, the Core team now no longer have to waste their time examining half-finished patches, and the quality of patches are higher.&lt;/p&gt;


	&lt;p&gt;Verifying a patch is more than just agreeing with the idea, it requires a bit of effort.&lt;/p&gt;


Here’s the steps you need to take to verify a patch:
	&lt;ul&gt;
	&lt;li&gt;Find a patch that needs verifying.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This bit&#8217;s easy.  Just waltz on into #rails-contrib on freenode and ask &#8220;anyone want me to +1 their patch?&#8221; and prepare to be bombarded with requests.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Agree with what the patch does&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This may seem obvious, but there&#8217;s no need to verify a patch if you don&#8217;t believe that the patch belongs in Rails.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Look at the source code&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Give the code an eyeball in trac before downloading it.  Make sure that the patch includes appropriate tests and documentation, and the code doesn&#8217;t have any glaring peculiarities.  If it&#8217;s lacking in one of these areas post a comment on the ticket so that the contributor can make the necessary fixes.&lt;/p&gt;

	&lt;p&gt;If however you&#8217;re happy with how the patch looks then it&#8217;s time to take it for a test drive.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Update your own Rails repository&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This is so that when you install the patch you can be sure that it patches cleanly.  If it doesn&#8217;t then post a comment on the trac ticket letting the contributor know so they can fix it.&lt;/p&gt;


If you&#8217;re using subversion then run
&lt;pre&gt;
    svn update
&lt;/pre&gt;

	&lt;ul&gt;

	&lt;li&gt;Download the patch&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Sometimes trac tickets have multiple patch files, so make sure you&#8217;ve got the right one(s).&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Install the patch&lt;/li&gt;

	&lt;/ul&gt;


	&lt;p&gt;Now install the patch on your own repository using a command like:&lt;/p&gt;


&lt;pre&gt;
    $ patch -p0 -i [patch_file.diff]
&lt;/pre&gt;
(Replace [patch_file.diff] with the file path of the patch you want to apply.)

If the patch doesn&#8217;t apply cleanly you&#8217;ll get a message like:
&lt;pre&gt;

    Hunk #2 FAILED at 738.
&lt;/pre&gt;
This is a sign that the code base has changed since the patch was created.  The contributor will need to update the patch.  Make sure you post a comment on the ticket with the details.

	&lt;ul&gt;
	&lt;li&gt;Run the tests to make sure they all pass&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;
    $ rake test
&lt;/pre&gt;

	&lt;ul&gt;
	&lt;li&gt;Try out the patch with a test Rails application.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I like to keep my Rails repository in the vendor folder of a dummy application that for testing patches.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Verify&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;If the patch works, and there are no obvious side effects, then you can +1 the trac ticket.  Simply post a comment on the ticket with something along the lines of:&lt;/p&gt;


&lt;pre&gt;

    +1 tests pass
&lt;/pre&gt;

	&lt;p&gt;If you find that you&#8217;re the third person to verify a patch, then add the &lt;strong&gt;verified&lt;/strong&gt; keyword.  This signals to the core team that this patch is ready for examination.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Revert the patch&lt;/li&gt;

	&lt;/ul&gt;


Run something like
&lt;pre&gt;
    svn revert . -R
&lt;/pre&gt;

	&lt;p&gt;To ensure that the patch is no longer applied to your repository.&lt;/p&gt;
          </content>  </entry>
</feed>
