<?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>Plataforma Tecnologia Blog » English</title>
	
	<link>http://blog.plataformatec.com.br</link>
	<description>O espaço da Plataforma Tecnologia para falar sobre Ruby, Ruby on Rails e engenharia de software.</description>
	<lastBuildDate>Wed, 28 Jul 2010 19:50:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/PlataformaBlog_english" /><feedburner:info uri="plataformablog_english" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>New Active Record scoping syntax</title>
		<link>http://feedproxy.google.com/~r/PlataformaBlog_english/~3/US5Megjn7X8/</link>
		<comments>http://blog.plataformatec.com.br/2010/07/new-active-record-scoping-syntax/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 15:19:46 +0000</pubDate>
		<dc:creator>José Valim</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[arel]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[scopes]]></category>

		<guid isPermaLink="false">http://blog.plataformatec.com.br/?p=1181</guid>
		<description><![CDATA[You probably know that Active Record got a facelift and is now powered by Active Relation. A new chainable-award-winning-lazy API was added and received great feedback! However, as more and more people are trying Rails 3 beta, a small incompatibility between the old and new syntax was found. This post explains this incompatibility and how [...]]]></description>
			<content:encoded><![CDATA[<p>You probably know that <a href="http://www.railsdispatch.com/posts/activerelation" target="_blank">Active Record got a facelift</a> and is now powered by Active Relation. A new chainable-award-winning-lazy API was added and received great feedback! However, as more and more people are trying Rails 3 beta, <a href="https://rails.lighthouseapp.com/projects/8994/tickets/3838" target="_blank">a small incompatibility between the old and new syntax was found</a>. This post explains this incompatibility and how it was solved.</p>
<h3>The issue</h3>
<p>Quoting the Lighthouse ticket, imagine the following scenario in Rails 2.3:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Page <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  default_scope <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:deleted_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">deleted</span>
    with_exclusive_scope <span style="color:#ff3333; font-weight:bold;">:find</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;pages.deleted_at IS NOT NULL&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      all
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>If you rewrite it to the new 3.0 syntax, your first attempt would probably be:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Page <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  default_scope where<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:deleted_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">deleted</span>
    with_exclusive_scope <span style="color:#ff3333; font-weight:bold;">:find</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> where<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'pages.deleted_at IS NOT NULL'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      all
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>However, if you try it out on console, you will find out it does not work as expected:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Page.<span style="color:#9900CC;">all</span>         <span style="color:#008000; font-style:italic;">#=&gt; SELECT &quot;pages&quot;.* FROM &quot;pages&quot; WHERE (&quot;pages&quot;.&quot;deleted_at&quot; IS NULL)</span>
Page.<span style="color:#9900CC;">deleted</span>.<span style="color:#9900CC;">all</span> <span style="color:#008000; font-style:italic;">#=&gt; SELECT &quot;pages&quot;.* FROM &quot;pages&quot; WHERE (&quot;pages&quot;.&quot;deleted_at&quot; IS NULL) AND (&quot;pages&quot;.&quot;deleted_at&quot; IS NOT NULL)</span></pre></div></div>

<p>To understand why it does not work, let&#8217;s take a look at the source code!</p>
<h3>Investigating the issue</h3>
<p>With Active Relation, Active Record is no longer responsible to build queries. That said, <code>ActiveRecord::Base</code> is not the one that implements <code>where()</code> and friends, in fact, it simply delegates to an <code>ActiveRecord::Relation</code> object. From <a href="http://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb" target="_blank">ActiveRecord::Base source code</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">delegate <span style="color:#ff3333; font-weight:bold;">:select</span>, <span style="color:#ff3333; font-weight:bold;">:group</span>, <span style="color:#ff3333; font-weight:bold;">:order</span>, <span style="color:#ff3333; font-weight:bold;">:limit</span>, <span style="color:#ff3333; font-weight:bold;">:joins</span>, <span style="color:#ff3333; font-weight:bold;">:where</span>, <span style="color:#ff3333; font-weight:bold;">:preload</span>, <span style="color:#ff3333; font-weight:bold;">:eager_load</span>, <span style="color:#ff3333; font-weight:bold;">:includes</span>, <span style="color:#ff3333; font-weight:bold;">:from</span>, <span style="color:#ff3333; font-weight:bold;">:lock</span>, <span style="color:#ff3333; font-weight:bold;">:readonly</span>, <span style="color:#ff3333; font-weight:bold;">:having</span>, <span style="color:#ff3333; font-weight:bold;">:create_with</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:scoped</span></pre></div></div>

<p>And the <code>scoped</code> implementation is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> scoped<span style="color:#006600; font-weight:bold;">&#40;</span>options = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> options.<span style="color:#9900CC;">present</span>?
    scoped.<span style="color:#9900CC;">apply_finder_options</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    current_scoped_methods ? relation.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_scoped_methods<span style="color:#006600; font-weight:bold;">&#41;</span> : relation.<span style="color:#9900CC;">clone</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> relation
  <span style="color:#0066ff; font-weight:bold;">@relation</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Relation</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, arel_table<span style="color:#006600; font-weight:bold;">&#41;</span>
  finder_needs_type_condition? ? <span style="color:#0066ff; font-weight:bold;">@relation</span>.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span>type_condition<span style="color:#006600; font-weight:bold;">&#41;</span> : <span style="color:#0066ff; font-weight:bold;">@relation</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>As you can see, <code>scoped</code> always returns an ActiveRecord::Relation that you build your query on top of (notice that <a href="http://metautonomo.us/2010/05/11/activerecord-relation-vs-arel/">ARel::Relation is not the same as ActiveRecord::Relation</a>).</p>
<p>Besides, if there is any <code>current_scoped_methods</code>, the <code>scoped</code> method is responsible to merge this current scope into the raw relation. This is where things get interesting.</p>
<p>When you create your model, <code>current_scoped_methods</code> returns by default nil. However, when you define a <code>default_scope</code>, the current scope now becomes the relation given to <code>default_scope</code>, meaning that, every time you call <code>scoped</code>, it returns the raw relation merged with your default scope.</p>
<p>The whole idea of <code>with_exclusive_scope</code> is to be able to make a query without taking the default scope into account, just the relation you give in as argument. That said, it basically sets the <code>current_scope_methods</code> back to nil, so every time you call <code>scoped</code> to build your queries, it will be built on top of the raw relation without the default scope.</p>
<p>With that in mind, if we look again at the code which we were trying to port from Rails 2.3, we can finally understand what was happening:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">deleted</span>
  with_exclusive_scope <span style="color:#ff3333; font-weight:bold;">:find</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> where<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'pages.deleted_at IS NOT NULL'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>When we called <code>where('pages.deleted_at IS NOT NULL')</code> above, we were doing the same as: <code>scoped.where('pages.deleted_at IS NOT NULL')</code>. But, as <code>scoped</code> was called <strong>outside</strong> the <code>with_exclusive_scope</code> block, it means that the relation given as argument to <code>:find</code> was built on top of <code>default_scope</code> explaining the query we saw as results.</p>
<p>For example, the following syntax would work as expected:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">deleted</span>
  with_exclusive_scope <span style="color:#9966CC; font-weight:bold;">do</span>
    where<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'pages.deleted_at IS NOT NULL'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">all</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Since we are calling <code>where</code> inside the block, the <code>scoped</code> method no longer takes the default scope into account. However, moving the relation inside the block is not the same as specifying it to <code>:find</code>, because if we were doing three queries inside the block, we would have to specify the same relation three times (or refactor the whole code to always do a query on top of this new relation).</p>
<p>That said, it seems the previous <code>with_exclusive_scope</code> syntax does not suit very well with ActiveRecord&#8217;s new API. Maybe is it time for change? Can we provide a better API? Which are the use cases?</p>
<h3>Identifying the use cases</h3>
<p>The <code>with_exclusive_scope</code> method has mainly two use cases. The first one, which we just discussed above, is to allow us to make a query without taking the default scope into account inside our models:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">deleted</span>
  with_exclusive_scope <span style="color:#9966CC; font-weight:bold;">do</span>
    where<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'pages.deleted_at IS NOT NULL'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">all</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>While this code looks ok, if we think about relations, we will realize that we don&#8217;t need to give a block to achieve the behavior we want. If the <code>scoped</code> method returns a raw relation with the default scope, couldn&#8217;t we have a method that always returns the raw relation? Allowing us to build our query without taking the default scope into account?</p>
<p>In fact, this method was already implemented in Active Record and it is called <code>unscoped</code>. That said, the code above could simply be rewritten as:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">deleted</span>
  unscoped.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'pages.deleted_at IS NOT NULL'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">all</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Much simpler! So, it seems that we don&#8217;t need to support the block usage at all, confirm?</p>
<p>Deny! Going back to the <code>Page</code> example above, it seems we should never see deleted pages, that&#8217;s why we set the <code>default_scope</code> to <code>:deleted_at => nil</code>. However, if this application has an admin section, the admin may want to see all pages, including the deleted ones.</p>
<p>That said, what we could do is to have one controller for the normal User and another for the Admin. In the former, we would always use <code>Page.all</code>, and <code>Page.unscoped.all</code> in the latter.</p>
<p>However, if these controllers and views are very similar, you may not want do duplicate everything. Maybe it would be easier if we do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> resource_class
  <span style="color:#9966CC; font-weight:bold;">if</span> current_user.<span style="color:#9900CC;">is_admin</span>?
    Page.<span style="color:#9900CC;">unscoped</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    Page
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And, instead of always referencing the <code>Page</code> class directly in our actions, we could call <code>resource_class</code>. While this solution is also ok, there is a final alternative, that would require no changes to the current code. If you want to use the same controller for different roles, but changing the scope of what they are allowed to see, you could simply use an <code>around_filter</code> to change the model scope during the execution of an action. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> PagesController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  around_filter <span style="color:#ff3333; font-weight:bold;">:apply_scope</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># some code ...</span>
&nbsp;
  protected
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> apply_scope
    <span style="color:#9966CC; font-weight:bold;">if</span> current_user.<span style="color:#9900CC;">admin</span>?
      Page.<span style="color:#9900CC;">with_exclusive_scope</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">yield</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#9966CC; font-weight:bold;">yield</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That said, being allowed to give a block to <code>with_exclusive_scope</code> is actually useful and since we want to deprecate <code>with_exclusive_scope</code> in favor of <code>unscoped</code> in the future, we brought this very same syntax to <code>unscoped</code> as well:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> apply_scope
  <span style="color:#9966CC; font-weight:bold;">if</span> current_user.<span style="color:#9900CC;">admin</span>?
    Page.<span style="color:#9900CC;">unscoped</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">yield</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#9966CC; font-weight:bold;">yield</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Tidying it up</h3>
<p>Well, after the behavior in <code>with_exclusive_scope</code> was properly ported to the new API, we need to be sure we are not forgetting about anything&#8230; wait, actually we are.</p>
<p><code>with_exclusive_scope</code> has an evil twin brother called <code>with_scope</code> which behaves very similarly, except that it always build the query on top of the <code>scoped</code> relation. It works like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Page <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  default_scope where<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:deleted_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Page.<span style="color:#9900CC;">with_scope</span> <span style="color:#ff3333; font-weight:bold;">:find</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  Page.<span style="color:#9900CC;">all</span> <span style="color:#008000; font-style:italic;">#=&gt; Bring all active pages that were not deleted</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>However, this feels way too hash-ish. Of course, we could use relations to make it a bit prettier:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Page.<span style="color:#9900CC;">with_scope</span> <span style="color:#ff3333; font-weight:bold;">:find</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> where<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  Page.<span style="color:#9900CC;">all</span> <span style="color:#008000; font-style:italic;">#=&gt; Bring all active pages that were not deleted</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This is ok, but it seems that we could improve it even more. That said, we added a new method to relations, called <code>scoping</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Page.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">scoping</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  Page.<span style="color:#9900CC;">all</span> <span style="color:#008000; font-style:italic;">#=&gt; Bring all active pages that were not deleted</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Yeah! Sign me up &#8217;cause this looks way better than the previous syntax! And, if you check <a href="http://github.com/rails/rails/commit/bd1666ad1de88598ed6f04ceffb8488a77be4385">the original commit</a>, you will notice the <code>unscoped</code> method with a block simply delegates <code>scoping</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> unscoped
  block_given? ? relation.<span style="color:#9900CC;">scoping</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">yield</span> <span style="color:#006600; font-weight:bold;">&#125;</span> : relation
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So, with <code>unscoped</code> and <code>scoping</code> implemented, we just need to commit, git push and be happy, confirm? Deny! There is one last case to check.</p>
<h3>create_with</h3>
<p>If you payed attention properly, you can notice that every time we called <code>with_exclusive_scope</code> and <code>with_scope</code>, we always passed <code>{ :find => relation }</code> as hash, instead of simply giving the relation. This happens because these methods accept two hash keys: find and create.</p>
<p>As you may expect, one specifies the behavior for create and the other for finding. In most of the cases, they are exactly the same and work with the new syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = Page.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">new</span>
page.<span style="color:#9900CC;">active</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></div></div>

<p>However, for obvious reasons, this only works if the conditions are given as a hash. Consider this case:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = Page.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;active = true&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">new</span>
page.<span style="color:#9900CC;">active</span> <span style="color:#008000; font-style:italic;">#=&gt; nil</span></pre></div></div>

<p>That said, there may be a few scenarios where you want to specify the creation conditions on its own, explaining the :find and :create options in <code>with_exclusive_scope</code> and <code>with_scope</code> methods. So, how can I achieve it with the new syntax? Easy!</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = Page.<span style="color:#9900CC;">create_with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">new</span>
page.<span style="color:#9900CC;">active</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></div></div>

<p>If you provide both conditions as a hash and <code>create_with</code>, <code>create_with</code> always have higher priority:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = Page.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">create_with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">new</span>
page.<span style="color:#9900CC;">active</span> <span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></div></div>

<p>Note this syntax already existed, we are just making it explicit now as part of the new API! That said, commit, push and be happy!</p>
<h3>Wrapping up</h3>
<p>All in all, <code>with_exclusive_scope</code> and <code>with_scope</code> are now part of the old ActiveRecord API giving place to the new, strong and vibrant <code>unscoped</code> and <code>scoping</code> methods!</p>
<p>However, they are not going to be deprecated now. They will follow the same <a href="http://m.onkey.org/2010/1/22/active-record-query-interface" target="_blank">deprecation strategy as all the current methods</a>.</p>
<p>And you? What do you think about this new scoping API?</p>
<img src="http://feeds.feedburner.com/~r/PlataformaBlog_english/~4/US5Megjn7X8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.plataformatec.com.br/2010/07/new-active-record-scoping-syntax/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://blog.plataformatec.com.br/2010/07/new-active-record-scoping-syntax/</feedburner:origLink></item>
		<item>
		<title>SimpleForm: forms made easy</title>
		<link>http://feedproxy.google.com/~r/PlataformaBlog_english/~3/KrHff_utCLI/</link>
		<comments>http://blog.plataformatec.com.br/2010/06/simpleform-forms-made-easy/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 20:01:53 +0000</pubDate>
		<dc:creator>Carlos Antônio</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[simple_form]]></category>

		<guid isPermaLink="false">http://blog.plataformatec.com.br/?p=1143</guid>
		<description><![CDATA[Sometime ago we were working on a project together with a designer, and that specific application was full of forms, each one having a different layout, but most of them sharing the same features: inline errors, hints, specific label markup for required fields, etc. To start prototyping the application faster, we used the markup the [...]]]></description>
			<content:encoded><![CDATA[<p>Sometime ago we were working on a project together with a designer, and that specific application was full of forms, each one having a different layout, but most of them sharing the same features: inline errors, hints, specific label markup for required fields, etc. To start prototyping the application faster, we used the markup the designer created with similar forms, duplicating the code. But we don&#8217;t like code duplication, we weren&#8217;t feeling comfortable with it. So we decided to move on and create a tool to help us, that should be flexible enough to let us define the markup that fits better for each application, or even no extra markup at all. Here is SimpleForm!</p>
<h3>SimpleForm inputs</h3>
<p>From the README:</p>
<blockquote><p>Forms made easy (for Rails)!</p>
<p>SimpleForm aims to be as flexible as possible while helping you with powerful components to create your forms. The basic goal of simple form is to not touch your way of defining the layout, letting you find the better design for your eyes. Good part of the DSL was inherited from Formtastic, which we are thankful for and should make you feel right at home.</p></blockquote>
<p>As the README says, <a href="http://github.com/plataformatec/simple_form" title="SimpleForm on Github">SimpleForm is a tool to help you build forms easily in Rails</a>. Let&#8217;s see some examples:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= simple_form_for <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:username</span>, <span style="color:#ff3333; font-weight:bold;">:label</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Your username please'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#ff3333; font-weight:bold;">:hint</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'No special characters.'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:remember_me</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:boolean</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">button</span> <span style="color:#ff3333; font-weight:bold;">:submit</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span></pre></div></div>

<p>There are plenty of things going on here: we create a form using <code>simple_form_for</code> helper, then we use the <code>:input</code> method to create input elements based on column type. For instance, <code>:username</code> will create a default text input, while <code>:password</code> attribute will render an input type password. For the <code>:username</code> attribute, we are specifying a label manually. For <code>:password</code>, the label will be taken from I18n, and we are adding a hint message to the field. For <code>:remember_me</code>, we are explicitly saying to render it as a checkbox, using the <code>:as => :boolean</code> option (that is the default for boolean attributes). Also, there is a <code>button</code> helper that simply delegates to Rails helpers, in this case <code>submit</code>.</p>
<p>The output for a new <code>@user</code> would be:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;/users&quot; class=&quot;simple_form user&quot; id=&quot;new_user&quot; method=&quot;post&quot;&gt;
  &lt;div class=&quot;input string required&quot;&gt;
    &lt;label class=&quot;string required&quot; for=&quot;user_username&quot;&gt;&lt;abbr title=&quot;required&quot;&gt;*&lt;/abbr&gt; Your username please&lt;/label&gt;
    &lt;input class=&quot;string required&quot; id=&quot;user_username&quot; maxlength=&quot;255&quot; name=&quot;user[username]&quot; size=&quot;50&quot; type=&quot;text&quot; /&gt;
  &lt;/div&gt; 
  &lt;div class=&quot;input password required&quot;&gt;
    &lt;label class=&quot;password required&quot; for=&quot;user_password&quot;&gt;&lt;abbr title=&quot;required&quot;&gt;*&lt;/abbr&gt; Password&lt;/label&gt;
    &lt;input class=&quot;password required&quot; id=&quot;user_password&quot; name=&quot;user[password]&quot; size=&quot;30&quot; type=&quot;password&quot; /&gt;
    &lt;span class=&quot;hint&quot;&gt;No special characters.&lt;/span&gt;
  &lt;/div&gt; 
  &lt;div class=&quot;input boolean optional&quot;&gt;
    &lt;label class=&quot;boolean optional&quot; for=&quot;user_remember_me&quot;&gt; Remember me&lt;/label&gt;
    &lt;input name=&quot;user[remember_me]&quot; type=&quot;hidden&quot; value=&quot;0&quot; /&gt;
    &lt;input class=&quot;boolean optional&quot; id=&quot;user_remember_me&quot; name=&quot;user[remember_me]&quot; type=&quot;checkbox&quot; value=&quot;1&quot; /&gt;
  &lt;/div&gt; 
  &lt;input id=&quot;user_submit&quot; name=&quot;commit&quot; type=&quot;submit&quot; value=&quot;Create User&quot; /&gt; 
&lt;/form&gt;</pre></div></div>

<p>You may have noticed there is some additional css classes added to the markup, like <code>string</code> and <code>required</code>. They are added automatically by SimpleForm to help us style and plug some javascript in. There are specific css classes for each available input type. Also, pay some attention to the <code>label</code>: inside it there is an <code>abbr</code> tag with an asterisk (*) showing that the field is required. SimpleForm uses the new validations reflection API from Rails 3 to check if the attribute has the <code>presence</code> validator, and mark the field as required if so. And we are able to say that a field is required or disable the required mark, by passing the option <code>:required => true|false</code>.</p>
<p>Furthermore, there is the <code>hint</code> tag for the <code>:password</code> attribute that SimpleForm creates based on the <code>:hint</code> option we have defined. Also notice that the gem has automatically added a <code>div</code> wrapper to each input, with the same css classes. SimpleForm allows us to configure this wrapper as well, using for instance <code>p</code> instead of <code>div</code>. We are going to see more about configuration later.</p>
<p>SimpleForm is already prepared to generate some of the new HTML 5 input tags, such as <code>email</code>, <code>url</code> and <code>number</code> inputs:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= simple_form_for <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:website</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:age</span>, <span style="color:#ff3333; font-weight:bold;">:hint</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;This defaults to 'number' input based on field type&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">button</span> <span style="color:#ff3333; font-weight:bold;">:submit</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span></pre></div></div>

<p>Based on the attribute name, SimpleForm will generate <code>url</code> or <code>email</code> input types, and we can always set a specific type with the <code>:as</code> option. Numeric attributes will always be rendered as input type number.</p>
<h3>Working with associations</h3>
<p>SimpleForm adds a custom and straightforward method to render select tags for associations, called <code>association</code>. For now, consider our <code>User</code> belongs to a <code>Company</code>, and has and belongs to many <code>Roles</code>. Let&#8217;s go straight to the example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= simple_form_for <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">association</span> <span style="color:#ff3333; font-weight:bold;">:company</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">association</span> <span style="color:#ff3333; font-weight:bold;">:roles</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">button</span> <span style="color:#ff3333; font-weight:bold;">:submit</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span></pre></div></div>

<p>It will detect the association type and render a <code>select</code> tag for choosing the company, listing all companies in the database, and another <code>select</code> for roles, with <code>multiple</code> option enabled. </p>
<p>SimpleForm also has some add-ons, letting us render associations as a collection of radios or check boxes. Using the same example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  f.<span style="color:#9900CC;">association</span> <span style="color:#ff3333; font-weight:bold;">:company</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:radio</span>
  f.<span style="color:#9900CC;">association</span> <span style="color:#ff3333; font-weight:bold;">:roles</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:check_boxes</span></pre></div></div>

<p>Now we are rendering a collection of radios for choosing the <code>Company</code>, and another collection of check boxes for choosing <code>Roles</code>.</p>
<h3>Configuration</h3>
<p>SimpleForm lets us do some customizations by running its install generator:</p>
<pre>
rails generate simple_form:install

# Output
  create  config/initializers/simple_form.rb
  create  config/locales/simple_form.en.yml
  create  lib/templates/erb/scaffold/_form.html.erb
</pre>
<p>As we can see, running this generator will copy an initializer file, responsible for configuring SimpleForm; a locale file, to let us change some I18n messages; and a form template inside our lib dir. This template will be used instead of the default Rails scaffold form template, so it will create our form already using SimpleForm. Easy, right?</p>
<p>Let&#8217;s take a look at some configuration options:</p>
<ul>
<li><strong>components</strong>: defines the components used by the form builder. We can remove any of them, change the order, or add new ones. Defaults to <code>[ :label, :input, :hint, :error ]</code>.</li>
<li><strong>hint_tag</strong>: tag used for hints, defaults to <code>span</code>.</li>
<li><strong>error_tag</strong>: tag used for errors, defaults to <code>span</code>.</li>
<li><strong>wrapper_tag</strong>: tag used as wrapper to all inputs, defaults to <code>div</code></li>
<li><strong>label_text</strong>: determines how the label text should be generated altogether with the required text. It must be a lambda/proc that receives both label and required texts. Defaults to <code>"required label"</code>.</li>
</ul>
<p>There are a lot more options available in the initializer file, such as default input size and priority countries for generating country selects. Also, the locale file lets us determine the required text and mark, or even the entire required html tag.</p>
<h3>Internationalization</h3>
<p>SimpleForm is ready for I18n, supporting <code>labels</code> and <code>hints</code>. In addition, it lets us set different content for each action, <code>new</code> and <code>edit</code>. Here is an example locale file:</p>
<pre>
  en:
    simple_form:
      labels:
        user:
          username: 'User name'
          password: 'Password'
          edit:
            username: 'Change user name'
            password: 'Change password'
      hints:
        user:
          username: 'User name to sign in.'
          password: 'No special characters, please.'
</pre>
<p>Simple, right? If it does not find any specific translation using I18n for the <code>label</code>, it will fallback to <code>human_attribute_name</code>.</p>
<h3>Here we go!</h3>
<p>SimpleForm has much more to offer. We would like to invite you to take a <a href="http://github.com/plataformatec/simple_form" title="SimpleForm on Github">better look at the examples and possibilities</a>. Remember, SimpleForm aims to be flexible and powerful to help you easily build forms, without saying how you should create your markup.</p>
<p>Also, feel free to explore the source code and extend SimpleForm even further. Since it&#8217;s based on components, creating a new component which moves the current hints to inside the input (using javascript or the new placehoder attribute in HTML 5), should be easy!</p>
<p>It&#8217;s worth saying SimpleForm is Rails 3 compatible in the master branch. If you are using Rails 2.3.x, <a href="http://github.com/plataformatec/simple_form/tree/v1.0" title="SimpleForm for Rails 2.3.x">there is a v1.0 branch and version</a> that you might want to take a look.</p>
<p>SimpleForm has been helping us a lot so far, we hope you enjoy it. Moreover, we would like to enjoy other tools that help your productivity day by day, please leave a comment and let us know, we would appreciate a lot!</p>
<img src="http://feeds.feedburner.com/~r/PlataformaBlog_english/~4/KrHff_utCLI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.plataformatec.com.br/2010/06/simpleform-forms-made-easy/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		<feedburner:origLink>http://blog.plataformatec.com.br/2010/06/simpleform-forms-made-easy/</feedburner:origLink></item>
		<item>
		<title>DSL or NoDSL at Euruko 2010</title>
		<link>http://feedproxy.google.com/~r/PlataformaBlog_english/~3/Z4LzLbLKWw8/</link>
		<comments>http://blog.plataformatec.com.br/2010/06/dsl-or-nodsl-at-euruko-2010/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 19:58:48 +0000</pubDate>
		<dc:creator>José Valim</dc:creator>
				<category><![CDATA[English]]></category>

		<guid isPermaLink="false">http://blog.plataformatec.com.br/?p=1165</guid>
		<description><![CDATA[At the end of May, I was honored to talk at Euruko, the most important Ruby event in Europe! The event was excellent, the organizers made an awesome work before, during and they are still keeping it up by releasing several videos along this week! Without further ado, here follows my talk video: DSL or [...]]]></description>
			<content:encoded><![CDATA[<p>At the end of May, I was honored to talk at <a href="http://euruko2010.org/">Euruko</a>, the most important Ruby event in Europe! The event was excellent, the organizers made an awesome work before, during and they are still keeping it up by releasing several videos along this week!</p>
<p>Without further ado, here follows my talk video:</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=12614561&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=12614561&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object>
<p><a href="http://vimeo.com/12614561">DSL or NoDSL by José Valim</a> from <a href="http://vimeo.com/agilece">Krakow Tech Conferences</a>.</p>
<p>Also, <a href="http://www.slideshare.net/plataformatec/dsl-or-nodsl-euruko-29may2010">I uploaded my slides</a> (even though they do not make a lot of sense by themselves) as a way to complete the awesome list of resources being wrapped at <a href="http://euruko2010.heroku.com/">http://euruko2010.heroku.com/</a>! I&#8217;m definitely looking forward to <a href="http://euruko2011.org/">Euruko 2011 in Berlin</a>!</p>
<img src="http://feeds.feedburner.com/~r/PlataformaBlog_english/~4/Z4LzLbLKWw8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.plataformatec.com.br/2010/06/dsl-or-nodsl-at-euruko-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.plataformatec.com.br/2010/06/dsl-or-nodsl-at-euruko-2010/</feedburner:origLink></item>
		<item>
		<title>Plataforma Tec and José Valim in Barcelona</title>
		<link>http://feedproxy.google.com/~r/PlataformaBlog_english/~3/7lQy22KQJXE/</link>
		<comments>http://blog.plataformatec.com.br/2010/05/plataforma-tec-and-jose-valim-in-barcelona/#comments</comments>
		<pubDate>Tue, 11 May 2010 13:23:42 +0000</pubDate>
		<dc:creator>Marcelo Park</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[barcelona]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[events]]></category>

		<guid isPermaLink="false">http://blog.plataformatec.com.br/?p=1008</guid>
		<description><![CDATA[José Valim, Lead Developer at Plataforma Tecnologia and Rails Core Member, will be in Barcelona from 12nd to 19th June. Between trying some Tapas and Paellas, José Valim will be at Spain to open new business opportunities. So if you are running a company in Barcelona, Madrid or nearby, this is a great opportunity for a Training Session in Rails [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://blog.plataformatec.com.br/wp-content/uploads/2010/05/barcelona-121.jpg"><img class="aligncenter size-full wp-image-1007" title="barcelona-12" src="http://blog.plataformatec.com.br/wp-content/uploads/2010/05/barcelona-121.jpg" alt="Plataforma Tec and José Valim visit Barcelona" width="448" height="301" /></a></p>
<p><a href="http://github.com/josevalim" target="_blank">José Valim</a>, Lead Developer at Plataforma Tecnologia and <a href="http://www.rubyonrails.org/core" target="_blank">Rails Core Member</a>, will be in Barcelona from 12nd to 19th June. Between trying some Tapas and Paellas, José Valim will be at Spain to open new business opportunities.</p>
<p>So if you are running a company in Barcelona, Madrid or nearby, this is a great opportunity for a <a title="Plataforma Tec Services" href="http://www.plataformatec.com.br/en/services">Training Session in Rails 3</a> or a tech-talk about <a href="http://github.com/plataformatec/">Plataforma Tec&#8217;s development tools</a> &#8211; with Valim in person. Or if you want to start a new project as well, this would be a great opportunity for a kick-off meeting!</p>
<p>If you&#8217;re interested or have other ideas that you want to discuss, please <a href="http://www.plataformatec.com.br/en/contact">contact us</a>.</p>
<p>And last but not least, Valim will also give a talk at Barcelona on Rails on the 17th, so don&#8217;t forget to stop by and say hi! If you are not from Barcelona and would like to talk with us, you are welcome as well!</p>
<img src="http://feeds.feedburner.com/~r/PlataformaBlog_english/~4/7lQy22KQJXE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.plataformatec.com.br/2010/05/plataforma-tec-and-jose-valim-in-barcelona/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.plataformatec.com.br/2010/05/plataforma-tec-and-jose-valim-in-barcelona/</feedburner:origLink></item>
		<item>
		<title>Accented chars in IRB with RVM and ruby 1.8.7</title>
		<link>http://feedproxy.google.com/~r/PlataformaBlog_english/~3/3OpaWSgpdgA/</link>
		<comments>http://blog.plataformatec.com.br/2010/05/accented-chars-in-irb-with-rvm-and-ruby-1-8-7/#comments</comments>
		<pubDate>Wed, 05 May 2010 17:14:44 +0000</pubDate>
		<dc:creator>George Guimarães</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[homebrew]]></category>
		<category><![CDATA[irb]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rvm]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://blog.plataformatec.com.br/?p=990</guid>
		<description><![CDATA[Since moving to Mac, I always find it surprising that the default Mac OS X Ruby and IRB doesn&#8217;t allow inputs with accented characters. So, you cannot do name = &#34;George Guimarães&#34; This is annoying for brazilians and anyone who uses non-ASCII characters. The problem is that the default Ruby in Mac OS X isn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Since moving to Mac, I always find it surprising that the default Mac OS X Ruby and IRB doesn&#8217;t allow inputs with accented characters. So, you cannot do</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">name = <span style="color:#996600;">&quot;George Guimarães&quot;</span></pre></div></div>

<p>This is annoying for brazilians and anyone who uses non-ASCII characters. The problem is that the default Ruby in Mac OS X isn&#8217;t linked against readline. A simple solution is to compile readline on your system, and then compile your own ruby binaries. A better one is to use tools that automate this process. </p>
<p><a href="http://www.codeography.com/2010/03/29/fix-snow-leopard-ruby-with-homebrew-readline.html">This post by Christopher Sexton</a> uses homebrew to compile readline-0.6 but compiles ruby by hand. Since <a href="http://blog.plataformatec.com.br/2010/04/rails-3-with-rvm-ftw">we love RVM</a>, we&#8217;ll use it too.</p>
<p>If you use <a href="http://github.com/mxcl/homebrew">Homebrew</a> (and you *should*), just do</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">brew <span style="color: #c20cb9; font-weight: bold;">install</span> readline
brew <span style="color: #c20cb9; font-weight: bold;">link</span> readline</pre></div></div>

<p><strong>Beware</strong>: linking readline into your system may break other tools that depends on readline source to compile. It was harmless on my system.</p>
<p>Ok, so now we want to build a new ruby and irb binaries. I wanted to use ruby 1.8.7-p248, so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rvm <span style="color: #c20cb9; font-weight: bold;">install</span> 1.8.7-p248 <span style="color: #660033;">-C</span> --enable-shared,--with-readline-dir=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span></pre></div></div>

<p>This tells the configure script to enable shared library linking (it is the default), and to search for readline in <code>/usr/local</code> (homebrew has just linked readline there). You may have to use <code>--force</code> if you already have this ruby version compiled. Newer versions of RVM do not need <code>--force</code>.</p>
<p>Now you can use accented and unicode characters on keyboard input in IRB using ruby 1.8.7.</p>
<p>And you? Do you have any tricks with IRB that you may want to share? Do you use <a href="http://github.com/blackwinter/wirble">Wirble</a>, <a href="http://github.com/gilesbowkett/utility-belt">Utility Belt</a> or others?</p>
<img src="http://feeds.feedburner.com/~r/PlataformaBlog_english/~4/3OpaWSgpdgA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.plataformatec.com.br/2010/05/accented-chars-in-irb-with-rvm-and-ruby-1-8-7/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://blog.plataformatec.com.br/2010/05/accented-chars-in-irb-with-rvm-and-ruby-1-8-7/</feedburner:origLink></item>
		<item>
		<title>Best Ruby Open Source Test Suites Awards</title>
		<link>http://feedproxy.google.com/~r/PlataformaBlog_english/~3/0YXC_d_eXwM/</link>
		<comments>http://blog.plataformatec.com.br/2010/04/best-ruby-open-source-test-suites-awards/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 15:40:37 +0000</pubDate>
		<dc:creator>José Valim</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[capybara]]></category>
		<category><![CDATA[euruko]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[railties]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://blog.plataformatec.com.br/?p=974</guid>
		<description><![CDATA[One of the beauties in the Open Source world is the possibility of reading other people source code and learn new things. However, lately I found out that not only the library code, but the test suite of several open source projects are full lessons for us. In this post, I want to tell you [...]]]></description>
			<content:encoded><![CDATA[<p>One of the beauties in the Open Source world is the possibility of reading other people source code and learn new things. However, lately I found out that not only the library code, but the test suite of several open source projects are full lessons for us.</p>
<p>In this post, I want to tell you which are the three test suites that I admire the most and why.</p>
<h3>Integration award: Railties</h3>
<p><a href="http://github.com/rails/rails" target="_blank">Rails 3</a> has several improvements and not all of them may be visible to the application developer. One of the hidden unicorns is Railties test suite. As Yehuda stated <a href="http://yehudakatz.com/2009/07/01/new-rails-isolation-testing/" target="_blank">in a blog post</a> during the refactoring of version 2.3 to 3.0:</p>
<blockquote><p>&#8220;Although the Rails initializer tests covered a fair amount of area, successfully getting the tests to pass did not guarantee that Rails booted.&#8221;</p></blockquote>
<p>This happened because, in order to have fast tests, Rails 2.3 suite stubbed and mocked a significant part of the booting process. The new test suite is able to create a new application using the application generator, change configuration options, add plugins and engines, boot it and even make HTTP requests using Rack::Test.</p>
<p>For instance, take a look at this test which ensures that app/metals inside plugins are successfully added to the application middleware stack:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> test_plugin_metals_added_to_middleware_stack
  <span style="color:#0066ff; font-weight:bold;">@plugin</span>.<span style="color:#9900CC;">write</span> <span style="color:#996600;">'app/metal/foo_metal.rb'</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>RUBY
    <span style="color:#9966CC; font-weight:bold;">class</span> FooMetal
      <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">200</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;Content-Type&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;text/html&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;FooMetal&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  RUBY
&nbsp;
  boot_rails
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rack/test'</span>
  extend <span style="color:#6666ff; font-weight:bold;">Rack::<span style="color:#CC00FF; font-weight:bold;">Test</span>::Methods</span>
&nbsp;
  get <span style="color:#996600;">&quot;/not/slash&quot;</span>
  assert_equal <span style="color:#006666;">200</span>, last_response.<span style="color:#9900CC;">status</span>
  assert_equal <span style="color:#996600;">&quot;FooMetal&quot;</span>, last_response.<span style="color:#9900CC;">body</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The most important lesson here is: whenever mocking or stubbing in our tests, we still need to add tests without the mocks and stubs to ensure all API contracts are respected.</p>
<h3>Readability award: Capybara</h3>
<p><a href="http://github.com/jnicklas/capybara" target="_blank">Capybara</a> is a tool to aid writing acceptance tests for web applications. <a href="http://github.com/jnicklas/capybara" target="_blank">Capybara</a> can use several drivers to interact with a web application, as Selenium, Celerity or even Rack::Test. Each driver needs a different setup and has different features. For instance, both Selenium and Celerity can handle javascript, but not Rack::Test.</p>
<p>As you may imagine, all these different drivers can make a test suite become a real spaghetti. However, <a href="http://twitter.com/jncoward" target="_blank">Jonas Nicklas</a> was able to transform a potential problem into a very elegant and readable test suite with Rspec help. Here is, for instance, the tests for selenium:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">describe <span style="color:#6666ff; font-weight:bold;">Capybara::Driver::Selenium</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  before <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@driver</span> = <span style="color:#6666ff; font-weight:bold;">Capybara::Driver::Selenium</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>TestApp<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  it_should_behave_like <span style="color:#996600;">&quot;driver&quot;</span>
  it_should_behave_like <span style="color:#996600;">&quot;driver with javascript support&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Each behavior group above (&#8220;driver&#8221; and &#8220;driver with javascript support&#8221;) is inside <a href="http://github.com/jnicklas/capybara" target="_blank">Capybara</a> library allowing everyone to develop its own extensions using a shared suite. For instance, if a driver has javascript support, it means the following tests should pass:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">shared_examples_for <span style="color:#996600;">&quot;driver with javascript support&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  before <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0066ff; font-weight:bold;">@driver</span>.<span style="color:#9900CC;">visit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/with_js'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  describe <span style="color:#996600;">'#find'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    it <span style="color:#996600;">&quot;should find dynamically changed nodes&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@driver</span>.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'//p'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">text</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">'I changed it'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  describe <span style="color:#996600;">'#drag_to'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    it <span style="color:#996600;">&quot;should drag and drop an object&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      draggable = <span style="color:#0066ff; font-weight:bold;">@driver</span>.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'//div[@id=&quot;drag&quot;]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span>
      droppable = <span style="color:#0066ff; font-weight:bold;">@driver</span>.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'//div[@id=&quot;drop&quot;]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span>
      draggable.<span style="color:#9900CC;">drag_to</span><span style="color:#006600; font-weight:bold;">&#40;</span>droppable<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@driver</span>.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'//div[contains(., &quot;Dropped!&quot;)]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">should_not</span> be_nil
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  describe <span style="color:#996600;">&quot;#evaluate_script&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    it <span style="color:#996600;">&quot;should return the value of the executed script&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@driver</span>.<span style="color:#9900CC;">evaluate_script</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'1+1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">2</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><a href="http://github.com/jnicklas/capybara" target="_blank">Capybara</a> test suite is one of the best examples of using tests as documentation. By skimming the test suite you can easily know which features are supported by each driver! Sweet, isn&#8217;t it?</p>
<h3>Friendliness award: I18n</h3>
<p>When you are a big Open Source project, your test suite needs to be easy to run in order to new developers can create patches without hassle. The <a href="http://github.com/svenfuchs/i18n" target="_blank">I18n</a> library for Ruby definitely meets the big Open Source project requirement since it&#8217;s widely used and provides several extensions.</p>
<p>However, some of these extensions depends on <strong>ActiveRecord</strong>, some in <strong>ruby2ruby</strong>, others in <strong>ruby-cldr</strong>&#8230; and <a href="http://gist.github.com/381892" target="_blank">soon it will even support a few Key-Value stores</a>, as Tokyo and Redis. Due to all these dependencies, you would probably imagine that running I18n test suite would require several trials and a lot of configuration before it finally works, right?</p>
<p>WRONG! If you don&#8217;t have ActiveRecord, <a href="http://github.com/svenfuchs/i18n" target="_blank">I18n</a> will say: &#8220;hey, you don&#8217;t have ActiveRecord&#8221; but still run the part of test suite that does not depend on it. So if a developer wants to fix or add something trivial, he doesn&#8217;t need to worry with installing all sorts of dependencies.</p>
<p>Besides, as mentioned a couple months ago, <a href="http://blog.plataformatec.com.br/2009/12/run-i18n-run/">the I18n library allows you to create several combinations of backends</a>. In other words, the I18n test suite needs to ensure that all these different combinations work as expected.</p>
<p>This problem is quite similar to the one in <a href="http://github.com/jnicklas/capybara" target="_blank">Capybara</a> which needs to test different drivers. However, <a href="http://github.com/svenfuchs/i18n" target="_blank">I18n</a> uses Test::Unit thus it cannot use shared examples groups as in Rspec. So how were I18n developers able to solve this issue? Using Ruby modules!</p>
<p>Here are the tests for the upcoming KeyValue backend:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'api'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> I18nKeyValueApiTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Tests::Api::Basics</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Tests::Api::Defaults</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Tests::Api::Interpolation</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Tests::Api::Link</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Tests::Api::Lookup</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Tests::Api::Pluralization</span>
  <span style="color:#008000; font-style:italic;"># include Tests::Api::Procs</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Tests::Api::Localization::Date</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Tests::Api::Localization::DateTime</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Tests::Api::Localization::Time</span>
  <span style="color:#008000; font-style:italic;"># include Tests::Api::Localization::Procs</span>
&nbsp;
  STORE = <span style="color:#6666ff; font-weight:bold;">Rufus::Tokyo::Cabinet</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'*'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> setup
    I18n.<span style="color:#9900CC;">backend</span> = <span style="color:#6666ff; font-weight:bold;">I18n::Backend::KeyValue</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>STORE<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">super</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  test <span style="color:#996600;">&quot;make sure we use the KeyValue backend&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    assert_equal <span style="color:#6666ff; font-weight:bold;">I18n::Backend::KeyValue</span>, I18n.<span style="color:#9900CC;">backend</span>.<span style="color:#9966CC; font-weight:bold;">class</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Each included module above adds a series of tests to the backend. Since key-value backends cannot store procs, we don&#8217;t include any test related to procs.</p>
<h3>Wrapping up</h3>
<p>These three are my favorite test suites and also part of my favorite open source projects!</p>
<p>We&#8217;ve adopted <a href="http://github.com/jnicklas/capybara" target="_blank">Capybara</a> as the official testing tool at PlataformaTec for some time already and I18n is one of the subjects of my upcoming book about Rails 3. In one specific chapter, we will build a tool that stores <a href="http://github.com/svenfuchs/i18n" target="_blank">I18n</a> translations into TokyoCabinet, which allows us to create and update translations through a web interface, similarly to ActiveRecord. The only difference is that <a href="http://gist.github.com/381892" target="_blank">TokyoCabinet is waaaay faster</a>.</p>
<p>Finally, the fact you can mimic several of Rspec features using simple Ruby (like <a href="http://github.com/jnicklas/capybara" target="_blank">Capybara</a> using shared example groups and <a href="http://github.com/svenfuchs/i18n" target="_blank">I18n</a> simply using modules) will be part of my talk in <a href="http://euruko2010.org/" target="_blank">Euruko 2010</a> entitled <strong>DSL or NoDSL: The power is in the middle</strong>. The talk will show cases where DSLs mimics much of the behavior provided by Ruby and discuss what we are winning and/or losing in such cases.</p>
<p>Keep following us and, until the next blog post is out, we would love to hear in the comments which are your favorite test suites!</p>
<img src="http://feeds.feedburner.com/~r/PlataformaBlog_english/~4/0YXC_d_eXwM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.plataformatec.com.br/2010/04/best-ruby-open-source-test-suites-awards/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://blog.plataformatec.com.br/2010/04/best-ruby-open-source-test-suites-awards/</feedburner:origLink></item>
	</channel>
</rss>
