<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>webcrisps - webby snippets found by Max Williams</title>
	<atom:link href="http://webcrisps.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://webcrisps.wordpress.com</link>
	<description>Occasional observations of a Ruby On Rails developer living in Brighton, UK</description>
	<lastBuildDate>Thu, 14 Jul 2011 09:10:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='webcrisps.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>webcrisps - webby snippets found by Max Williams</title>
		<link>http://webcrisps.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://webcrisps.wordpress.com/osd.xml" title="webcrisps - webby snippets found by Max Williams" />
	<atom:link rel='hub' href='http://webcrisps.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Dynamically add class methods in Rails</title>
		<link>http://webcrisps.wordpress.com/2010/01/07/dynamically-add-class-methods-in-rails/</link>
		<comments>http://webcrisps.wordpress.com/2010/01/07/dynamically-add-class-methods-in-rails/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 17:59:20 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webcrisps.wordpress.com/?p=113</guid>
		<description><![CDATA[I have a Product class which has a small number of records in the db, each of which has a name, eg &#8220;millionaire&#8221;.  Rather than keep typing Product.find_by_name(&#8220;millionaire&#8221;) i want to type Product.millionaire. I could use method_missing for this but &#8230; <a href="http://webcrisps.wordpress.com/2010/01/07/dynamically-add-class-methods-in-rails/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=113&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have a Product class which has a small number of records in the db, each of which has a name, eg &#8220;millionaire&#8221;.  Rather than keep typing Product.find_by_name(&#8220;millionaire&#8221;) i want to type Product.millionaire.</p>
<p>I could use method_missing for this but that means every time Product gets a method it doesn&#8217;t recognise it loads all the products out of the db &#8211; not very good.  Instead, let&#8217;s use define_method.  Normally define_method adds instance methods to a class but we can add them at the class level like so.  In the product class:</p>
<p><pre class="brush: ruby;">
class &lt;&lt; self
  Product.find(:all, :select =&gt; &quot;name&quot;).collect(&amp;:name).each do |name|
    define_method name.to_sym do
      self.find_by_name(name)
    end
  end
end</pre></p>
<p>Now, when the classes are loaded, which happens only at restart in production mode, the methods are created.  No method_missing required and no db access besides the single access at class load.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/113/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=113&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2010/01/07/dynamically-add-class-methods-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
		<item>
		<title>Gem magic &#8211; copying gems from a server to your local repository</title>
		<link>http://webcrisps.wordpress.com/2009/11/11/gem-magic-copying-gems-from-a-server-to-your-local-repository/</link>
		<comments>http://webcrisps.wordpress.com/2009/11/11/gem-magic-copying-gems-from-a-server-to-your-local-repository/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 19:34:57 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webcrisps.wordpress.com/?p=110</guid>
		<description><![CDATA[I recently upgraded to Ubuntu 9.10, wiping everything, formatting my partition and starting again.  There was the usual sunday full of software installing hassles.  But, i discovered one thing that made my life a ton easier, thanks to Marnen Laibow-Koser &#8230; <a href="http://webcrisps.wordpress.com/2009/11/11/gem-magic-copying-gems-from-a-server-to-your-local-repository/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=110&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently upgraded to Ubuntu 9.10, wiping everything, formatting my partition and starting again.  There was the usual sunday full of software installing hassles.  But, i discovered one thing that made my life a ton easier, thanks to <a href="http://www.marnen.org">Marnen Laibow-Koser</a> on http://ruby-forum.com</p>
<p>My situation (a common one) was that i had several apps deployed on servers, and i wanted my development environment to match that of the servers as closely as possible.  This means having the same gems as the servers, and, crucially, having the same versions of those gems.  Normally this would be a big pain in the ass, hunting around for places to get old out of date gems from.  Not so, however &#8211; i ssh onto my server and run</p>
<p><strong>gem server </strong>&amp; #the &amp; is just to make it run in the background</p>
<p>This starts a gem server running on port 8808.</p>
<p>Now, back in my local machine, i can type</p>
<p><strong>sudo gem install insert-gem-name-here &#8211;remote &#8211;source http://mywebsite.com:8808</strong></p>
<p>and, hey presto, i get the version that&#8217;s on the server, installed locally.  Even better, you can pass it a list of all the gems you need at once, and go and drink a beer to celebrate the utter wonderfulness of rubygems.  Repeat for every app you&#8217;re working on, pointing it at it&#8217;s relevant server.  Huzzah!</p>
<p>Thanks Marnen!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=110&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2009/11/11/gem-magic-copying-gems-from-a-server-to-your-local-repository/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
		<item>
		<title>BASH howto &#8211; find files with required text in an svn managed folder</title>
		<link>http://webcrisps.wordpress.com/2009/11/05/bash-howto-find-files-with-required-text-in-an-svn-managed-folder/</link>
		<comments>http://webcrisps.wordpress.com/2009/11/05/bash-howto-find-files-with-required-text-in-an-svn-managed-folder/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 11:26:11 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webcrisps.wordpress.com/?p=104</guid>
		<description><![CDATA[This is just a bash (linux/mac terminal) one-liner to look for files with matching text in my rails app, which i do all the time.  It&#8217;s a lot faster and more useful than Nautilus&#8217; file search, and is useful when &#8230; <a href="http://webcrisps.wordpress.com/2009/11/05/bash-howto-find-files-with-required-text-in-an-svn-managed-folder/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=104&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is just a bash (linux/mac terminal) one-liner to look for files with matching text in my rails app, which i do all the time.  It&#8217;s a lot faster and more useful than Nautilus&#8217; file search, and is useful when you don&#8217;t use any IDEs.  Personally i do everything in the command line and gedit, nice and simple.</p>
<p>Here i&#8217;m looking for the string &#8220;ratings&#8221; in my views folder and my public/javascripts folder</p>
<p>This is broken down as follows:</p>
<p>get all files in these folders<br />
| surround them with quotes (otherwise filenames with spaces would screw up the later steps)<br />
| only consider non-svn files (i&#8217;m not interested in the files svn uses to manage things)<br />
| look inside the files for the search string</p>
<p>And here it is:</p>
<p>find app/views public/javascripts -print | awk &#8216;{print &#8220;\&#8221;"$0&#8243;\&#8221;"}&#8217; | grep -v \.svn | xargs grep -i &#8220;ratings&#8221;</p>
<p>Obviously this is a bit of a mouthful but once it&#8217;s been done it will go into your command memory, and you can get it back by pressing ctrl&amp;r, and starting to type &#8216;find&#8217;.  You may need to press ctrl&amp;r a few more times to get the right command from memory.</p>
<p>Note &#8211; wordpress or your browser may change some of the above double quotes to weird web quote characters.  I really fracking hate that.  Anyway, when you paste it over, change all the quotes to normal single or double quotes.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/104/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=104&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2009/11/05/bash-howto-find-files-with-required-text-in-an-svn-managed-folder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
		<item>
		<title>Distinguish between local production mode and server production mode in Rails</title>
		<link>http://webcrisps.wordpress.com/2009/10/07/distinguish-between-local-production-mode-and-server-production-mode-in-rails/</link>
		<comments>http://webcrisps.wordpress.com/2009/10/07/distinguish-between-local-production-mode-and-server-production-mode-in-rails/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 11:31:45 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webcrisps.wordpress.com/?p=101</guid>
		<description><![CDATA[I just ran into a problem with production mode. Normally i never run this locally but i decided to use it to run my selenium test battery against, since it&#8217;s much faster than dev mode. In production mode on our &#8230; <a href="http://webcrisps.wordpress.com/2009/10/07/distinguish-between-local-production-mode-and-server-production-mode-in-rails/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=101&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I just ran into a problem with production mode.  Normally i never run this locally but i decided to use it to run my selenium test battery against, since it&#8217;s much faster than dev mode.  In production mode on our server we pull our assets in from an amazon asset server, done with this line in production.rb</p>
<p>config.action_controller.asset_host = &#8220;http://assets.fakedomain.ir.com&#8221;</p>
<p>In my local version of production i don&#8217;t want to do this though, i just want to use what&#8217;s in my local public folder.  I decided to have a look at ENV which i did by making apache explode <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   prior to the above line &#8211; </p>
<p>raise &#8220;ENV = #{ENV.inspect}&#8221;</p>
<p>Quick restart of my local apache, reload the page, and BANG, i can see this:</p>
<p>ENV = {&#8220;APACHE_PID_FILE&#8221;=&gt;&#8221;/var/run/apache2.pid&#8221;, &#8220;PATH&#8221;=&gt;&#8221;/usr/local/bin:/usr/bin:/bin&#8221;, &#8220;LANG&#8221;=&gt;&#8221;C&#8221;, &#8220;APACHE_RUN_GROUP&#8221;=&gt;&#8221;www-data&#8221;, &#8220;APACHE_RUN_USER&#8221;=&gt;&#8221;www-data&#8221;, &#8220;PWD&#8221;=&gt;&#8221;/etc/apache2/sites-available&#8221;, &#8220;RAILS_ENV&#8221;=&gt;&#8221;production&#8221;, &#8220;HOME&#8221;=&gt;&#8221;/home/max&#8221;, &#8220;RAILS_ASSET_ID&#8221;=&gt;&#8221;"}</p>
<p>aha &#8211;  &#8220;HOME&#8221;=&gt;&#8221;/home/max&#8221; &#8211; perfect!</p>
<p>So, i changed the line (and removed the raise!) to </p>
<p>config.action_controller.asset_host = &#8220;http://assets.fakedomain.ir.charanga.com&#8221; unless ENV['HOME'] == &#8220;/home/max&#8221;</p>
<p>Bingo.  Perhaps not the cleverest way of doing it, but it&#8217;s safe &#8211; for everyone else, including our server, ENV['HOME'] will be something else, or perhaps nil.  Either way, they&#8217;ll use the remote assets site.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/101/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=101&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2009/10/07/distinguish-between-local-production-mode-and-server-production-mode-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
		<item>
		<title>Stubbing methods (eg callbacks) in a Machinist blueprint</title>
		<link>http://webcrisps.wordpress.com/2009/08/13/stubbing-before_create-callbacks-in-a-machinist-blueprint/</link>
		<comments>http://webcrisps.wordpress.com/2009/08/13/stubbing-before_create-callbacks-in-a-machinist-blueprint/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 08:59:41 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webcrisps.wordpress.com/?p=96</guid>
		<description><![CDATA[I had a problem recently where i was using the test object-generating plugin Machinist to make a blueprint for one of my model classes. The model has a before_create filter which references some other data in the database, which didn&#8217;t &#8230; <a href="http://webcrisps.wordpress.com/2009/08/13/stubbing-before_create-callbacks-in-a-machinist-blueprint/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=96&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had a problem recently where i was using the test object-generating plugin <a href="http://github.com/notahat/machinist/tree/master">Machinist</a> to make a blueprint for one of my model classes.  The model has a before_create filter which references some other data in the database, which didn&#8217;t exist in my test version of the db.  This before_create filter had nothing to do with the tests i was running and i wanted to just stub it out.  I couldn&#8217;t work out how, so wrote a wrapper method that looked like this:</p>
<p><pre class="brush: ruby;">
def make_music_service(attributes = {})
  music_service = MusicService.make_unsaved(attributes)
  music_service.stub!(:set_default_column_customisations).and_return(true)
  music_service.save
  music_service
end
</pre></p>
<p>That meant that whenever i wanted to make a MusicService in my tests i had to call this method instead.  Pretty clumsy, eh.  But &#8211; Machinist creator <a href="http://notahat.com/">Pete Yandell</a> came through with a great bit of info on the machinist mailing list &#8211; when you&#8217;re inside your blueprint, you can reference the object being made with &#8216;object&#8217;.  So, now i can put the stub inside the blueprint, like so:</p>
<p><pre class="brush: ruby;">
MusicService.blueprint do
  name     { Faker::Name.name }
  address1 { Faker::Address.street_address }
  address2 { Faker::Address.street_address }
  address3 { Faker::Address.city }
  postcode { Faker::Address.uk_postcode }
  default_password { &quot;password&quot; }
  object.stub!(:set_default_column_customisations).and_return(true)
end
</pre></p>
<p>Thanks Pete!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/96/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=96&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2009/08/13/stubbing-before_create-callbacks-in-a-machinist-blueprint/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
		<item>
		<title>Firebug &amp; jQuery &#8211; making other people&#8217;s sites better</title>
		<link>http://webcrisps.wordpress.com/2009/07/24/firebug-jquery-making-other-peoples-site-better/</link>
		<comments>http://webcrisps.wordpress.com/2009/07/24/firebug-jquery-making-other-peoples-site-better/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 14:43:28 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webcrisps.wordpress.com/?p=91</guid>
		<description><![CDATA[Was just on a website that had 80 checkboxes (all checked) for various options. I wanted to have only one checked. There&#8217;s no &#8216;Uncheck all&#8217; button. Design Fail! But &#8211; open the firebug console and run jQuery(&#8220;input[type='checkbox']&#8220;).attr(&#8216;checked&#8217;,false) Bing, all unchecked. &#8230; <a href="http://webcrisps.wordpress.com/2009/07/24/firebug-jquery-making-other-peoples-site-better/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=91&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Was just on a website that had 80 checkboxes (all checked) for various options.  I wanted to have only one checked.  There&#8217;s no &#8216;Uncheck all&#8217; button.  Design Fail!  But &#8211; open the firebug console and run</p>
<p>jQuery(&#8220;input[type='checkbox']&#8220;).attr(&#8216;checked&#8217;,false)</p>
<p>Bing, all unchecked.  I get a childlike sense of wonder and joy from that, still.  If only more things in life could be improved with a little console activity.</p>
<p>jQuery(&#8220;#pub.crap&#8221;).attr(&#8220;ale-count&#8221;, 20)<br />
 <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=91&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2009/07/24/firebug-jquery-making-other-peoples-site-better/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
		<item>
		<title>Gotcha with hand-rolled sql in rails associations &#8211; eg self.id and has_many</title>
		<link>http://webcrisps.wordpress.com/2009/07/21/gotcha-with-hand-rolled-sql-in-rails-associations-eg-self-id-and-has_many/</link>
		<comments>http://webcrisps.wordpress.com/2009/07/21/gotcha-with-hand-rolled-sql-in-rails-associations-eg-self-id-and-has_many/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 10:31:59 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webcrisps.wordpress.com/?p=87</guid>
		<description><![CDATA[Sometimes in rails associations (eg has_many) you can&#8217;t quite do what you want with the existing options and have to do a bit of hand-rolled sql. And, often you want to refer to the object that the association will be &#8230; <a href="http://webcrisps.wordpress.com/2009/07/21/gotcha-with-hand-rolled-sql-in-rails-associations-eg-self-id-and-has_many/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=87&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes in rails associations (eg has_many) you can&#8217;t quite do what you want with the existing options and have to do a bit of hand-rolled sql.  And, often you want to refer to the object that the association will be called on.  BUT &#8211; the following doesn&#8217;t work. (i&#8217;ve used a trivial example here, we wouldn&#8217;t normally hand-roll in this case)</p>
<p><pre class="brush: ruby;">
has_many :foos, :finder_sql =&gt; &quot;select * from foos where bar_id = #{self.id}&quot;
</pre><br />
The reason this doesn&#8217;t work is that the string has double quotes, which means that #{self.id} will be evaluated when the string is evaluated, which, because this is a class method, is at class load time!  That means that self.id will come out as the id of the class &#8211; not what we want.  </p>
<p>To get around this, use single quotes instead:</p>
<p><pre class="brush: ruby;">
has_many :foos, :finder_sql =&gt; 'select * from foos where bar_id = #{self.id}'
</pre><br />
With single quotes, the #{} block isn&#8217;t evaluated and sits there, waiting for the association to be called.  When that happens, the finder_sql is combined, and the eval block evaluated, and at that point self IS the current instance, and the association will work.   Bear in mind that, because we&#8217;re now using single quotes to wrap the string, any single quotes you might have in your sql will now need to be escaped:</p>
<p><pre class="brush: ruby;">
has_many :foos, :finder_sql =&gt; 'select * from foos where flavour = \'chicken\' and bar_id =#{self.id}'
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/87/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=87&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2009/07/21/gotcha-with-hand-rolled-sql-in-rails-associations-eg-self-id-and-has_many/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
		<item>
		<title>The shocking truth about rails callbacks! (mind answering my poll?)</title>
		<link>http://webcrisps.wordpress.com/2009/06/14/the-shocking-truth-about-rails-callbacks-mind-answering-my-poll/</link>
		<comments>http://webcrisps.wordpress.com/2009/06/14/the-shocking-truth-about-rails-callbacks-mind-answering-my-poll/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 22:07:32 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webcrisps.wordpress.com/?p=81</guid>
		<description><![CDATA[Ok, the title&#8217;s a bit overly dramatic.  But i recently stumbled upon a feature of rails callbacks (methods called by before_save, after_create, etc) which meant my code was breaking, in a silent way.  After speaking to a few people about &#8230; <a href="http://webcrisps.wordpress.com/2009/06/14/the-shocking-truth-about-rails-callbacks-mind-answering-my-poll/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=81&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ok, the title&#8217;s a bit overly dramatic.  But i recently stumbled upon a feature of rails callbacks (methods called by before_save, after_create, etc) which meant my code was breaking, in a silent way.  After speaking to a few people about it, it seems a lot of people were unaware of this as well.  If you don&#8217;t want to read me waffling about my voyage of discovery then would you mind just skipping down to my poll about this?   I&#8217;m trying to get a sense of how many other people didn&#8217;t know about this either.</p>
<p>Halfway down the api page documenting callbacks ( <a href="http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=C00000640&amp;name=Callbacks">http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=C00000640&amp;name=Callbacks </a>) , we read this:</p>
<h2><em>Canceling callbacks</em></h2>
<p><em>If a <tt>before_*</tt> callback returns <tt>false</tt>, all the later callbacks and the associated action are cancelled. If an <tt>after_*</tt><tt>false</tt>, all the later callbacks are cancelled. <a href="http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=C00000640&amp;name=Callbacks">Callbacks</a> are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last.</em></p>
<p>Now, i noticed this only because i encountered what seemed to be a bug in my code.  A record wasn&#8217;t being saved, and i couldn&#8217;t work out why.  It wasn&#8217;t failing validations.  If i tried to do it manually in the console, with the same data, i got this odd situation:</p>
<p>@user.valid?<br />
=&gt; true</p>
<p>@user.save<br />
=&gt; false</p>
<p>@user.errors.full_messages<br />
=&gt; []</p>
<p>WTF? i thought&#8230;  after a lot of logging, i managed to work out that it was because i&#8217;d added a before_create filter, which set one of the fields to the same as whatever was in a particular field in an associated object, like this:</p>
<p><pre class="brush: ruby;">
before_create :set_student_access_to_same_as_music_service&lt;/pre&gt;
def set_student_access_to_same_as_music_service
  self.student_access = self.music_service.show_student_access if self.music_service
end
</pre></p>
<p>So, if the user&#8217;s music service&#8217;s show_student_access field is false, we set the new user&#8217;s student_access to false, as a sensible default value.  As a side effect, the method returns false, as that&#8217;s the last thing to be evaluated.  And because the callback method returned false, the callback chain is halted and the record isn&#8217;t saved.  But, it&#8217;s not failing validations so i don&#8217;t get to see why.</p>
<p>Now, i&#8217;m not saying this is wrong &#8211; it&#8217;s in the api after all.  I&#8217;m just curious as to how many people were also unaware of this &#8211; out of the people i&#8217;ve spoken to it seems about only half of them knew, which is pretty bad considering it&#8217;s a major feature of callbacks, that would be causing unexpected effects all over the place if you weren&#8217;t careful about not returning false.  So, would you mind answering my poll, below?  </p>
<p>thanks! max</p>
<a href="http://polldaddy.com/poll/1706581/">View This Poll</a>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/81/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=81&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2009/06/14/the-shocking-truth-about-rails-callbacks-mind-answering-my-poll/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
		<item>
		<title>Making acts_as_ferret deal nicely with pluralisation</title>
		<link>http://webcrisps.wordpress.com/2009/03/19/making-acts_as_ferret-deal-nicely-with-pluralisation/</link>
		<comments>http://webcrisps.wordpress.com/2009/03/19/making-acts_as_ferret-deal-nicely-with-pluralisation/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 12:23:21 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webcrisps.wordpress.com/?p=66</guid>
		<description><![CDATA[I recently discovered how to nicely deal with pluralisation in the Rails plugin &#8216;acts_as_ferret&#8216; (which we use in our site). When i search for &#8216;trumpet&#8217;, i want to find resources with &#8216;trumpets&#8217;, and vice versa. By default acts_as_ferret doesn&#8217;t do &#8230; <a href="http://webcrisps.wordpress.com/2009/03/19/making-acts_as_ferret-deal-nicely-with-pluralisation/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=66&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently discovered how to nicely deal with pluralisation in the Rails plugin &#8216;<a href="http://www.jkraemer.net/projects/acts_as_ferret">acts_as_ferret</a>&#8216; (which we use in our site).   When i search for &#8216;trumpet&#8217;, i want to find resources with &#8216;trumpets&#8217;, and vice versa.  By default acts_as_ferret doesn&#8217;t do this, but it&#8217;s easy to add.  I found the answer in this post on the acts_as_ferret mailing list/forum:</p>
<p><a href="http://www.ruby-forum.com/topic/80178">http://www.ruby-forum.com/topic/80178</a></p>
<p>Anyway, here&#8217;s how i used it.  I&#8217;m going to assume that anyone reading this has already installed the ferret gem and acts_as_ferret plugin.</p>
<p>Make a new file in lib, called &#8216;acts_as_ferret_extensions.rb&#8217;</p>
<p><pre class="brush: ruby;">
#lib/acts_as_ferret_extensions.rb

module Ferret::Analysis
  class StemmingAnalyzer
    def token_stream(field, text)
      StemFilter.new(StandardTokenizer.new(text.downcase))
    end
  end
end
</pre></p>
<p>Now require this file in the place in your config where you require stuff.  In the default case this would be in config/environment.rb:</p>
<p><pre class="brush: ruby;">
#in config/environment.rb
require 'acts_as_ferret_extensions'
</pre></p>
<p>Next, change your calls to acts_as_ferret to specify an analyzer:  here&#8217;s one of mine as an example, from the &#8216;Asset&#8217; class in this case.</p>
<p><pre class="brush: ruby;">
  #in asset.rb (an example from my app)
  acts_as_ferret( :ferret =&gt; {:analyzer =&gt; Ferret::Analysis::StemmingAnalyzer.new},
                  :fields =&gt; { :name =&gt; {},
                               :description =&gt; {}
                               #...etc
                              } )
</pre></p>
<p>Now, delete the index and rebuild it &#8211; deleting it might not be necessary but i&#8217;ve found weird results rebuilding after changing the options to acts_as_ferret: deleting the index is definitely safer.</p>
<p><pre class="brush: ruby;">
#at the command line
rm -r index/development/asset/*
#in the console
Asset.rebuild_index
</pre></p>
<p>Now, when you do a search, specify this analyzer again:</p>
<p><pre class="brush: ruby;">
#in the console
assets = Asset.find_with_ferret(&quot;trumpet&quot;, :page =&gt; 1,
                                           :per_page =&gt; 20,
                                           :analyzer =&gt; Ferret::Analysis::StemmingAnalyzer.new)
</pre></p>
<p>And that should be it!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=66&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2009/03/19/making-acts_as_ferret-deal-nicely-with-pluralisation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating a custom &#8220;be_valid&#8221; matcher for rspec_on_rails</title>
		<link>http://webcrisps.wordpress.com/2008/02/07/creating-a-custom-be_valid-matcher-for-rspec_on_rails/</link>
		<comments>http://webcrisps.wordpress.com/2008/02/07/creating-a-custom-be_valid-matcher-for-rspec_on_rails/#comments</comments>
		<pubDate>Thu, 07 Feb 2008 13:51:57 +0000</pubDate>
		<dc:creator>Max Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nubyonrails.wordpress.com/?p=61</guid>
		<description><![CDATA[I&#8217;ve been discovering the excellent &#8216;behaviour-driven development&#8217; plugin &#8216;rspec&#8217; , and its rails-specific cousin (nephew?) &#8216;rspec_on_rails&#8217; this week. At the moment i&#8217;m testing that validation works properly on some models. Now, rspec already generates a matcher for any predicate method &#8230; <a href="http://webcrisps.wordpress.com/2008/02/07/creating-a-custom-be_valid-matcher-for-rspec_on_rails/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=63&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been discovering the excellent &#8216;behaviour-driven development&#8217; plugin &#8216;rspec&#8217; , and its rails-specific cousin (nephew?) &#8216;rspec_on_rails&#8217; this week.</p>
<p>At the moment i&#8217;m testing that validation works properly on some models.  Now, rspec already generates a matcher for any predicate method that can be called on an object (eg  &#8220;object.should be_foo&#8221; for &#8220;object.foo?.should eql(true)&#8221;).  I was using this with valid?, which we use on an typical (ie ActiveRecord) model to see if it will pass validation (and therefore be able to be saved to the database).  This is generally nicer than trying to save it and seeing if it saved ok.</p>
<p>So, i was writing specs like this<br />
<pre class="brush: ruby;">
it &quot;should validate with only a name&quot; do
  @foo = Foo.new(:name =&gt; &quot;bar&quot;)
  @foo.should be_valid
end</pre><br />
This works, in terms of correctly passing or failing, but it doesn&#8217;t give me much information &#8211; it just tells me that something fails validation.  I could get at the error messages in the test by doing the test as follows:<br />
<pre class="brush: ruby;">
it &quot;should validate with only a name&quot; do
  @foo = Foo.new(:name =&gt; &quot;bar&quot;)
  @foo.should be_valid
  @foo.errors.full_messages.should eql([])
end</pre><br />
but this is a bit tiresome.  I&#8217;d rather just write &#8220;should be_valid&#8221;.  So, i decided to write a custom matcher that overrides the auto-generated &#8216;be_valid&#8217; method and tells me why it failed validation (if it does fail).  Here&#8217;s how i did it.  Thanks to the excellent <a href="http://peepcode.com">peepcode</a> rspec movies for the inspiration and knowhow!In my spec folder, i made a file called &#8216;custom_matcher.rb&#8217;.   In it is a module which will hold any custom rspec matchers i  write, though there&#8217;s only one at the moment.<br />
<pre class="brush: ruby;">
module CustomMatchers
  #checks that an AR model validates, by testing error messages from .valid?
  #displays any error messages recieved in test failure output
  class BeValid
    #do any setup required - at the very least, set some instance variables.
    #In this case, i don't take any arguments - it simply either passes or fails.
    def initialize
      @expected = []
    end

    #perform the actual match - 'target' is the thing being tested
    def matches?(target)
      #target.errors.full_messages is an array of error messages produced by the valid? method
      #if valid? is true, it will be empty
      target.valid?
      @errors = target.errors.full_messages
      @errors.eql?(@expected)
    end

    #displayed when 'should' fails
    def failure_message
      &quot;validation failed with #{@errors.inspect}, expected no validation errors&quot;
    end

    #displayed when 'should_not' fails
    def negative_failure_message
      &quot;validation succeeded, expected one or more validation errors&quot;
    end

    #displayed in the spec description if the user doesn't provide one (ie if they just write 'it do' for the spec header)
    def description
      &quot;validate successfully&quot;
    end

    # Returns string representation of the object being tested
    def to_s(value)
      &quot;#{@errors.inspect}&quot;
    end
  end

  # the matcher method that the user calls in their specs
  def be_valid
    BeValid.new
  end
end</pre><br />
<pre class="brush: ruby;">
#To hook it up, add the following require to spec_helper.rb:
require 'spec/custom_matchers'</pre><br />
<pre class="brush: ruby;">
#And add the following line to the &quot;Spec::Runner.configure do |config|&quot; section:
config.include(CustomMatchers)</pre><br />
This is about as simple as a matcher could be &#8211; i  don&#8217;t take any arguments at all, so the expected result is always an empty array (of errors).   The &#8216;value&#8217; added by this is that i&#8217;m now outputting the error messages (if any) in the failure report.  Because i&#8217;ve provided a description method, i don&#8217;t even need to provide a description to my specs if i don&#8217;t want to:  for example, here&#8217;s a test which will fail (and its surrounding description):<br />
<pre class="brush: ruby;">
describe SchoolSubscriber, &quot;, when we make an empty school_subscriber, &quot; do
  before do
    @ss = SchoolSubscriber.new
  end

  it do
    @ss.should be_valid
  end
end</pre><br />
And here&#8217;s the failure report from rspec:</p>
<pre>
<font color="#ff0000">1)
'SchoolSubscriber , when we make an empty school_subscriber,  should validate successfully' FAILED
validation failed with ["Phone number can't be blank", "First name can't be blank", "Position can't be blank", "School can't be blank", "Last name can't be blank", "Email can't be blank"], expected no validation errors</font></pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webcrisps.wordpress.com/63/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webcrisps.wordpress.com/63/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webcrisps.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webcrisps.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webcrisps.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webcrisps.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webcrisps.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webcrisps.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webcrisps.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webcrisps.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webcrisps.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webcrisps.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webcrisps.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webcrisps.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webcrisps.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webcrisps.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webcrisps.wordpress.com&amp;blog=7009457&amp;post=63&amp;subd=webcrisps&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webcrisps.wordpress.com/2008/02/07/creating-a-custom-be_valid-matcher-for-rspec_on_rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73c04e9ef9ca435c5b19a2e765ae6d20?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Max Williams</media:title>
		</media:content>
	</item>
	</channel>
</rss>
