<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>iain.nl</title>
	
	<link>http://iain.nl</link>
	<description>Adventures with Ruby</description>
	<lastBuildDate>Fri, 05 Feb 2010 22:51:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/iain-nl" /><feedburner:info uri="iain-nl" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Going crazy with to_proc</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/6lFGsj8UB4M/</link>
		<comments>http://iain.nl/2010/02/going-crazy-with-to_proc/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 21:13:32 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[to_proc]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=621</guid>
		<description><![CDATA[You all know Symbol#to_proc, right? It allows you to write this:

It has been in Rails as long as I can remember, and is in Ruby 1.8.7 and 1.9.x. I love it to death and I use it everywhere I can.
It is actually quite simple, and you can implement it yourself:

It works because when you prepend ]]></description>
			<content:encoded><![CDATA[<p><img src="http://iain.nl/wp-content/uploads/2010/02/ampersand.jpg" alt="" title="ampersand" width="215" height="238" class="alignright size-full wp-image-626" />You all know <tt>Symbol#to_proc</tt>, right? It allows you to write this:<br />
<script src="http://gist.github.com/296165.js?file=example_1.rb"></script></p>
<p>It has been in Rails as long as I can remember, and is in Ruby 1.8.7 and 1.9.x. I love it to death and I use it everywhere I can.</p>
<p>It is actually quite simple, and you can implement it yourself:</p>
<p><script src="http://gist.github.com/296165.js?file=symbol_to_proc.rb"></script></p>
<p>It works because when you prepend an ampersand (&amp;) to any Ruby object, it calls <tt>#to_proc</tt> to get a proc to use as block for the method.</p>
<p>What I always regretted though was not being to pass any arguments, so I hacked and monkeypatched a bit, and got:</p>
<p><script src="http://gist.github.com/296165.js?file=symbol.rb"></script></p>
<p>So you can now write:</p>
<p><script src="http://gist.github.com/296165.js?file=example_2.rb"></script></p>
<p>Not that this is any shorter than just creating the darn block in the first place. But hey, it&#8217;s a good exercise in metaprogramming and show of more of Ruby&#8217;s awesome flexibility.</p>
<p>After this I remembered something similar that annoyed me before. It&#8217;s that Rails helper methods are just a bag of methods available to, because they are mixed in your template. So if you have an array of numbers that you want to format as currency, you&#8217;d have to do:</p>
<p><script src="http://gist.github.com/296165.js?file=example_3.erb"></script></p>
<p>What if I could apply some <tt>to_proc</tt>-love to that too? All these helper methods cannot be added to strings, fixnums, and the likes; that would clutter <em>way</em> to much. Rather, it might by a nice idea to use procs that understands helper methods. Here is what I created:</p>
<p><script src="http://gist.github.com/296165.js?file=proc_proxy_helper.rb"></script></p>
<p>I used a clean blank class (in Ruby 1.9, you&#8217;d want to inherit it from <tt>BasicObject</tt>), in which I will provide the proper <tt>proc</tt>-object. I play around with the argument list a bit, handling multiple arguments and blocks too. You can now use this syntax:</p>
<p><script src="http://gist.github.com/296165.js?file=example_4.erb"></script></p>
<p>That is a lot sexier if you as me. And you can use it in any object, not just inside views. And lets add some extra arguments and some <tt>Enumerator</tt>-love too:</p>
<p><script src="http://gist.github.com/296165.js?file=example_5.rb"></script></p>
<p>In case you are wondering, the position you can specify is to tell where the arguments need to go. Position 0 is the method name, so you shouldn&#8217;t use that, but any other value is okay.  An example might be that you cant to wrap an array of texts into span-tags:</p>
<p><script src="http://gist.github.com/296165.js?file=example_6.erb"></script></p>
<p>So there you have it. I&#8217;m probably solving a problem that doesn&#8217;t exist. It is however a nice example of the awesome power of Ruby. I hope you&#8217;ve enjoyed this little demonstration of the possible uses of <tt>to_proc</tt>.</p>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2010%2F02%2Fgoing-crazy-with-to_proc%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=Going%20crazy%20with%20to_proc&amp;body=http%3A%2F%2Fiain.nl%2F2010%2F02%2Fgoing-crazy-with-to_proc%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2010%2F02%2Fgoing-crazy-with-to_proc%2F&amp;title=Going%20crazy%20with%20to_proc&amp;bodytext=You%20all%20know%20Symbol%23to_proc%2C%20right%3F%20It%20allows%20you%20to%20write%20this%3A%0D%0A%0D%0A%0D%0AIt%20has%20been%20in%20Rails%20as%20long%20as%20I%20can%20remember%2C%20and%20is%20in%20Ruby%201.8.7%20and%201.9.x.%20I%20love%20it%20to%20death%20and%20I%20use%20it%20everywhere%20I%20can.%0D%0A%0D%0AIt%20is%20actually%20quite%20simple%2C%20and%20you%20can%20implem" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2010%2F02%2Fgoing-crazy-with-to_proc%2F&amp;title=Going%20crazy%20with%20to_proc&amp;notes=You%20all%20know%20Symbol%23to_proc%2C%20right%3F%20It%20allows%20you%20to%20write%20this%3A%0D%0A%0D%0A%0D%0AIt%20has%20been%20in%20Rails%20as%20long%20as%20I%20can%20remember%2C%20and%20is%20in%20Ruby%201.8.7%20and%201.9.x.%20I%20love%20it%20to%20death%20and%20I%20use%20it%20everywhere%20I%20can.%0D%0A%0D%0AIt%20is%20actually%20quite%20simple%2C%20and%20you%20can%20implem" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2010%2F02%2Fgoing-crazy-with-to_proc%2F&amp;t=Going%20crazy%20with%20to_proc" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2010%2F02%2Fgoing-crazy-with-to_proc%2F&amp;title=Going%20crazy%20with%20to_proc&amp;annotation=You%20all%20know%20Symbol%23to_proc%2C%20right%3F%20It%20allows%20you%20to%20write%20this%3A%0D%0A%0D%0A%0D%0AIt%20has%20been%20in%20Rails%20as%20long%20as%20I%20can%20remember%2C%20and%20is%20in%20Ruby%201.8.7%20and%201.9.x.%20I%20love%20it%20to%20death%20and%20I%20use%20it%20everywhere%20I%20can.%0D%0A%0D%0AIt%20is%20actually%20quite%20simple%2C%20and%20you%20can%20implem" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2010%2F02%2Fgoing-crazy-with-to_proc%2F&amp;title=Going%20crazy%20with%20to_proc" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Going%20crazy%20with%20to_proc%20-%20http%3A%2F%2Fiain.nl%2F2010%2F02%2Fgoing-crazy-with-to_proc%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/6lFGsj8UB4M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/02/going-crazy-with-to_proc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/02/going-crazy-with-to_proc/</feedburner:origLink></item>
		<item>
		<title>3 times ActiveSupport 3</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/ta7z3o0JilM/</link>
		<comments>http://iain.nl/2010/02/3-times-activesupport-3/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 09:42:55 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[activesupport]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[rails 3]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=613</guid>
		<description><![CDATA[Rails 3 is coming. All the big changes are spoken of elsewhere, so I&#8217;m going to mention some small changes. Here are 3 random new methods added to ActiveSupport:
presence
First up is Object#presence which is a shortcut for Object#present? &#038;&#038; Object. It is a bit of a sanitizer. Empty strings and other blank values will return ]]></description>
			<content:encoded><![CDATA[<p>Rails 3 is coming. All the big changes are spoken of elsewhere, so I&#8217;m going to mention some small changes. Here are 3 random new methods added to ActiveSupport:</p>
<h4>presence</h4>
<p>First up is <tt>Object#presence</tt> which is a shortcut for <tt>Object#present? &#038;&#038; Object</tt>. It is a bit of a sanitizer. Empty strings and other blank values will return <tt>nil</tt> and any other value will return itself. Use this one and your code might be a tad cleaner.</p>
<pre>
"".presence # => nil
"foo".presence #=> "foo"

# without presence:
if params[:foo].present? &#038;&#038; (foo = params[:foo])
  # ..
end

# with presence:
if foo = params[:foo].presence
  # ...
end

# The example Rails gives:
state = params[:state] if params[:state].present?
country = params[:country] if params[:country].present?
region = state || country || 'US'
# ...becomes:
region = params[:state].presence || params[:country].presence || 'US'
</pre>
<p>I like this way of cleaning up you&#8217;re code. I guess it&#8217;s Rubyesque to feel the need to tidy and shorten your code like this.</p>
<h4>uniq_by</h4>
<p>Another funny one is <tt>Array.uniq_by</tt> (and it sister-with-a-bang-method). It works as select, but returns only the first element from the array that complies with the block you gave it. Here are some examples to illustrate that:</p>
<pre>
[ 1, 2, 3, 4 ].uniq_by(&#038;:odd?) # => [ 1, 2 ]

posts = %W"foo bar foo".map.with_index do |title, i|
  Post.create(:title => title, :index => i)
end
posts.uniq_by(&#038;:title)
# => [ Post("foo", 0), Post("bar", 1) ] ( and not Post("foo", 2) )

some_array.uniq_by(&#038;:object_id) # same as some_array.uniq
</pre>
<h4>exclude?</h4>
<p>And the final one for today is <tt>exclude?</tt> which is the opposite of <tt>include?</tt>. Nobody likes the exclamation mark before predicate methods.</p>
<pre>
# yuck:
!some_array.include?(some_value)
# better:
some_array.exclude?(some_value)
</pre>
<p>And it also works on strings:</p>
<pre>
# even more yuck:
!"The quick fox".include?("quick") # => false
# better:
"The quick fox".exclude?("quick") # => false
</pre>
<p>The full release notes of Rails 3 can be <a href="http://guides.rails.info/3_0_release_notes.html">read here</a>.</p>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2010%2F02%2F3-times-activesupport-3%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=3%20times%20ActiveSupport%203&amp;body=http%3A%2F%2Fiain.nl%2F2010%2F02%2F3-times-activesupport-3%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2010%2F02%2F3-times-activesupport-3%2F&amp;title=3%20times%20ActiveSupport%203&amp;bodytext=Rails%203%20is%20coming.%20All%20the%20big%20changes%20are%20spoken%20of%20elsewhere%2C%20so%20I%27m%20going%20to%20mention%20some%20small%20changes.%20Here%20are%203%20random%20new%20methods%20added%20to%20ActiveSupport%3A%0D%0A%0D%0Apresence%0D%0A%0D%0AFirst%20up%20is%20Object%23presence%20which%20is%20a%20shortcut%20for%20Object%23present%3F%20%26%26%20Ob" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2010%2F02%2F3-times-activesupport-3%2F&amp;title=3%20times%20ActiveSupport%203&amp;notes=Rails%203%20is%20coming.%20All%20the%20big%20changes%20are%20spoken%20of%20elsewhere%2C%20so%20I%27m%20going%20to%20mention%20some%20small%20changes.%20Here%20are%203%20random%20new%20methods%20added%20to%20ActiveSupport%3A%0D%0A%0D%0Apresence%0D%0A%0D%0AFirst%20up%20is%20Object%23presence%20which%20is%20a%20shortcut%20for%20Object%23present%3F%20%26%26%20Ob" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2010%2F02%2F3-times-activesupport-3%2F&amp;t=3%20times%20ActiveSupport%203" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2010%2F02%2F3-times-activesupport-3%2F&amp;title=3%20times%20ActiveSupport%203&amp;annotation=Rails%203%20is%20coming.%20All%20the%20big%20changes%20are%20spoken%20of%20elsewhere%2C%20so%20I%27m%20going%20to%20mention%20some%20small%20changes.%20Here%20are%203%20random%20new%20methods%20added%20to%20ActiveSupport%3A%0D%0A%0D%0Apresence%0D%0A%0D%0AFirst%20up%20is%20Object%23presence%20which%20is%20a%20shortcut%20for%20Object%23present%3F%20%26%26%20Ob" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2010%2F02%2F3-times-activesupport-3%2F&amp;title=3%20times%20ActiveSupport%203" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=3%20times%20ActiveSupport%203%20-%20http%3A%2F%2Fiain.nl%2F2010%2F02%2F3-times-activesupport-3%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/ta7z3o0JilM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/02/3-times-activesupport-3/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/02/3-times-activesupport-3/</feedburner:origLink></item>
		<item>
		<title>http_accept_language released as a gem</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/TWdHwWJDBWM/</link>
		<comments>http://iain.nl/2010/01/http_accept_language-released-as-a-gem/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 11:27:16 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Gems]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=606</guid>
		<description><![CDATA[I released an old Rails plugin as gem today. Slowly but surely, all my plugins will be converted to gems.
This time it&#8217;s an old one: http_accept_language

Splits the http-header into languages specified by the user
Returns empty array if header is illformed.
Corrects case to xx-XX
Sorted by priority given, as much as possible.
Gives you the most important language
Gives ]]></description>
			<content:encoded><![CDATA[<p>I released an old Rails plugin as gem today. Slowly but surely, all my plugins will be converted to gems.</p>
<p>This time it&#8217;s an old one: <a href="http://github.com/iain/http_accept_language">http_accept_language</a></p>
<ul>
<li>Splits the http-header into languages specified by the user</li>
<li>Returns empty array if header is illformed.</li>
<li>Corrects case to xx-XX</li>
<li>Sorted by priority given, as much as possible.</li>
<li>Gives you the most important language</li>
<li>Gives compatible languages</li>
</ul>
<p>For more information, read the README on GitHub.</p>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2010%2F01%2Fhttp_accept_language-released-as-a-gem%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=http_accept_language%20released%20as%20a%20gem&amp;body=http%3A%2F%2Fiain.nl%2F2010%2F01%2Fhttp_accept_language-released-as-a-gem%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2010%2F01%2Fhttp_accept_language-released-as-a-gem%2F&amp;title=http_accept_language%20released%20as%20a%20gem&amp;bodytext=I%20released%20an%20old%20Rails%20plugin%20as%20gem%20today.%20Slowly%20but%20surely%2C%20all%20my%20plugins%20will%20be%20converted%20to%20gems.%0D%0A%0D%0AThis%20time%20it%27s%20an%20old%20one%3A%20http_accept_language%0D%0A%0D%0A%0D%0ASplits%20the%20http-header%20into%20languages%20specified%20by%20the%20user%0D%0AReturns%20empty%20array%20if%20head" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2010%2F01%2Fhttp_accept_language-released-as-a-gem%2F&amp;title=http_accept_language%20released%20as%20a%20gem&amp;notes=I%20released%20an%20old%20Rails%20plugin%20as%20gem%20today.%20Slowly%20but%20surely%2C%20all%20my%20plugins%20will%20be%20converted%20to%20gems.%0D%0A%0D%0AThis%20time%20it%27s%20an%20old%20one%3A%20http_accept_language%0D%0A%0D%0A%0D%0ASplits%20the%20http-header%20into%20languages%20specified%20by%20the%20user%0D%0AReturns%20empty%20array%20if%20head" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2010%2F01%2Fhttp_accept_language-released-as-a-gem%2F&amp;t=http_accept_language%20released%20as%20a%20gem" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2010%2F01%2Fhttp_accept_language-released-as-a-gem%2F&amp;title=http_accept_language%20released%20as%20a%20gem&amp;annotation=I%20released%20an%20old%20Rails%20plugin%20as%20gem%20today.%20Slowly%20but%20surely%2C%20all%20my%20plugins%20will%20be%20converted%20to%20gems.%0D%0A%0D%0AThis%20time%20it%27s%20an%20old%20one%3A%20http_accept_language%0D%0A%0D%0A%0D%0ASplits%20the%20http-header%20into%20languages%20specified%20by%20the%20user%0D%0AReturns%20empty%20array%20if%20head" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2010%2F01%2Fhttp_accept_language-released-as-a-gem%2F&amp;title=http_accept_language%20released%20as%20a%20gem" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=http_accept_language%20released%20as%20a%20gem%20-%20http%3A%2F%2Fiain.nl%2F2010%2F01%2Fhttp_accept_language-released-as-a-gem%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/TWdHwWJDBWM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/01/http_accept_language-released-as-a-gem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/01/http_accept_language-released-as-a-gem/</feedburner:origLink></item>
		<item>
		<title>Basic Named Scopes</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/yL-yf1pZfAU/</link>
		<comments>http://iain.nl/2009/12/basic_named_scopes/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 17:26:43 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Gems]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[named_scope]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=581</guid>
		<description><![CDATA[Cal it tiny, I don&#8217;t care. I&#8217;ve made a gem named BasicNamedScopes.
Basic Usage
I was fed up with writing:
Post.all(:conditions => { :published => true },
  :select => :title, :include => :author)
So with BasicNamedScopes, you can now write:
Post.conditions(:published => true).select(:title).with(:author)
All named scopes are called the same, except for include, which is now called with, because include ]]></description>
			<content:encoded><![CDATA[<p>Cal it tiny, I don&#8217;t care. I&#8217;ve made a gem named <a href="http://github.com/iain/basic_named_scopes">BasicNamedScopes</a>.</p>
<h3>Basic Usage</h3>
<p>I was fed up with writing:</p>
<pre>Post.all(:conditions => { :published => true },
  :select => :title, :include => :author)</pre>
<p>So with BasicNamedScopes, you can now write:</p>
<pre>Post.conditions(:published => true).select(:title).with(:author)</pre>
<p>All named scopes are called the same, except for <tt>include</tt>, which is now called <tt>with</tt>, because <tt>include</tt> is a reserved method.</p>
<p>Reuse them by making class methods:</p>
<pre>
class Post < ActiveRecord::Base
  def self.published
    conditions(:published => true)
  end

  def self.visible
    conditions(:visible => true)
  end

  def self.index
    published.visible
  end
end</pre>
<p>Also, the <tt>all</tt>-method is a named scope now, so you can chain after callling <tt>all</tt>, for greater flexibility.</p>
<pre>Post.all.published</pre>
<p>Arrays can be used as multple parameters too, sparing you some brackets.</p>
<pre>Post.with(:author, :comments).conditions("name LIKE ?", query)</pre>
<p>The <tt>read_only</tt> and <tt>lock</tt> scopes default to true, but can be adjusted.</p>
<pre>Post.readonly         # => same as Post.all(:readonly => true)
Post.readonly(false)  # => same as Post.all(:readonly => false)</pre>
<h3>Why?</h3>
<p>NamedScopes are really handy and they should play a more central theme in ActiveRecord. While I heard that Rails 3 will support similar syntax, there is no reason to wait any longer.</p>
<p>I find defining named scopes very ugly, especially when dealing with parameters. Just compare the amount of curly braces!</p>
<pre># Using normal named scope:
named_scope :name_like,
  lambda { |query| { :conditions => ["name LIKE ?", query] } }

# Using BasicNamedScopes
def self.name_like(query)
  conditions("name LIKE ?", query)
end</pre>
<p>Also, regular named scopes don&#8217;t support using other named scopes at all!</p>
<p>I found myself implementing these named scopes (mostly conditions, but others too) so often, that a little gem like this would be the obvious choice. Use it if a gem like <a href="http://github.com/binarylogic/searchlogic">searchlogic</a> is overkill for your needs.</p>
<h3>Installing</h3>
<p>The gem is called &#8220;basic_named_scopes&#8221;. You know how to install it.</p>
<pre>gem install basic_named_scopes</pre>
<p>Use it in Rails:</p>
<pre>config.gem "basic_named_scopes"</pre>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fbasic_named_scopes%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=Basic%20Named%20Scopes&amp;body=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fbasic_named_scopes%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fbasic_named_scopes%2F&amp;title=Basic%20Named%20Scopes&amp;bodytext=Cal%20it%20tiny%2C%20I%20don%27t%20care.%20I%27ve%20made%20a%20gem%20named%20BasicNamedScopes.%0D%0A%0D%0ABasic%20Usage%0D%0A%0D%0AI%20was%20fed%20up%20with%20writing%3A%0D%0A%0D%0APost.all%28%3Aconditions%20%3D%3E%20%7B%20%3Apublished%20%3D%3E%20true%20%7D%2C%0D%0A%20%20%3Aselect%20%3D%3E%20%3Atitle%2C%20%3Ainclude%20%3D%3E%20%3Aauthor%29%0D%0A%0D%0ASo%20with%20BasicNamedScopes%2C%20you%20can%20now%20wri" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fbasic_named_scopes%2F&amp;title=Basic%20Named%20Scopes&amp;notes=Cal%20it%20tiny%2C%20I%20don%27t%20care.%20I%27ve%20made%20a%20gem%20named%20BasicNamedScopes.%0D%0A%0D%0ABasic%20Usage%0D%0A%0D%0AI%20was%20fed%20up%20with%20writing%3A%0D%0A%0D%0APost.all%28%3Aconditions%20%3D%3E%20%7B%20%3Apublished%20%3D%3E%20true%20%7D%2C%0D%0A%20%20%3Aselect%20%3D%3E%20%3Atitle%2C%20%3Ainclude%20%3D%3E%20%3Aauthor%29%0D%0A%0D%0ASo%20with%20BasicNamedScopes%2C%20you%20can%20now%20wri" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fbasic_named_scopes%2F&amp;t=Basic%20Named%20Scopes" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fbasic_named_scopes%2F&amp;title=Basic%20Named%20Scopes&amp;annotation=Cal%20it%20tiny%2C%20I%20don%27t%20care.%20I%27ve%20made%20a%20gem%20named%20BasicNamedScopes.%0D%0A%0D%0ABasic%20Usage%0D%0A%0D%0AI%20was%20fed%20up%20with%20writing%3A%0D%0A%0D%0APost.all%28%3Aconditions%20%3D%3E%20%7B%20%3Apublished%20%3D%3E%20true%20%7D%2C%0D%0A%20%20%3Aselect%20%3D%3E%20%3Atitle%2C%20%3Ainclude%20%3D%3E%20%3Aauthor%29%0D%0A%0D%0ASo%20with%20BasicNamedScopes%2C%20you%20can%20now%20wri" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fbasic_named_scopes%2F&amp;title=Basic%20Named%20Scopes" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Basic%20Named%20Scopes%20-%20http%3A%2F%2Fiain.nl%2F2009%2F12%2Fbasic_named_scopes%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/yL-yf1pZfAU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2009/12/basic_named_scopes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.nl/2009/12/basic_named_scopes/</feedburner:origLink></item>
		<item>
		<title>Cucumber 0.5 and my little commit</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/OAZwkmrSVK8/</link>
		<comments>http://iain.nl/2009/12/cucumber-0-5-and-my-little-commit/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 15:15:20 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=570</guid>
		<description><![CDATA[The strangest thing happened to me this week. I was working on a little side project at work. It seemed like a nice time to try out some new gems (Bundler and Devise: love it! InheritedResources, not that much, Formtastic, very nice). I was experimenting with Cucumber and writing Dutch features too.
I had figured that ]]></description>
			<content:encoded><![CDATA[<p>The strangest thing happened to me this week. I was working on a little side project at work. It seemed like a nice time to try out some new gems (<a href="http://gemcutter.org/gems/bundler">Bundler</a> and <a href="http://gemcutter.org/gems/devise">Devise</a>: love it! <a href="http://gemcutter.org/gems/inherited_resources">InheritedResources</a>, not that much, <a href="http://gemcutter.org/gems/formtastic">Formtastic</a>, very nice). I was experimenting with <a href="http://cukes.info">Cucumber</a> and writing Dutch features too.</p>
<p>I had figured that the supplied dutch keywords in <tt><a href="http://github.com/aslakhellesoy/cucumber/blob/master/lib/cucumber/languages.yml#L328-340">languages.yml</a></tt> were not very practical. It supplied &#8220;<em>Gegeven</em>&#8221; as a naive translation of &#8220;<em>Given</em>&#8220;. Although this is correctly translated, it is very unpractical to form Dutch sentences with it. The only to really start a sentence with &#8220;<em>Gegeven</em>&#8221; is to write &#8220;<em>Gegeven het feit dat&#8230;</em>&#8221; (&#8220;<em>Given the fact that&#8230;</em>&#8220;). It&#8217;s very strange to say &#8220;<em>Gegeven dat ik een profiel heb</em>&#8221; (&#8220;<em>Given I have a profile</em>&#8220;) in Dutch.</p>
<p>I decided to use the synonym &#8220;<em>Stel</em>&#8221; like in &#8220;<em>Stel ik heb een profiel</em>&#8220;. Not entirely correct either because it misses a comma after &#8220;<em>Stel</em>&#8220;, but much better, in my honest opinion. So I forked cucumber, changed <tt>languages.yml</tt> and used this as custom git repository in my <tt>Gemfile</tt>.</p>
<p>Apparently I was in the middle of a Release Candidate, so what I had committed on github. The Rails integration was extracted out in that version (like RSpec does) to the &#8220;<a href="http://github.com/aslakhellesoy/cucumber-rails">cucumber-rails</a>&#8221; gem. This gave about an evening of confusion, but I got it to work eventually.</p>
<p>Two days later, we had visitors at our company, from another Ruby company, talking about cooperation on future projects. I introduced myself and he was trying to remember if he&#8217;d seen anything on the interwebs by me. That seems to be common. When meeting people on conferences it&#8217;s always the same question: &#8220;Do I know any of your gems?&#8221;.</p>
<p>Anyway, he had actually read my name recently, namely in the commit log of cucumber. Without asking or sending a pull request, they had added my commit to the 0.5 release of cucumber. A pleasant surprise! Aslak Hellesøy, you&#8217;re a very observant <a href="http://github.com/aslakhellesoy">GitHub user</a>!</p>
<p>By the way, you can still use &#8220;<em>Gegeven</em>&#8220;, as it is just an alias.</p>
<p>I am constantly wrestling with the best way write cucumber features down. Does anyone have Dutch features? Do you write them down with your customers or are they for developers only? Can you share some of them? Here are some of mine:</p>
<pre>

Scenario: Inloggen
  Stel ik ben uitgelogd
  En ik heb een account voor "gebruiker@test.com" met het wachtwoord "geheim"
  En ik ben op de inlogpagina
  Als ik de volgende velden invul:
    | E-mailadres | gebruiker@test.com  |
    | Wachtwoord  | geheim              |
  En ik op "Inloggen" druk
  Dan zie ik de melding "Je bent ingelogd"

Scenario: Verkeerd wachtwoord
  Stel ik ben uitgelogd
  En ik heb een account voor "gebruiker@test.com" met het wachtwoord "geheim"
  En ik ben op de inlogpagina
  Als ik de volgende velden invul:
    | E-mailadres | gebruiker@test.com  |
    | Wachtwoord  | verkeerd            |
  En ik op "Inloggen" druk
  Dan zie ik de foutmelding "Ongeldig e-mailadres of wachtwoord"
</pre>
<p><img src="http://iain.nl/wp-content/uploads/2009/12/achbd-150x150.jpg" alt="The RSpec Book" title="The RSpec Book" width="150" height="150" class="alignright size-thumbnail wp-image-575" />By the way, don&#8217;t forget to order the <a href="http://pragprog.com/titles/achbd/the-rspec-book">RSpec and Friends book</a>. It&#8217;ll be released this februari and is a really good read if you want to learn BDD, RSpec or Cucumber.</p>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fcucumber-0-5-and-my-little-commit%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=Cucumber%200.5%20and%20my%20little%20commit&amp;body=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fcucumber-0-5-and-my-little-commit%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fcucumber-0-5-and-my-little-commit%2F&amp;title=Cucumber%200.5%20and%20my%20little%20commit&amp;bodytext=The%20strangest%20thing%20happened%20to%20me%20this%20week.%20I%20was%20working%20on%20a%20little%20side%20project%20at%20work.%20It%20seemed%20like%20a%20nice%20time%20to%20try%20out%20some%20new%20gems%20%28Bundler%20and%20Devise%3A%20love%20it%21%20InheritedResources%2C%20not%20that%20much%2C%20Formtastic%2C%20very%20nice%29.%20I%20was%20experimen" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fcucumber-0-5-and-my-little-commit%2F&amp;title=Cucumber%200.5%20and%20my%20little%20commit&amp;notes=The%20strangest%20thing%20happened%20to%20me%20this%20week.%20I%20was%20working%20on%20a%20little%20side%20project%20at%20work.%20It%20seemed%20like%20a%20nice%20time%20to%20try%20out%20some%20new%20gems%20%28Bundler%20and%20Devise%3A%20love%20it%21%20InheritedResources%2C%20not%20that%20much%2C%20Formtastic%2C%20very%20nice%29.%20I%20was%20experimen" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fcucumber-0-5-and-my-little-commit%2F&amp;t=Cucumber%200.5%20and%20my%20little%20commit" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fcucumber-0-5-and-my-little-commit%2F&amp;title=Cucumber%200.5%20and%20my%20little%20commit&amp;annotation=The%20strangest%20thing%20happened%20to%20me%20this%20week.%20I%20was%20working%20on%20a%20little%20side%20project%20at%20work.%20It%20seemed%20like%20a%20nice%20time%20to%20try%20out%20some%20new%20gems%20%28Bundler%20and%20Devise%3A%20love%20it%21%20InheritedResources%2C%20not%20that%20much%2C%20Formtastic%2C%20very%20nice%29.%20I%20was%20experimen" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2009%2F12%2Fcucumber-0-5-and-my-little-commit%2F&amp;title=Cucumber%200.5%20and%20my%20little%20commit" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Cucumber%200.5%20and%20my%20little%20commit%20-%20http%3A%2F%2Fiain.nl%2F2009%2F12%2Fcucumber-0-5-and-my-little-commit%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/OAZwkmrSVK8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2009/12/cucumber-0-5-and-my-little-commit/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://iain.nl/2009/12/cucumber-0-5-and-my-little-commit/</feedburner:origLink></item>
		<item>
		<title>Prawn and controller tests</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/kxswj0tcigk/</link>
		<comments>http://iain.nl/2009/11/prawn-and-controller-tests/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 09:33:44 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Gems]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[prawn]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=560</guid>
		<description><![CDATA[There is a real annoying gotcha in using controller tests to test an action that renders a pdf with Prawn. You&#8217;ll get a NoMethodException on &#8220;nil.downcase&#8221;. The troubling part is that it totally puts you off by providing the wrong lines and backtrace.
This has been mentioned somewhere on some mailinglists, but to make it a ]]></description>
			<content:encoded><![CDATA[<p><img src="http://iain.nl/wp-content/uploads/2009/11/prawn_logo-150x150.png" alt="prawn_logo" title="prawn_logo" width="150" height="150" class="alignright size-thumbnail wp-image-563" />There is a real annoying gotcha in using controller tests to test an action that renders a pdf with <a href="http://prawn.majesticseacreature.com/">Prawn</a>. You&#8217;ll get a NoMethodException on &#8220;nil.downcase&#8221;. The troubling part is that it totally puts you off by providing the wrong lines and backtrace.</p>
<p>This has been mentioned somewhere on some <a href="http://groups.google.com/group/prawn-ruby/browse_thread/thread/a44c7647894d165c">mailinglists</a>, but to make it a bit more findable, I&#8217;d thought I&#8217;d post it here.</p>
<p>The <s>solution</s>workaround is to set the server protocol, like this:</p>
<pre class="brush: ruby">
    it &quot;should show the pdf&quot; do
      request.env[&quot;SERVER_PROTOCOL&quot;] = &quot;http&quot;
      get :show, :id =&gt; &quot;report&quot;, &quot;format&quot; =&gt; &quot;pdf&quot;
      response.should render_template(:show)
    end
</pre>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2009%2F11%2Fprawn-and-controller-tests%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=Prawn%20and%20controller%20tests&amp;body=http%3A%2F%2Fiain.nl%2F2009%2F11%2Fprawn-and-controller-tests%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2009%2F11%2Fprawn-and-controller-tests%2F&amp;title=Prawn%20and%20controller%20tests&amp;bodytext=There%20is%20a%20real%20annoying%20gotcha%20in%20using%20controller%20tests%20to%20test%20an%20action%20that%20renders%20a%20pdf%20with%20Prawn.%20You%27ll%20get%20a%20NoMethodException%20on%20%22nil.downcase%22.%20The%20troubling%20part%20is%20that%20it%20totally%20puts%20you%20off%20by%20providing%20the%20wrong%20lines%20and%20backtrace" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2009%2F11%2Fprawn-and-controller-tests%2F&amp;title=Prawn%20and%20controller%20tests&amp;notes=There%20is%20a%20real%20annoying%20gotcha%20in%20using%20controller%20tests%20to%20test%20an%20action%20that%20renders%20a%20pdf%20with%20Prawn.%20You%27ll%20get%20a%20NoMethodException%20on%20%22nil.downcase%22.%20The%20troubling%20part%20is%20that%20it%20totally%20puts%20you%20off%20by%20providing%20the%20wrong%20lines%20and%20backtrace" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2009%2F11%2Fprawn-and-controller-tests%2F&amp;t=Prawn%20and%20controller%20tests" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2009%2F11%2Fprawn-and-controller-tests%2F&amp;title=Prawn%20and%20controller%20tests&amp;annotation=There%20is%20a%20real%20annoying%20gotcha%20in%20using%20controller%20tests%20to%20test%20an%20action%20that%20renders%20a%20pdf%20with%20Prawn.%20You%27ll%20get%20a%20NoMethodException%20on%20%22nil.downcase%22.%20The%20troubling%20part%20is%20that%20it%20totally%20puts%20you%20off%20by%20providing%20the%20wrong%20lines%20and%20backtrace" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2009%2F11%2Fprawn-and-controller-tests%2F&amp;title=Prawn%20and%20controller%20tests" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Prawn%20and%20controller%20tests%20-%20http%3A%2F%2Fiain.nl%2F2009%2F11%2Fprawn-and-controller-tests%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/kxswj0tcigk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2009/11/prawn-and-controller-tests/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iain.nl/2009/11/prawn-and-controller-tests/</feedburner:origLink></item>
		<item>
		<title>Configuring Autotest</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/dqQRQUiB0Cg/</link>
		<comments>http://iain.nl/2009/10/configuring-autotest/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 15:30:30 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[autospec]]></category>
		<category><![CDATA[autotest]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[fsevent]]></category>
		<category><![CDATA[green]]></category>
		<category><![CDATA[growl]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[red]]></category>
		<category><![CDATA[refactor]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=521</guid>
		<description><![CDATA[I have a big test suite in the current Rails application I&#8217;m working on. I have 2340 examples in RSpec, taking over 2 minutes to run. This is an absolute pain to run. Luckily there is autotest (or autospec if you&#8217;re running RSpec, like I am), which tests only the changed files. I&#8217;ve grown to ]]></description>
			<content:encoded><![CDATA[<p>I have a big test suite in the current Rails application I&#8217;m working on. I have 2340 examples in RSpec, taking over 2 minutes to run. This is an absolute pain to run. Luckily there is autotest (or autospec if you&#8217;re running RSpec, like I am), which tests only the changed files. I&#8217;ve grown to be totally dependent on this behavior, and I can&#8217;t imagine programming without it anymore.</p>
<p>I also do TDD, which means that I write a failing test first, and then program until it passes. But Autotest&#8217;s flow is that, once you&#8217;ve fixed a failing test or spec, it reruns the entire the suite to see if you&#8217;re solution doesn&#8217;t have any side effects. Normally this is fine, but with this kind of test suite, I cannot afford to wait for it to complete.</p>
<p>So, after going through Autotest&#8217;s code, I&#8217;ve decided to stub out this behavior. You can still trigger a complete rerun of the entire suite by pressing Ctrl+C, but it doesn&#8217;t do that every time you go green. It&#8217;s a bit of a monkey patch, but it works just right.</p>
<p>The autotest-growl gem clears the terminal. I don&#8217;t like that, because I like to see a bit of history. That&#8217;s why I changed that behavior too.</p>
<p>Here&#8217;s my <tt>~/.autotest</tt> file:</p>
<pre class="brush: ruby">
# Use file system hooks on OS X
require &#039;autotest/fsevent&#039;

# Don&#039;t run entire test suite when going from red to green
class Autotest
  def tainted
    false
  end
end

# Use Growl support
require &#039;autotest/growl&#039;

# Don&#039;t clear the terminal, when using Growl
module Autotest::Growl
  @@clear_terminal = false
end
</pre>
<p>While browsing through the code of Autotest I also found that it also looks for a <tt>.autotest</tt> file in the current working directory. So if you want to apply these changes to one project only, you can define this file locally for the project. I didn&#8217;t know that!</p>
<div id="attachment_523" class="wp-caption alignnone" style="width: 510px"><img src="http://iain.nl/wp-content/uploads/2009/10/red_green_apples.jpg" alt="Red -> Green -> Eat&#8230; eh&#8230; Refactor&#8221; title=&#8221;red_green_apples&#8221; width=&#8221;500&#8243; height=&#8221;206&#8243; class=&#8221;size-full wp-image-523&#8243; /><p class="wp-caption-text">Red -> Green -> Eat... eh... Refactor</p></div>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2009%2F10%2Fconfiguring-autotest%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=Configuring%20Autotest&amp;body=http%3A%2F%2Fiain.nl%2F2009%2F10%2Fconfiguring-autotest%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2009%2F10%2Fconfiguring-autotest%2F&amp;title=Configuring%20Autotest&amp;bodytext=I%20have%20a%20big%20test%20suite%20in%20the%20current%20Rails%20application%20I%27m%20working%20on.%20I%20have%202340%20examples%20in%20RSpec%2C%20taking%20over%202%20minutes%20to%20run.%20This%20is%20an%20absolute%20pain%20to%20run.%20Luckily%20there%20is%20autotest%20%28or%20autospec%20if%20you%27re%20running%20RSpec%2C%20like%20I%20am%29%2C%20which%20t" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2009%2F10%2Fconfiguring-autotest%2F&amp;title=Configuring%20Autotest&amp;notes=I%20have%20a%20big%20test%20suite%20in%20the%20current%20Rails%20application%20I%27m%20working%20on.%20I%20have%202340%20examples%20in%20RSpec%2C%20taking%20over%202%20minutes%20to%20run.%20This%20is%20an%20absolute%20pain%20to%20run.%20Luckily%20there%20is%20autotest%20%28or%20autospec%20if%20you%27re%20running%20RSpec%2C%20like%20I%20am%29%2C%20which%20t" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2009%2F10%2Fconfiguring-autotest%2F&amp;t=Configuring%20Autotest" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2009%2F10%2Fconfiguring-autotest%2F&amp;title=Configuring%20Autotest&amp;annotation=I%20have%20a%20big%20test%20suite%20in%20the%20current%20Rails%20application%20I%27m%20working%20on.%20I%20have%202340%20examples%20in%20RSpec%2C%20taking%20over%202%20minutes%20to%20run.%20This%20is%20an%20absolute%20pain%20to%20run.%20Luckily%20there%20is%20autotest%20%28or%20autospec%20if%20you%27re%20running%20RSpec%2C%20like%20I%20am%29%2C%20which%20t" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2009%2F10%2Fconfiguring-autotest%2F&amp;title=Configuring%20Autotest" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Configuring%20Autotest%20-%20http%3A%2F%2Fiain.nl%2F2009%2F10%2Fconfiguring-autotest%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/dqQRQUiB0Cg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2009/10/configuring-autotest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.nl/2009/10/configuring-autotest/</feedburner:origLink></item>
		<item>
		<title>Plugin release: Root table</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/SsYtXE2Pj14/</link>
		<comments>http://iain.nl/2009/07/root-table/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 15:05:20 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Rails Plugins]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[acts as list]]></category>
		<category><![CDATA[dropdown]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[rails engine]]></category>
		<category><![CDATA[scaffold]]></category>
		<category><![CDATA[select box]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=466</guid>
		<description><![CDATA[Yes, I&#8217;ve written another plugin for Rails. It&#8217;s about so called root tables. It seem to be making them often. I want a list with options to choose from and some easy way to manage that list, which is a tedious task. That&#8217;s why I made a plugin to do this for me.
What I have ]]></description>
			<content:encoded><![CDATA[<p>Yes, I&#8217;ve written another plugin for Rails. It&#8217;s about so called root tables. It seem to be making them often. I want a list with options to choose from and some easy way to manage that list, which is a tedious task. That&#8217;s why I made a <a href="http://github.com/iain/root_table">plugin</a> to do this for me.</p>
<h3>What I have so far is:</h3>
<ul>
<li>Automatic validations and relations</li>
<li>Completely configurable, with sensible defaults</li>
<li>A management interface</li>
<li>Works with <a href="http://github.com/rails/acts_as_list">acts_as_list</a> and supports drag and drop sorting</li>
<li>I18n support</li>
</ul>
<p><span id="more-466"></span></p>
<h3>Let&#8217;s take a tour of it&#8217;s usage.</h3>
<p>Install acts_as_list, if you want to:</p>
<pre class="brush: bash">./script/plugin install git://github.com/rails/acts_as_list.git</pre>
<p>Install root_table:</p>
<pre class="brush: bash">./script/plugin install git://github.com/iain/root_table.git</pre>
<p>Make a model that needs a list to choose from, like a product that has a category:</p>
<pre class="brush: bash">./script/genetate model Product name:string category_id:integer</pre>
<p>Make a model for the root table, category:</p>
<pre class="brush: bash">./script/generate model Category name:string position:integer</pre>
<p>Make category a root table for product:</p>
<pre class="brush: ruby">class Category &lt; ActiveRecord::Base
  root_table_for :product
end
</pre>
<p>And let the product model know as well what is happening:</p>
<pre class="brush: ruby">class Product &lt; ActiveRecord::Base
  has_root_table :category
end</pre>
<p>Let&#8217;s add a configured root table for good measure. Let&#8217;s convert the User-model to a root table. User is not a list, and doesn&#8217;t have a <tt>name</tt>-field to recognize it, nor does it have a position field. It requires some options to make it work.</p>
<pre class="brush: ruby">class User &lt; ActiveRecord::Base
  root_table_for :product, :to =&gt; :last_edited_by,
      :validate =&gt; false, :field =&gt; :login
end</pre>
<p>And update the product model again:</p>
<pre class="brush: ruby">
class Product &lt; ActiveRecord::Base
  has_root_table :category
  has_root_table :user
end</pre>
<p>The relation is called <tt>last_edited_by</tt>, it doesn&#8217;t add any validations and the displayed (and thus sorted) field is <tt>login</tt> instead of <tt>name</tt>. It doesn&#8217;t have position field, nor do I provide it, so it&#8217;ll sort on <tt>login</tt> and won&#8217;t be manually sortable, as we&#8217;ll se in a bit.</p>
<p><small>Using the User model might not be the best example, but you&#8217;ll get the point.</small></p>
<h3>Management in a Rails Engine</h3>
<p>Visit the management interface at <tt>http://localhost:3000/root_tables</tt> and see something like this:</p>
<p><img src="http://iain.nl/wp-content/uploads/2009/07/root_tables_path.png" alt="root_tables_path" title="root_tables_path" width="508" height="339" class="alignnone size-full wp-image-478" /></p>
<p>Since User has no position field, you&#8217;ll end up with a simple scaffold-like management screen for it:</p>
<p><img src="http://iain.nl/wp-content/uploads/2009/07/root_table_contents_pathuser.png" alt="root_table_contents_path(user)" title="root_table_contents_path(user)" width="508" height="339" class="alignnone size-full wp-image-479" /></p>
<p>However, the Category model does have a position, so the interface has drag and drop functionality:</p>
<p><img src="http://iain.nl/wp-content/uploads/2009/07/root_table_contents_pathcategory.png" alt="root_table_contents_path(category)" title="root_table_contents_path(category)" width="508" height="339" class="alignnone size-full wp-image-480" /></p>
<p>It has a very simple new and edit screen, just as you would with scaffold:</p>
<p><img src="http://iain.nl/wp-content/uploads/2009/07/edit_root_table_contents_path.png" alt="edit_root_table_contents_path" title="edit_root_table_contents_path" width="508" height="339" class="alignnone size-full wp-image-481" /></p>
<p>This is all done with a layout that Rails scaffold generates. If you have your own layout it might look completely different. The management pages are a Rails engine. This means that you can override any file by creating a file with the same name in your <tt>app</tt>-directory. Have a look at the code to see which files you can override.</p>
<p>Also, to enable the drag and drop interface, you&#8217;ll need to have prototype included. If you don&#8217;t want prototype be loaded everywhere, I&#8217;ve made it so that only drag and drop pages load the javascript. Please add <tt><%= yield(:head) %></tt> to your html header.</p>
<p>You can also override views on a table basis. That means that you might want a view that is different just in case of one table. The form for new users might want to have more fields than just the login field, but also include password fields. Create a view for that in <tt>app/views/root_table_contents/new_user.html.erb</tt>. Available for override per model are index, new and edit.</p>
<h3>Using it elsewhere</h3>
<p>But we&#8217;re not there yet. Now we&#8217;ve built a root table and filled it, we need to use it somewhere. The plugin provides a <tt>root_table_select</tt> helper, that renders a drop down. The first parameter is the root_tables (real) name, it figures the rest out automatically. You can pass any options as the options you would pass the other select helpers. Here&#8217;s an example:</p>
<pre class="brush: ruby">
&lt;% form_for @product do |f| %&gt;
  &lt;p&gt;
    &lt;%= f.label :category_id %&gt;&lt;br /&gt;
    &lt;%= f.root_table_select :category, :include_blank =&gt; true %&gt;
  &lt;/p&gt;
&lt;% end %&gt;
</pre>
<p>Also for showing a delegate method is provided. This is how you can show the category name:</p>
<pre class="brush: ruby">@product.category_name</pre>
<p>That about wraps it up for <a href="http://github.com/iain/root_table">root_table</a>. Please provide me with feedback and report any bugs and improvements. You can use the comments on my blog, or the <a href="http://github.com/iain/root_table/issues">issues page on github</a>.</p>
<h2>Update!</h2>
<p>I&#8217;ve made some tiny updates, most importantly reducing the amount of magic. Rails does a very nice job of lazy loading your models which can lead to some strange errors with the previous version of my plugin. These should be fixed now.</p>
<h3>Lessons learned:</h3>
<ul>
<li>A model does not know another model exist in development environment. Mentioning a model is enough to trigger Rails autoload and even constantizing a string works. Sweet!</li>
<li>Don&#8217;t require a model again during a request. Some models will break. I found this to be the case with the session class needed by AuthLogic. Again, to know for sure that a model has been loaded, simply mention it in your code, usually that is enough.</li>
<li>Tests are run in an environment very similar to production. I already knew that, but it&#8217;s worth mentioning that a stable development environment is also essential and you might not catch that with unit tests alone.</li>
</ul>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2009%2F07%2Froot-table%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=Plugin%20release%3A%20Root%20table&amp;body=http%3A%2F%2Fiain.nl%2F2009%2F07%2Froot-table%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2009%2F07%2Froot-table%2F&amp;title=Plugin%20release%3A%20Root%20table&amp;bodytext=Yes%2C%20I%27ve%20written%20another%20plugin%20for%20Rails.%20It%27s%20about%20so%20called%20root%20tables.%20It%20seem%20to%20be%20making%20them%20often.%20I%20want%20a%20list%20with%20options%20to%20choose%20from%20and%20some%20easy%20way%20to%20manage%20that%20list%2C%20which%20is%20a%20tedious%20task.%20That%27s%20why%20I%20made%20a%20plugin%20to%20do%20" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2009%2F07%2Froot-table%2F&amp;title=Plugin%20release%3A%20Root%20table&amp;notes=Yes%2C%20I%27ve%20written%20another%20plugin%20for%20Rails.%20It%27s%20about%20so%20called%20root%20tables.%20It%20seem%20to%20be%20making%20them%20often.%20I%20want%20a%20list%20with%20options%20to%20choose%20from%20and%20some%20easy%20way%20to%20manage%20that%20list%2C%20which%20is%20a%20tedious%20task.%20That%27s%20why%20I%20made%20a%20plugin%20to%20do%20" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2009%2F07%2Froot-table%2F&amp;t=Plugin%20release%3A%20Root%20table" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2009%2F07%2Froot-table%2F&amp;title=Plugin%20release%3A%20Root%20table&amp;annotation=Yes%2C%20I%27ve%20written%20another%20plugin%20for%20Rails.%20It%27s%20about%20so%20called%20root%20tables.%20It%20seem%20to%20be%20making%20them%20often.%20I%20want%20a%20list%20with%20options%20to%20choose%20from%20and%20some%20easy%20way%20to%20manage%20that%20list%2C%20which%20is%20a%20tedious%20task.%20That%27s%20why%20I%20made%20a%20plugin%20to%20do%20" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2009%2F07%2Froot-table%2F&amp;title=Plugin%20release%3A%20Root%20table" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Plugin%20release%3A%20Root%20table%20-%20http%3A%2F%2Fiain.nl%2F2009%2F07%2Froot-table%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/SsYtXE2Pj14" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2009/07/root-table/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://iain.nl/2009/07/root-table/</feedburner:origLink></item>
		<item>
		<title>Silencing Passenger</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/XSkswCcEE-E/</link>
		<comments>http://iain.nl/2009/06/silencing-passenger/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 17:33:48 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=460</guid>
		<description><![CDATA[When using Rails 2.3 and Passenger, you can do yourself a favor by adding this line to config/silencers/backtrace_silencer.rb

Rails.backtrace_cleaner.add_silencer { &#124;line&#124; line =~ /^\s*passenger/ }


Saves you scrolling through the endless backtraces passenger gives you for free  
PS. A colleague tweeted this lovely backtrace of a spring with grails error. I say: backtrace cleaner FTW!




	
	
	
	
	
	
	
	
	


]]></description>
			<content:encoded><![CDATA[<p>When using Rails 2.3 and Passenger, you can do yourself a favor by adding this line to <tt>config/silencers/backtrace_silencer.rb</tt></p>
<pre class="brush: ruby">
Rails.backtrace_cleaner.add_silencer { |line| line =~ /^\s*passenger/ }
</pre>
<p><img src="http://iain.nl/wp-content/uploads/2009/06/sssh.jpg" alt="sssh" title="sssh" width="200" height="171" class="alignright size-full wp-image-537" /><br />
Saves you scrolling through the endless backtraces passenger gives you for free <img src='http://iain.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>PS. A colleague tweeted this <a href="http://twitter.com/pascaldevink/status/2240565349">lovely backtrace</a> of a spring with grails error. I say: backtrace cleaner FTW!</p>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2009%2F06%2Fsilencing-passenger%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=Silencing%20Passenger&amp;body=http%3A%2F%2Fiain.nl%2F2009%2F06%2Fsilencing-passenger%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2009%2F06%2Fsilencing-passenger%2F&amp;title=Silencing%20Passenger&amp;bodytext=When%20using%20Rails%202.3%20and%20Passenger%2C%20you%20can%20do%20yourself%20a%20favor%20by%20adding%20this%20line%20to%20config%2Fsilencers%2Fbacktrace_silencer.rb%0D%0A%0D%0A%5Bsourcecode%20language%3D%27ruby%27%5D%0D%0ARails.backtrace_cleaner.add_silencer%20%7B%20%7Cline%7C%20line%20%3D%7E%20%2F%5E%5Cs%2Apassenger%2F%20%7D%0D%0A%5B%2Fsourcecode%5D%0D%0A%0D%0AS" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2009%2F06%2Fsilencing-passenger%2F&amp;title=Silencing%20Passenger&amp;notes=When%20using%20Rails%202.3%20and%20Passenger%2C%20you%20can%20do%20yourself%20a%20favor%20by%20adding%20this%20line%20to%20config%2Fsilencers%2Fbacktrace_silencer.rb%0D%0A%0D%0A%5Bsourcecode%20language%3D%27ruby%27%5D%0D%0ARails.backtrace_cleaner.add_silencer%20%7B%20%7Cline%7C%20line%20%3D%7E%20%2F%5E%5Cs%2Apassenger%2F%20%7D%0D%0A%5B%2Fsourcecode%5D%0D%0A%0D%0AS" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2009%2F06%2Fsilencing-passenger%2F&amp;t=Silencing%20Passenger" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2009%2F06%2Fsilencing-passenger%2F&amp;title=Silencing%20Passenger&amp;annotation=When%20using%20Rails%202.3%20and%20Passenger%2C%20you%20can%20do%20yourself%20a%20favor%20by%20adding%20this%20line%20to%20config%2Fsilencers%2Fbacktrace_silencer.rb%0D%0A%0D%0A%5Bsourcecode%20language%3D%27ruby%27%5D%0D%0ARails.backtrace_cleaner.add_silencer%20%7B%20%7Cline%7C%20line%20%3D%7E%20%2F%5E%5Cs%2Apassenger%2F%20%7D%0D%0A%5B%2Fsourcecode%5D%0D%0A%0D%0AS" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2009%2F06%2Fsilencing-passenger%2F&amp;title=Silencing%20Passenger" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Silencing%20Passenger%20-%20http%3A%2F%2Fiain.nl%2F2009%2F06%2Fsilencing-passenger%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/XSkswCcEE-E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2009/06/silencing-passenger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.nl/2009/06/silencing-passenger/</feedburner:origLink></item>
		<item>
		<title>Filtering with named scopes (encore)</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/I6A7RbP1txA/</link>
		<comments>http://iain.nl/2009/06/filtering-with-named-scopes-encore/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 16:17:40 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[named_scope]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=446</guid>
		<description><![CDATA[In my previous post, I talked about making filters using named scopes. To summorize:

I like the method of using a named_scope and delegating to specified filters. This way, you can structure your filters properly and get clean URLs. Also, you can chain other named scopes to the filter.
If you find yourself making an administrative web ]]></description>
			<content:encoded><![CDATA[<p>In my previous post, I talked about making filters using named scopes. To summorize:</p>
<blockquote><p>
I like the method of using a named_scope and delegating to specified filters. This way, you can structure your filters properly and get clean URLs. Also, you can chain other named scopes to the filter.</p></blockquote>
<p>If you find yourself making an administrative web application, with many tables and filters, here&#8217;s an example to make it a little more DRY.<br />
<span id="more-446"></span></p>
<h2>Making a partial</h2>
<p>First, make the filters a partial, in something like <tt>app/views/shared/_filters.html.haml</tt>.</p>
<pre class="brush: ruby">
%h3= t(model_name, :scope =&gt; :filter_titles)
%ul
  - model_class.available_filters.each do |filter|
    %li= link_to t(filter, :scope =&gt; [:filter_names, model_name]), url_for(params.merge(:filter =&gt; filter))
</pre>
<p>I&#8217;ve changed the translate-calls a bit, so they work with different models.</p>
<h2>A helper method</h2>
<p>Then, create a helper method:</p>
<pre class="brush: ruby">
def show_filters_for(model_name)
  render :partial =&gt; &quot;shared/filters&quot;, :locals =&gt; { :model_name =&gt; model_name, :model_class =&gt; model_name.to_s.camilze.constantize }
end
</pre>
<p>Now you can render the filters like this:</p>
<pre class="brush: ruby">
= show_filters_for :person
</pre>
<h2>And a module</h2>
<p>On the model side, you can make a module, probably in <tt>lib/chainable_filters.rb</tt>.</p>
<pre class="brush: ruby">
module ChainableFilters

  def self.extended(model)
    model.named_scope :filter, lambda { |f| model.available_filters.include?(f) ? model.send(&quot;filter_#{f}&quot;) : {} }
  end

  def available_filters
    self.methods.select { |m| m =~ /^filter_/ }.map { |m| m[7..-1].to_sym }
  end

end
</pre>
<p>Use it in a specific model, by extending with the module you just made:</p>
<pre class="brush: ruby">
class Person &lt; ActiveRecord::Base
  extend ChainableFilters
end
</pre>
<p>Or just every ActiveRecord class, by creating an initializer file (i.e. <tt>config/initializers/chainable_filters.rb</tt>):</p>
<pre class="brush: ruby">ActiveRecord::Base.extend ChainableFilters</pre>
<p>Now, that is some nice meta-programming, if you ask me! <img src='http://iain.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>




	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fiain.nl%2F2009%2F06%2Ffiltering-with-named-scopes-encore%2F&amp;partner=sociable" title="Print"><img src="http://iain.nl/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="mailto:?subject=Filtering%20with%20named%20scopes%20%28encore%29&amp;body=http%3A%2F%2Fiain.nl%2F2009%2F06%2Ffiltering-with-named-scopes-encore%2F" title="email"><img src="http://iain.nl/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fiain.nl%2F2009%2F06%2Ffiltering-with-named-scopes-encore%2F&amp;title=Filtering%20with%20named%20scopes%20%28encore%29&amp;bodytext=In%20my%20previous%20post%2C%20I%20talked%20about%20making%20filters%20using%20named%20scopes.%20To%20summorize%3A%0D%0A%0D%0A%0D%0AI%20like%20the%20method%20of%20using%20a%20named_scope%20and%20delegating%20to%20specified%20filters.%20This%20way%2C%20you%20can%20structure%20your%20filters%20properly%20and%20get%20clean%20URLs.%20Also%2C%20you%20ca" title="Digg"><img src="http://iain.nl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fiain.nl%2F2009%2F06%2Ffiltering-with-named-scopes-encore%2F&amp;title=Filtering%20with%20named%20scopes%20%28encore%29&amp;notes=In%20my%20previous%20post%2C%20I%20talked%20about%20making%20filters%20using%20named%20scopes.%20To%20summorize%3A%0D%0A%0D%0A%0D%0AI%20like%20the%20method%20of%20using%20a%20named_scope%20and%20delegating%20to%20specified%20filters.%20This%20way%2C%20you%20can%20structure%20your%20filters%20properly%20and%20get%20clean%20URLs.%20Also%2C%20you%20ca" title="del.icio.us"><img src="http://iain.nl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fiain.nl%2F2009%2F06%2Ffiltering-with-named-scopes-encore%2F&amp;t=Filtering%20with%20named%20scopes%20%28encore%29" title="Facebook"><img src="http://iain.nl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fiain.nl%2F2009%2F06%2Ffiltering-with-named-scopes-encore%2F&amp;title=Filtering%20with%20named%20scopes%20%28encore%29&amp;annotation=In%20my%20previous%20post%2C%20I%20talked%20about%20making%20filters%20using%20named%20scopes.%20To%20summorize%3A%0D%0A%0D%0A%0D%0AI%20like%20the%20method%20of%20using%20a%20named_scope%20and%20delegating%20to%20specified%20filters.%20This%20way%2C%20you%20can%20structure%20your%20filters%20properly%20and%20get%20clean%20URLs.%20Also%2C%20you%20ca" title="Google Bookmarks"><img src="http://iain.nl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fiain.nl%2F2009%2F06%2Ffiltering-with-named-scopes-encore%2F&amp;title=Filtering%20with%20named%20scopes%20%28encore%29" title="Reddit"><img src="http://iain.nl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://iain.nl/feed/" title="RSS"><img src="http://iain.nl/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Filtering%20with%20named%20scopes%20%28encore%29%20-%20http%3A%2F%2Fiain.nl%2F2009%2F06%2Ffiltering-with-named-scopes-encore%2F" title="Twitter"><img src="http://iain.nl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/><img src="http://feeds.feedburner.com/~r/iain-nl/~4/I6A7RbP1txA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2009/06/filtering-with-named-scopes-encore/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.nl/2009/06/filtering-with-named-scopes-encore/</feedburner:origLink></item>
	</channel>
</rss>
