<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kerry Buckley &#187; Rails</title>
	<atom:link href="http://www.kerrybuckley.org/category/software/ruby/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerrybuckley.org</link>
	<description>What's the simplest thing that could possibly go wrong?</description>
	<lastBuildDate>Wed, 30 Nov 2011 11:26:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Memoised remote attribute readers for ActiveRecord</title>
		<link>http://www.kerrybuckley.org/2010/04/27/memoised-remote-attribute-readers-for-activerecord/</link>
		<comments>http://www.kerrybuckley.org/2010/04/27/memoised-remote-attribute-readers-for-activerecord/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 11:29:29 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.org/?p=377</guid>
		<description><![CDATA[I was recently working with an ActiveRecord class that exposed some attributes retrieved from a remote API, rather than from the database. The rules for handling the remote attributes were as follows: If the record is unsaved, return the local value of the attribute, even if it's nil. If the record is saved and we [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently working with an ActiveRecord class that exposed some attributes retrieved from a remote API, rather than from the database. The rules for handling the remote attributes were as follows:</p>
<ul>
<li>If the record is unsaved, return the local value of the attribute, even if it's nil.</li>
<li>If the record is saved and we don't have a local value, call the remote API and remember and return the value.</li>
<li>If the record is saved and we already have a local value, return that.</li>
</ul>
<p>Here's the original code (names changed to protect the innocent):</p>
<div class="igBar"><span id="lruby-8"><a href="#" onclick="javascript:showCodeTxt('ruby-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-8">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">class</span> MyModel &lt;ActiveRecord::Base</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; attr_writer :foo, :bar</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> foo</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>new_record? || @foo<span style="color:#006600; font-weight:bold;">&#41;</span> ? @foo : remote_object.<span style="color:#9900CC;">foo</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> bar</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>new_record? || @bar<span style="color:#006600; font-weight:bold;">&#41;</span> ? @bar : remote_object.<span style="color:#9900CC;">bar</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> remote_object</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; @remote_object ||= RemoteService.<span style="color:#9900CC;">remote_object</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The <code>remote_object</code> method makes a call to the remote service, and memoises the returned object (which contains all the attributes we are interested in).</p>
<p>I didn't really like the duplication in all these accessor methods &ndash; we had more than the two I've shown here &ndash; so decided to factor it out into a common <code>remote_attr_reader</code> class method. Originally I had the method take a block which returned the remote value, but that made the tests more complicated, so I ended up using convention over configuration and having the accessor for <em>foo</em> call a <em>remote_foo</em> method.</p>
<p>Here's the new code in the model:</p>
<div class="igBar"><span id="lruby-9"><a href="#" onclick="javascript:showCodeTxt('ruby-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-9">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">class</span> MyModel &lt;ActiveRecord::Base</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; remote_attr_reader :foo, :bar</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> remote_foo</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; remote_object.<span style="color:#9900CC;">foo</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> remote_bar</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; remote_object.<span style="color:#9900CC;">bar</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> remote_object</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; @remote_object ||= RemoteService.<span style="color:#9900CC;">remote_object</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Here's the <code>RemoteAttrReader</code> module that makes it possible:</p>
<div class="igBar"><span id="lruby-10"><a href="#" onclick="javascript:showCodeTxt('ruby-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-10">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">module</span> RemoteAttrReader</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> remote_attr_reader *names</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; names.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |name|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; attr_writer name</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; define_method name <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> new_record? || instance_variable_get<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">"@#{name}"</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instance_eval <span style="color:#996600;">"@#{name}"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instance_eval <span style="color:#996600;">"remote_#{name}"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>To make the module available to all models, I added an initialiser containing this line:</p>
<div class="igBar"><span id="lruby-11"><a href="#" onclick="javascript:showCodeTxt('ruby-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-11">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ActiveRecord::Base.<span style="color:#9900CC;">send</span> :extend, RemoteAttrReader </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Here's the spec for the module:</p>
<div class="igBar"><span id="lruby-12"><a href="#" onclick="javascript:showCodeTxt('ruby-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-12">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">require</span> File.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> + '/../spec_helper'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">class</span> RemoteAttrReaderTestClass</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; extend RemoteAttrReader</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; remote_attr_reader :foo</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> remote_foo</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#996600;">"remote value"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe RemoteAttrReader <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; let<span style="color:#006600; font-weight:bold;">&#40;</span>:model<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> RemoteAttrReaderTestClass.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"for an unsaved object"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; before <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; model.<span style="color:#9900CC;">stub</span><span style="color:#006600; font-weight:bold;">&#40;</span>:new_record?<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span> <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; describe <span style="color:#996600;">"When the attribute is not set"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">"returns nil"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color:#9900CC;">foo</span>.<span style="color:#9900CC;">should</span> be_nil</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; describe <span style="color:#996600;">"When the attribute is set"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; before <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color:#9900CC;">foo</span> = <span style="color:#996600;">"foo"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">"returns the attribute"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color:#9900CC;">foo</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">"foo"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"for a saved object"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; before <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; model.<span style="color:#9900CC;">stub</span><span style="color:#006600; font-weight:bold;">&#40;</span>:new_record?<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span> <span style="color:#0000FF; font-weight:bold;">false</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; describe <span style="color:#996600;">"When the attribute is set"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; before <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color:#9900CC;">foo</span> = <span style="color:#996600;">"foo"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">"returns the attribute"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color:#9900CC;">foo</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">"foo"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; describe <span style="color:#996600;">"When the attribute is not set"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">"returns the result of calling remote_&lt;attribute&gt;"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color:#9900CC;">foo</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">"remote value"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>To simplify testing of the model, I created a matcher, which I put into a file in <code>spec/support</code>:</p>
<div class="igBar"><span id="lruby-13"><a href="#" onclick="javascript:showCodeTxt('ruby-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-13">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">class</span> ExposeRemoteAttribute</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize attribute</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; @attribute = attribute</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> matches? model</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; @model = model</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">unless</span> model.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>@attribute<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; model.<span style="color:#9900CC;">send</span> <span style="color:#996600;">"#{@attribute}="</span>, <span style="color:#996600;">"foo"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">unless</span> model.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>@attribute<span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#996600;">"foo"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; model.<span style="color:#9900CC;">stub</span><span style="color:#006600; font-weight:bold;">&#40;</span>:new_record?<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span> <span style="color:#0000FF; font-weight:bold;">false</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">unless</span> model.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>@attribute<span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#996600;">"foo"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; model.<span style="color:#9900CC;">send</span> <span style="color:#996600;">"#{@attribute}="</span>, <span style="color:#0000FF; font-weight:bold;">nil</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; model.<span style="color:#9900CC;">stub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">"remote_#{@attribute}"</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span> <span style="color:#996600;">"bar"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; model.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>@attribute<span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#996600;">"bar"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> failure_message_for_should</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#996600;">"expected #{@model.class} to expose remote attribute #{@attribute}"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> failure_message_for_should_not</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#996600;">"expected #{@model.class} not to expose remote attribute #{@attribute}"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> description</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#996600;">"expose remote attribute #{@attribute}"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> expose_remote_attribute expected</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; ExposeRemoteAttribute.<span style="color:#9900CC;">new</span> expected</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Testing the model now becomes a simple case of testing the <em>remote_</em> methods in isolation, and using the matcher to test the behaviour of the <em>remote_attr_reader</em> call(s).</p>
<div class="igBar"><span id="lruby-14"><a href="#" onclick="javascript:showCodeTxt('ruby-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-14">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">require</span> File.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> + '/../spec_helper'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe MyModel <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; it <span style="color:#006600; font-weight:bold;">&#123;</span> should expose_remote_attribute<span style="color:#006600; font-weight:bold;">&#40;</span>:name<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; it <span style="color:#006600; font-weight:bold;">&#123;</span> should expose_remote_attribute<span style="color:#006600; font-weight:bold;">&#40;</span>:origin_server<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; it <span style="color:#006600; font-weight:bold;">&#123;</span> should expose_remote_attribute<span style="color:#006600; font-weight:bold;">&#40;</span>:delivery_domain<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; describe <span style="color:#996600;">"reading remote foo"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># test as a normal method</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Technorati Tags: <a href="http://technorati.com/tag/ruby" rel="tag">ruby</a>, <a href="http://technorati.com/tag/rails" rel="tag">rails</a>, <a href="http://technorati.com/tag/activerecord" rel="tag">activerecord</a>, <a href="http://technorati.com/tag/metaprogramming" rel="tag">metaprogramming</a>, <a href="http://technorati.com/tag/rspec" rel="tag">rspec</a>, <a href="http://technorati.com/tag/matcher" rel="tag">matcher</a>, <a href="http://technorati.com/tag/refactoring" rel="tag">refactoring</a>, <a href="http://technorati.com/tag/dry" rel="tag">dry</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2010/04/27/memoised-remote-attribute-readers-for-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Managing gems in a Rails project</title>
		<link>http://www.kerrybuckley.org/2009/11/02/managing-gems-in-a-rails-project/</link>
		<comments>http://www.kerrybuckley.org/2009/11/02/managing-gems-in-a-rails-project/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 17:24:10 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.org/?p=321</guid>
		<description><![CDATA[Over the years I've tried a number of approaches for managing gem dependencies in a Rails project. Here's a quick round-up of what I've tried, and the pros and cons of each. Just use what's on the system This is probably most people's default approach when first starting with Rails. Just sudo gem install whatever [...]]]></description>
			<content:encoded><![CDATA[<p>Over the years I've tried a number of approaches for managing gem dependencies in a Rails project. Here's a quick round-up of what I've tried, and the pros and cons of each.</p>
<h4>Just use what's on the system</h4>
<p>This is probably most people's default approach when first starting with Rails. Just <code>sudo gem install</code> whatever you need, require the appropriate gems (either in <code>environment.rb</code> or in the class that uses them), and you're away.</p>
<p>This mostly works OK for small projects where you're the only developer, but you still need to make sure the right gems are installed on the machine you're deploying the application to.</p>
<p>Worse, though, is what happens when you come back to the project after a while, various gems have been updated, and things mysteriously don't work any more. Not only do you have to mess around getting the code to work with the latest gem versions, but you probably don't even know exactly which versions it used to work with.</p>
<h4>Freeze (unpack) gems</h4>
<p>I think I first came across this technique in Err the Blog's <a href="http://errtheblog.com/posts/50-vendor-everything">Vendor Everything</a> post. The idea is to install copies of all your gems into the project's vendor/gems directory, meaning that wherever the code is running, you can guarantee that it has the correct versions of all its dependencies.</p>
<p>This <a href="http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies">got much easier in Rails 2.1</a>, which allowed you to specify all your gems using <code>config.gem</code> lines in <code>environment.rb</code> (you can also put gems only needed in specific environments in the appropriate file, eg you might only want to list things like rspec and cucumber in <code>config/enviroments/test.rb</code>). You can then run <code>sudo rake gems:install</code> to install any gems that aren't on your system, and <code>rake gems:unpack</code> to freeze them into <code>vendor/rails</code>, and be sure that wherever you check out or deploy the code, you'll be running the same versions of the gems. There's even a gems:build task to deal with gems that have native code (but more on that later).</p>
<p>Subsequent versions of Rails have improved on the original rake tasks &ndash; dependencies are now handled much better, for example &ndash; but there are still a few problems. The main one is the handling of gems that are required by rake tasks in your project, rather than just from your application code.</p>
<p>When you call a rake task in your Rails project, this is more-or-less what happens (I may have got some of the details slightly wrong):</p>
<ol>
<li>The top-level <code>Rakefile</code> is loaded.</li>
<li>This in turn requires <code>config/boot.rb</code>, but <em>not</em> <code>config/environment.rb</code>.</li>
<li>It then requires some standard rake stuff, and finally <code>tasks/rails</code> (which is part of Rails &ndash; specifically railties). This finds and requires all the <code>.rake</code> files in your plugins and your project's <code>lib/rake</code> directory.</li>
</ol>
<p>The problems start when you have a task depends on the rails <code>environment</code> task, and also requires a gem which is listed in <code>environment.rb</code>. Because the gem-loading magic only happens when the environment is loaded, the rake task will be blissfully unaware of your frozen gems, and will load them from the system instead.</p>
<p>If the system gem is newer than the frozen one, you get errors like this:</p>
<pre>
can't activate foo (= 1.2.3, runtime) for [], already activated foo-1.2.4 for []
</pre>
<p>If you work on two projects that use different versions of a gem like this, you end up having to uninstall and reinstall them as you switch from one to the other, which gets tedious fairly quickly.</p>
<h4>Specify gems, but don't freeze</h4>
<p>You can get round the wrong-version problem to some extent by specifying version numbers in <code>environment.yml</code> as '>=x.z.y' (or by not specifying them at all). If you're doing that, though, there's not really much benefit in unpacking the gems, and you may as well just use <code>rake gems:install</code> to make sure they're on the system. Of course the downside of this approach is that you can't be sure that everyone's running the exact same versions of the gems. Worse still, you can't be sure that what's on your production box matches your development and test environments.</p>
<h4>GemInstaller</h4>
<p><a href="http://geminstaller.rubyforge.org/">GemInstaller</a> solves most of the problems with the built-in Rails gem management by running as a <a href="http://ryandaigle.com/articles/2007/11/18/what-s-new-in-edge-rails-pre-environment-load-hook">preinitializer</a>, meaning it gets loaded before the other <code>boot.rb</code> gubbins.</p>
<p>GemInstaller uses the gems installed on the system rather than freezing them into the project, but because it gets to run first it ensures that the correct versions are used, even if there are newer versions installed. By default it checks your project's gem list and installs anything that's missing every time it runs (which is whenever you start a server, run the console, execute a rake task etc). You create a YAML file listing the gems you need (dependencies are handled automatically), and other options such as an HTTP proxy if necessary.</p>
<p>Of course on Unix-like systems, which is most of them (although I hear there are still people developing Rails projects on Windows), gems are generally installed as root. GemInstaller can get round this in two ways &ndash; either by setting the <code>--sudo</code> option and setting a rule in <code>/etc/sudoers</code> to allow the appropriate user(s) to run the gem commands as root without having to provide a password, or by using the built-in gem behaviour that falls back to installing in <code>~/.gem</code>.</p>
<p>Personally I like to keep all my gems in one place, accessible to any user, so I went for the sudo approach. The only problem with this is that it uses sudo for all gem commands, rather than just install or update, which means it runs a <code>sudo gem list</code> every time your app starts up. Depending on the way you have Apache and Passenger set up this may mean granting sudo access to what should be a low-privileged user.</p>
<p>I ended up disabling the automatic updating of gems, and just warning when they're missing instead. In fact later versions of GemInstaller don't try to handle the update automatically anyway.</p>
<p>I created a separate script to do the update, which can be run manually, on a post-merge git hook, or as part of the Capistrano deployment task.</p>
<p>Because GemInstaller needs to go out to the network to fetch any new or updated gems, things get a bit more painful (as always) if you are unfortunate enough to be stuck behind a corporate HTTP proxy. Actually it's easy enough to configure if you're <em>always</em> behind a proxy, but it gets slightly trickier if your web access is sometimes proxied and sometimes direct. Nothing that can't be solved of course.</p>
<p>Unfortunately you can still end up with version conflicts if a gem is required by one you have specified, then you explicitly require an older version, but these can usually be resolved by shuffling the order of the gems in <code>geminstaller.yml</code>.</p>
<h4>Bundler</h4>
<p><a href="http://github.com/wycats/bundler">Bundler</a> is the newest kid on the gem management block, and looks to have solved pretty much all the problems faced by the other approaches. It's based on the gem management approach from Merb, and can be used in any Ruby project (not just Rails).</p>
<p>Bundler works by unpacking gems into the project (I recommend using a directory other than the default <code>vendor/gems</code> to avoid confusing Rails &ndash; this can be configured by setting <code>bundle_path</code> and <code>bin_path</code> in the Gemfile), but the intention is that you only commit the <code>.gem</code> files in the cache directory to source control. Gems are then installed locally within the project, including any platform-specific native code as well as the commands in <code>bin</code>.</p>
<p>Because Bundler resolves all dependencies up-front, you only need to specify the gems you're using explicitly, and let it handle the rest, which hopefully means an end to version conflicts at last.</p>
<p>Here's an example Gemfile:</p>
<div class="igBar"><span id="lruby-17"><a href="#" onclick="javascript:showCodeTxt('ruby-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-17">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">source 'http://gemcutter.<span style="color:#9900CC;">org</span>'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">source 'http://gems.<span style="color:#9900CC;">github</span>.<span style="color:#9900CC;">com</span>'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bundle_path 'vendor/bundled_gems'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bin_path 'vendor/bundled_gems/bin'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gem 'rails', '<span style="color:#006666;color:#800000;">2</span>.<span style="color:#006666;color:#800000;">3</span>.<span style="color:#006666;color:#800000;">4</span>'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gem 'bundler', '<span style="color:#006666;color:#800000;">0</span>.<span style="color:#006666;color:#800000;">6</span>.<span style="color:#006666;color:#800000;">0</span>'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gem 'capistrano', '<span style="color:#006666;color:#800000;">2</span>.<span style="color:#006666;color:#800000;">5</span>.<span style="color:#006666;color:#800000;">8</span>'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gem 'capistrano-ext', '<span style="color:#006666;color:#800000;">1</span>.<span style="color:#006666;color:#800000;">2</span>.<span style="color:#006666;color:#800000;">1</span>'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gem 'cucumber', '<span style="color:#006666;color:#800000;">0</span>.<span style="color:#006666;color:#800000;">4</span>.<span style="color:#006666;color:#800000;">3</span>', :except =&gt; :production</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># [more gems here]</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">disable_system_gems </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Note the two additional sources (rubyforge.org is configured by default), the path overrides, and the last line, which removes the system gems from the paths, avoiding any potential confusion.</p>
<p>I've put this in <code>config/preinitializer.rb</code> to update from the cached gems on startup (this doesn't hit the network):</p>
<div class="igBar"><span id="lruby-18"><a href="#" onclick="javascript:showCodeTxt('ruby-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-18">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$stderr.<span style="color:#CC0066; font-weight:bold;">puts</span> 'Updating bundled gems...'</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">system</span> 'gem bundle --cached'</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">"#{RAILS_ROOT}/vendor/bundled_gems/environment"</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>To avoid any startup delays after an upgrade, I also call <code>system 'gem bundle --cached'</code> from the <code>after_update_code</code> hook in the capfile.</p>
<p>Finally, to make sure only the .gem files are checked in, add these lines to <code>.gitignore</code> (you'll still need to explicitly <code>git add</code> the <code>bundled_gems/cache</code> directory):</p>
<pre>
vendor/bundled_gems
!vendor/bundled_gems/cache
</pre>
<p>[Update 3 November] Yehuda Katz just <a href="http://yehudakatz.com/2009/11/03/using-the-new-gem-bundler-today/">posted an article all about Bundler</a>, including features coming in the imminent 0.7 release.</p>
<p>Technorati Tags: <a href="http://technorati.com/tag/ruby" rel="tag">ruby</a>, <a href="http://technorati.com/tag/rails" rel="tag">rails</a>, <a href="http://technorati.com/tag/gems" rel="tag">gems</a>, <a href="http://technorati.com/tag/rubygems" rel="tag">rubygems</a>, <a href="http://technorati.com/tag/geminstaller" rel="tag">geminstaller</a>, <a href="http://technorati.com/tag/bundler" rel="tag">bundler</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2009/11/02/managing-gems-in-a-rails-project/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My (very!) small part in the Array#forty_two controversy</title>
		<link>http://www.kerrybuckley.org/2008/11/24/my-very-small-part-in-the-arrayforty_two-controversy/</link>
		<comments>http://www.kerrybuckley.org/2008/11/24/my-very-small-part-in-the-arrayforty_two-controversy/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 12:52:11 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[General nonsense]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.org/2008/11/24/my-very-small-part-in-the-arrayforty_two-controversy/</guid>
		<description><![CDATA[For those outside the Rails community who have no idea what I'm on about, some people got a bit upset about Rails 2.2 defining Array#second up to Array#tenth, so for example you can call foo.fifth instead of foo[4] (you can already call foo.first instead of foo[0]). One of the last changes to be committed before [...]]]></description>
			<content:encoded><![CDATA[<p>For those outside the Rails community who have no idea what I'm on about, some people got a bit upset about Rails 2.2 defining <code>Array#second</code> up to <code>Array#tenth</code>, so for example you can call <code>foo.fifth</code> instead of <code>foo[4]</code> (you can already call <code>foo.first</code> instead of <code>foo[0]</code>). One of the last changes to be committed before 2.2 was released was to slim the list down to just second, third, fourth and fifth, but <a href="http://github.com/rails/rails/commit/e50530ca3ab5db53ebc74314c54b62b91b932389">adding <code>Array#forty_two</code></a> (<a href="http://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Everything#Answer_to_Life.2C_the_Universe_and_Everything_.2842.29">the ultimate answer</a>) instead.</p>
<p><img src='http://www.kerrybuckley.org/wp-content/uploads/2008/11/dhh_tweet.png' alt='dhh_tweet.png' /></p>
<p><img src='http://www.kerrybuckley.org/wp-content/uploads/2008/11/commit_1.png' alt='commit_1.png' /></p>
<p><img src='http://www.kerrybuckley.org/wp-content/uploads/2008/11/kerry_reply.png' alt='kerry_reply.png' /></p>
<p><img src='http://www.kerrybuckley.org/wp-content/uploads/2008/11/dhh_reply.png' alt='dhh_reply.png' /></p>
<p><img src='http://www.kerrybuckley.org/wp-content/uploads/2008/11/commit_2.png' alt='commit_2.png' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2008/11/24/my-very-small-part-in-the-arrayforty_two-controversy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails 2.2 Envycast Review</title>
		<link>http://www.kerrybuckley.org/2008/11/08/rails-22-envycast-review/</link>
		<comments>http://www.kerrybuckley.org/2008/11/08/rails-22-envycast-review/#comments</comments>
		<pubDate>Sat, 08 Nov 2008 21:04:43 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.org/2008/11/08/rails-22-envycast-review/</guid>
		<description><![CDATA[I've been a fan of the RailsEnvy guys (Gregg Pollack and Jason Seifer) ever since their "Hi, I'm Ruby on Rails" spoof of the "I'm a Mac" ads, and have been listening to their podcast ever since. I even got a mention on it once. Well that's not strictly true &#8211; I wasn't actually mentioned, [...]]]></description>
			<content:encoded><![CDATA[<p>I've been a fan of the <a href="http://www.railsenvy.com/">RailsEnvy</a> guys (Gregg Pollack and Jason Seifer) ever since their "<a href="http://www.railsenvy.com/2007/5/14/ruby-on-rails-commercial">Hi, I'm Ruby on Rails</a>" spoof of the "I'm a Mac" ads, and have been listening to their <a href="http://www.railsenvy.com/podcast">podcast</a> ever since. I even got a mention on it once. Well that's not strictly true &ndash; I wasn't <em>actually</em> mentioned, but a <a href="http://capistrano.lighthouseapp.com/projects/8716/tickets/18-add-support-chain-of-gateways">patch</a> I'd contributed to Capistrano was, which is close enough.</p>
<p>Recently, Gregg and Jason have branched out into screencasts, but I hadn't actually watched one because (understandably) they charge for them, and I was too tight to cough up the cash. &pound;1200 for a new MacBook, no problem. A fiver for a screencast? What am I, made of money?</p>
<p>Anyhow, when I <a href="http://twitter.com/greggpollack/status/981073984">saw</a> that they were looking for people to review their <a href="http://envycasts.com/products/ruby-on-rails-22-screencast" title="Ruby on Rails 2.2 Envycast">Ruby on Rails Envycast</a>, covering the latest goodness in Rails 2.2, I jumped at the chance to get a free copy. A wonderful example of cognitive bias, given that I wouldn't have agreed to write a review just to be paid $16.</p>
<h4>What do I get for the money?</h4>
<p>The basic $9 gets you the screencast and a set of code samples to go with it, or for $16 they'll throw in Carlos Brando's <em>Ruby on Rails 2.2</em> PDF too. Alternatively the PDF is available on its own, also for $9.</p>
<h4>Video</h4>
<p>The video is available in Quicktime or Ogg formats at a resolution of 569&times;480, as well as a version optimised for iPhones and iPods. Total running time is just under 45 minutes, and incredibly the first 39&frac12; of those go by before Jason makes any claims about Rails's scalability.</p>
<p>I don't know whether it's unique, but the Envycast style of having the presenters chroma-keyed onto the Keynote presentation generally works very well. The visuals themselves are professional, although sometimes the 'sparkle' effect is a bit overused for my taste. The presentation style is much as you'd expect if you've listened to the podcasts, with plenty of cheesy humour to keep things interesting. I think having two people present in a conversational style is a big help.</p>
<p>The screencast is split into sections, each covering the new features for a different component (ActiveRecord, ActiveSupport, ActionPack, ActionController, Railties, internationalization and performance). The Quicktime version (not sure about the others) has bookmarks, making it easy to jump to a particular section. The whole thing is set against a variety of city skylines to liven the background up a little &ndash; by the way guys, that's <a href="http://en.wikipedia.org/wiki/Tower_bridge">Tower Bridge</a>, not <a href="http://en.wikipedia.org/wiki/London_bridge">London Bridge</a>.</p>
<p>Each new feature is introduced with an example, generally contrasting the 'old' way of doing something with the equivalent in 2.2. There's enough detail to get the idea of what's changed, without dwelling too long on each one. One tiny gripe with the code snippets on screen: the pedant in me hates seeing curly quotes in code, because I know if I typed <code>puts &lsquo;foo&rsquo;</code> into irb instead of <code>puts &apos;foo&apos;</code>, it wouldn't work.</p>
<h4>Code samples</h4>
<p>The screencast comes with a set of code samples to illustrate all the features discussed in the screencast. These take the form of sample classes with Test::Unit test cases, along with rakefiles to run them. The sample directory contains a frozen installation of Rails 2.2, so all you need to do to run them is add the appropriate values to database.yml. I had trouble running them initially because they were inside a directory with a space in its name, but other than that it all worked nicely.</p>
<h4>PDF</h4>
<p>The PDF that comes with the $16 bundle is by Carlos Brando, well-known for his free Rails 2.1 book. It's available in the original Portugese, or translated to English by Carl Youngblood. The book weighs in at 118 pages, and as you would expect goes into more detail than the screencast. It claims to cover all the major changes in Rails 2.2 (I haven't checked!), and contains clear descriptions with examples.</p>
<h4>Conclusion</h4>
<p>So is it worth it? On balance, I think the answer is yes, although I wonder whether they'd sell more at $5 rather than $9 &ndash; after all, I can buy (to pick an example at random) the entire Naked Gun trilogy on DVD for roughly the same amount, and Gregg and Jason aren't <em>that</em> funny. The value is in collecting all the information in one place &ndash; you <em>could</em> trawl through the release notes and lighthouse tickets to get all the same information, but if you value your time at all, the screencast and PDF pay for themselves many times over.</p>
<p>Should you buy the PDF, the video or both? If you just want the hard facts, go for the PDF, but if you want to be entertained too (assuming you find the Rails Envy podcasts entertaining), get the video as well. The next episode, <a href="http://envycasts.com/products/scaling-ruby">Scaling Ruby</a>, is out now, and I might buy it just to see if Jason finally admits that Rails might actually be able to scale.</p>
<p>Technorati Tags: <a href="http://technorati.com/tag/railsenvy+screencast+rails2.2" rel="tag">railsenvy screencast rails2.2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2008/11/08/rails-22-envycast-review/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Defending Ruby and Rails in the Enterprise</title>
		<link>http://www.kerrybuckley.org/2008/06/12/defending-ruby-and-rails-in-the-enterprise/</link>
		<comments>http://www.kerrybuckley.org/2008/06/12/defending-ruby-and-rails-in-the-enterprise/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 09:42:52 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Enterprise]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.org/2008/06/12/defending-ruby-and-rails-in-the-enterprise/</guid>
		<description><![CDATA[I consider myself fortunate that the previous two projects I worked on (the BT Web21C Portal and Mojo) were Rails-based (actually it wan't just luck in the former case, as I had a part in selecting the framework). I love the expressiveness and flexibility of the Ruby language, the power and relative simplicity of the [...]]]></description>
			<content:encoded><![CDATA[<p>I consider myself fortunate that the previous two projects I worked on (the <a href="http://web21c.bt.com/">BT Web21C Portal</a> and <a href="http://mojo.bt.com/">Mojo</a>) were Rails-based (actually it wan't just luck in the former case, as I had a part in selecting the framework). I love the expressiveness and flexibility of the Ruby language, the power and relative simplicity of the Rails framework, and the all-round awesomeness of tools like <a href="http://rspec.info/">RSpec</a> and <a href="http://capify.org/">Capistrano</a>, and I don't particularly relish the thought of going back to Java (although I'm told that <a href="http://springframework.org/">Spring</a> is much nicer than last time I used it).</p>
<p>At our recent release planning session, I was assigned to a new project, which involves (among other things) exposing a CLI-based configuration interface as a web service. In our initial discussions, <a href="http://twitter.com/drpep">the</a> <a href="http://twitter.com/ddunwoody">four</a> <a href="http://twitter.com/moserp">of</a> <a href="http://twitter.com/kerryb">us</a> more-or-less agreed on a few initial decisions:</p>
<ul>
<li>The exposure should be <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST</a>. Fortunately the people developing the upstream system shared this opinion.</li>
<li>Rails is an ideal framework for RESTful web services.</li>
<li>Ruby seemed like a good fit for parsing the command responses too.</li>
<li>Asynchronous behaviour would be handled using queues (probably <a href="http://activemq.apache.org/">ActiveMQ</a>).</li>
</ul>
<p>Since we'd all been working on Rails projects when the new team was formed, we assumed that this wouldn't be a particularly contentious route to go down, but unfortunately our director/architect/boss didn't see things quite the same way. He had two main objections:</p>
<ul>
<li>Rails may make sense for GUI applications, but why on earth would you use it for a service? All our other [SOAP] services are written in Java.</li>
<li>At some point the application will need to go into support, and we don't have support/operations people with Ruby or Rails experience</li>
</ul>
<p>I think the first point's easier to address, as I'd argue it's based on a misunderstanding: Rails isn't really anything to do with GUIs, but is a framework for creating MVC web applications. Virtually all the heavy lifting Rails takes care of is in the controller and model areas, with the creation of the actual visible GUI being left to the developer to take care of with the usual mix of HTML, CSS and Javascript. The only thing Rails adds is the ability to insert dynamic content using <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html">ERB</a> &ndash; similar to the role of JSP in Java EE.</p>
<p>A RESTful web service is, to all intents and purposes, the same as a normal web application, but (potentially) without the HTML. All the power that Rails brings to web application development is also harnessed when creating RESTful services.</p>
<p>The second point represents a much more fundamental strategy choice. If the company makes the decision that all development is going to use Java (the language as well as the platform), then we inevitably lose the flexibility to choose what may appear (in a local context) to be the right tool for the job. Personally I think that would be a shortsighted and ill-informed decision: if that were the strategy, we'd presumably all still be developing in C, or COBOL, or Assembler. Or we'd have gone bust. But then I'm not an architect (incidentally, according to Peter Gillard-Moss, that's <a href="http://jupitermoonbeam.blogspot.com/2008/06/10-things-that-should-get-devs-fired.html">reason number 10</a> why I don't deserve to be fired), so what do I know?</p>
<p>However, if Ruby <em>is</em> considered an acceptable technology choice for "normal" web applications, we'll still need people with appropriate skills to support those, so the problem doesn't go away. I suspect even for a Java specialist, supporting a well-written Rails application with good test coverage is probably easier than supporting some of the spaghetti-coded Java I've seen.</p>
<p>Anyway, our arguments obviously weren't totally unconvincing, because we were given a couple of weeks to show what we could produce before getting a final decision. That time runs out on Monday, so if I'm unnaturally grumpy after that it'll be because we've been told to chuck all our work so far away and start from scratch in Java. Or possibly FORTRAN.</p>
<p><strong>Update, 16 June</strong> Well we made our case, and we get to stick with Rails. Celebration all round!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2008/06/12/defending-ruby-and-rails-in-the-enterprise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;You have to declare the controller name in controller specs&#8221;</title>
		<link>http://www.kerrybuckley.org/2007/12/18/you-have-to-declare-the-controller-name-in-controller-specs/</link>
		<comments>http://www.kerrybuckley.org/2007/12/18/you-have-to-declare-the-controller-name-in-controller-specs/#comments</comments>
		<pubDate>Tue, 18 Dec 2007 21:35:29 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/12/18/you-have-to-declare-the-controller-name-in-controller-specs/</guid>
		<description><![CDATA[For ages I've been getting an intermittent problem with RSpec, where occasionally I'd see the following error on a model spec: You have to declare the controller name in controller specs. For example: describe "The ExampleController" do controller_name "example" #invokes the ExampleController end The problem seemed to depend on which order the specs were run [...]]]></description>
			<content:encoded><![CDATA[<p>For ages I've been getting an intermittent problem with <a href="http://rspec.info/">RSpec</a>, where occasionally I'd see the following error on a <strong>model</strong> spec:</p>
<pre>
You have to declare the controller name in controller specs. For example:
describe "The ExampleController" do
controller_name "example" #invokes the ExampleController
end
</pre>
<p>The problem seemed to depend on which order the specs were run in, and for rake it could be avoided by removing <code>--loadby mtime --reverse</code> from <em>spec.opts</em>. It was a real pain with autotest though, and today (my original plan of "wait for RSpec 1.1 and hope it goes away" having failed) I finally got round to looking into it properly.</p>
<p>It seemed that the error was being triggered by the rather unpleasant <a href="/2007/03/11/drying-out-model-specs/">code I wrote a while ago to simplify testing of model validation</a>. Digging into the RSpec source to see what was happening, I found that that error message only gets returned when (as you'd expect) you don't declare the controller name in a controller spec (specifically in an instance of <code>Spec::Rails::Example::ControllerExampleGroup</code>). The code that decides what type of example group to create lives in <code>Spec::DSL::BehaviourFactory</code>, and according to <em>its</em> specs, there are two methods it uses to figure out what type of spec it's looking at:</p>
<div class="igBar"><span id="lruby-22"><a href="#" onclick="javascript:showCodeTxt('ruby-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-22">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">it <span style="color:#996600;">"should return a ModelExampleGroup when given :type =&gt; :model"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">...</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9900CC;">it</span> <span style="color:#996600;">"should return a ModelExampleGroup when given :spec_path =&gt; '/blah/spec/models/'"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">...</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9900CC;">it</span> <span style="color:#996600;">"should return a ModelExampleGroup when given :spec_path =&gt; '<span style="color:#000099;">\\</span>blah<span style="color:#000099;">\\</span>spec<span style="color:#000099;">\\</span>models<span style="color:#000099;">\\</span>' (windows format)"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">...</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9900CC;">it</span> <span style="color:#996600;">"should favor the :type over the :spec_path"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">... </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I began to suspect that the problem was caused by the fact that my specify_attributes method wasn't declared in a file in spec/models, so I thought I'd try specifying the type explicitly. So instead of this:</p>
<div class="igBar"><span id="lruby-23"><a href="#" onclick="javascript:showCodeTxt('ruby-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-23">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"#{label} with all attributes set"</span> <span style="color:#9966CC; font-weight:bold;">do</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I changed it to this:</p>
<div class="igBar"><span id="lruby-24"><a href="#" onclick="javascript:showCodeTxt('ruby-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-24">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"#{label} with all attributes set"</span>, :type =&gt; 'model' <span style="color:#9966CC; font-weight:bold;">do</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Sure enough, it worked! Not sure whether anyone else is likely to see the same problem (unless they're foolish enough to use my validation spec code), but hopefully if you do, a Google search will bring up this post and it might point you in the right direction.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/12/18/you-have-to-declare-the-controller-name-in-controller-specs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails Envy&#8217;s take on the werewolf question</title>
		<link>http://www.kerrybuckley.org/2007/11/26/rails-envys-take-on-the-werewolf-question/</link>
		<comments>http://www.kerrybuckley.org/2007/11/26/rails-envys-take-on-the-werewolf-question/#comments</comments>
		<pubDate>Mon, 26 Nov 2007 10:18:24 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[General nonsense]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/11/26/rails-envys-take-on-the-werewolf-question/</guid>
		<description><![CDATA[This clip [MP3, 57s] from a Rails Envy podcast made me laugh. It's referring to Charles Nutter's recent musings on whether werewolf is killing the conference hackfest. Incidentally, how often do you get the chance to Google for "nutter werewolf"? Technorati Tags: rails envy, werewolf]]></description>
			<content:encoded><![CDATA[<p>This <a href='http://blog.janeandkerry.com/wp-content/uploads/2007/11/rails-envy-werewolf.mp3' title='Rails Envy clip'>clip</a> [MP3, 57s] from a <a href="http://www.railsenvy.com/">Rails Envy</a> podcast made me laugh. It's referring to Charles Nutter's recent musings on <a href="http://headius.blogspot.com/2007/11/is-werewolf-killing-conference-hackfest.html">whether werewolf is killing the conference hackfest</a>.</p>
<p>Incidentally, how often do you get the chance to Google for "nutter werewolf"?</p>
<p>Technorati Tags: <a href="http://technorati.com/tag/rails+envy" rel="tag">rails envy</a>, <a href="http://technorati.com/tag/werewolf" rel="tag"> werewolf</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/11/26/rails-envys-take-on-the-werewolf-question/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://blog.janeandkerry.com/wp-content/uploads/2007/11/rails-envy-werewolf.mp3" length="1152036" type="audio/mpeg" />
		</item>
		<item>
		<title>Weird Rails bug</title>
		<link>http://www.kerrybuckley.org/2007/11/14/weird-rails-bug/</link>
		<comments>http://www.kerrybuckley.org/2007/11/14/weird-rails-bug/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 09:21:16 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/11/14/weird-rails-bug/</guid>
		<description><![CDATA[I Spent some time yesterday tracking down a bizarre bug which was causing some of our Selenium tests to fail. Watching the browser running the tests, I could see that occasionally a page would fail to render, with an "invalid argument" error and a stack trace. The line in question was an &#60;%= end_form_tag %&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>I Spent some time yesterday tracking down a bizarre bug which was causing some of our Selenium tests to fail. Watching the browser running the tests, I could see that occasionally a page would fail to render, with an "invalid argument" error and a stack trace. The line in question was an <code>&lt;%= end_form_tag %&gt;</code> in a layout. The strange thing was, it didn't display the same behaviour when I ran the single failing test on its own, or when I viewed the page myself.</p>
<p>Or at least, I thought it didn't. Because it seemed intermittent, I tried reloading the page a few times, and sure enough, the error appeared. Once. Then the page reloaded successfully six times, before failing again. This was completely repeatable &ndash; six times OK; one stack trace. Regular as clockwork.</p>
<p>Completely stumped, I thought I might as well at least replace the deprecated <code>&lt;%= start_form_tag %&gt; &hellip; &lt;%= end_form_tag %&gt;</code> with <code>&lt;% form_tag do %&gt; &hellip; &lt;% end %&gt;</code>, and lo and behold, that fixed it.</p>
<p>Unfortunately, I have no idea why. An imaginary prize to whoever can explain it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/11/14/weird-rails-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails, SOAP and REST</title>
		<link>http://www.kerrybuckley.org/2007/10/05/rails-soap-and-rest/</link>
		<comments>http://www.kerrybuckley.org/2007/10/05/rails-soap-and-rest/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 08:24:12 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/10/05/rails-soap-and-rest/</guid>
		<description><![CDATA[From the list of new features coming in Rails 2.0: It’ll probably come as no surprise that Rails has picked a side in the SOAP vs REST debate. Unless you absolutely have to use SOAP for integration purposes, we strongly discourage you from doing so.]]></description>
			<content:encoded><![CDATA[<p>From the <a href="http://weblog.rubyonrails.com/2007/9/30/rails-2-0-0-preview-release">list of new features coming in Rails 2.0</a>:</p>
<blockquote><p>It’ll probably come as no surprise that Rails has picked a side in the SOAP vs REST debate. Unless you absolutely have to use SOAP for integration purposes, we strongly discourage you from doing so.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/10/05/rails-soap-and-rest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Correct use of the flash in Rails</title>
		<link>http://www.kerrybuckley.org/2007/07/04/correct-use-of-the-flash-in-rails/</link>
		<comments>http://www.kerrybuckley.org/2007/07/04/correct-use-of-the-flash-in-rails/#comments</comments>
		<pubDate>Wed, 04 Jul 2007 14:37:13 +0000</pubDate>
		<dc:creator>Kerry</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.kerrybuckley.com/2007/07/04/correct-use-of-the-flash-in-rails/</guid>
		<description><![CDATA[[Update 20 April 2010] I recently had problems testing flash.now, and Google kept leading me back to this post. Unfortunately it doesn't seem to work with the current version of Rails (I'm using 2.3.5 at the moment). This post from Pluit Solutions gives an alternative approach which seems to work. I haven't tried it with [...]]]></description>
			<content:encoded><![CDATA[<p>[Update 20 April 2010]</p>
<p>I recently had problems testing <code>flash.now</code>, and Google kept leading me back to this post. Unfortunately it doesn't seem to work with the current version of Rails (I'm using 2.3.5 at the moment).</p>
<p><a href="http://www.pluitsolutions.com/2008/01/22/testing-flashnow-in-rails/">This post</a> from Pluit Solutions gives an alternative approach which seems to work. I haven't tried it with Rails 3 though.</p>
<hr />
<p>I don't know whether this has caught anyone else out, or whether we just didn't read the documentation properly (it's covered briefly on p153 of <a href="http://www.pragmaticprogrammer.com/titles/rails2/index.html">AWDwR</a>), but I thought I'd mention it anyway.</p>
<p>Anyone who's written a Rails app will know that the 'flash' is used to store error and status messages, usually on form submissions. Model validation failure messages automatically get copied into the flash, but you often want to do it manually too.</p>
<div class="igBar"><span id="lruby-30"><a href="#" onclick="javascript:showCodeTxt('ruby-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-30">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">flash<span style="color:#006600; font-weight:bold;">&#91;</span>:notice<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">"User Details updated."</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">redirect_to edit_user_path<span style="color:#006600; font-weight:bold;">&#40;</span>@user<span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The gotcha comes when you want to display a message and render a page, as opposed to redirecting &ndash; for example when errors are preventing a form from being submitted. This is how not to do it:</p>
<div class="igBar"><span id="lruby-31"><a href="#" onclick="javascript:showCodeTxt('ruby-31'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-31">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">flash<span style="color:#006600; font-weight:bold;">&#91;</span>:error<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">"Password doesn't match confirmation."</span> <span style="color:#008000; font-style:italic;"># WRONG!</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">render :action =&gt; 'change_password' </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The problem is that the flash is stored for the <em>next</em> request. Because we're no longer doing a redirect, that means the message may appear wherever the user goes next, not just on the page that we just rendered. To avoid this, use <code>flash.now</code>, which is only used for the current request:</p>
<div class="igBar"><span id="lruby-32"><a href="#" onclick="javascript:showCodeTxt('ruby-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-32">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">flash.<span style="color:#9900CC;">now</span><span style="color:#006600; font-weight:bold;">&#91;</span>:error<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">"Password doesn't match confirmation."</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">render :action =&gt; 'change_password' </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The rule of thumb is to use <code>flash</code> if you're redirecting, and <code>flash.now</code> if you're rendering (either explicitly, or by dropping through to the default view for the action).</p>
<p>All very well, but whatever you put in <code>flash.now</code> is cleared out at the end of the request, so how do you test it? The answer (for RSpec, at least) lies in a comment on this <a href="http://rubyforge.org/tracker/?func=detail&#038;atid=3152&#038;aid=11834&#038;group_id=797">RSpec feature request</a> &ndash; basically just add the following to <code>spec_helper.rb</code>:</p>
<div class="igBar"><span id="lruby-33"><a href="#" onclick="javascript:showCodeTxt('ruby-33'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-33">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">module</span> ActionController</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">module</span> Flash</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">class</span> FlashHash </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; @hash = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; @now_hash = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>key<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; @hash<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#40;</span>key, obj<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; @hash<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span> = obj</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> discard<span style="color:#006600; font-weight:bold;">&#40;</span>k = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; initialize</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> now</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; @now_hash</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> update<span style="color:#006600; font-weight:bold;">&#40;</span>hash<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; @hash.<span style="color:#9900CC;">update</span><span style="color:#006600; font-weight:bold;">&#40;</span>hash<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> sweep</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># do nothing</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>You can now do something like this:</p>
<div class="igBar"><span id="lruby-34"><a href="#" onclick="javascript:showCodeTxt('ruby-34'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-34">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">describe <span style="color:#996600;">"When a user tries to change his password with an invalid verification code"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; ...</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9900CC;">it</span> <span style="color:#996600;">"should put an error message in the flash"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; flash.<span style="color:#9900CC;">now</span><span style="color:#006600; font-weight:bold;">&#91;</span>:error<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">"Incorrect verification code or password."</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; it <span style="color:#996600;">"should not persist the flash"</span> <span style="color:#9966CC; font-weight:bold;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; flash<span style="color:#006600; font-weight:bold;">&#91;</span>:error<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">should</span> be_nil</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Technorati Tags: <a href="http://technorati.com/tag/Ruby" rel="tag">Ruby</a>, <a href="http://technorati.com/tag/Rails" rel="tag">Rails</a>, <a href="http://technorati.com/tag/flash" rel="tag">flash</a>, <a href="http://technorati.com/tag/RSpec" rel="tag">RSpec</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrybuckley.org/2007/07/04/correct-use-of-the-flash-in-rails/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.083 seconds -->

