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

<channel>
	<title>La rolls des blogs &#187; metaprogramming</title>
	<atom:link href="http://my.rails-royce.org/category/ruby/metaprogramming/feed/" rel="self" type="application/rss+xml" />
	<link>http://my.rails-royce.org</link>
	<description>Another Ruby and Rails blog</description>
	<lastBuildDate>Fri, 13 Jan 2012 23:47:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Reloading models in Rails 3.1 when usign Spork and cache_classes = true</title>
		<link>http://my.rails-royce.org/2012/01/14/reloading-models-in-rails-3-1-when-usign-spork-and-cache_classes-true/</link>
		<comments>http://my.rails-royce.org/2012/01/14/reloading-models-in-rails-3-1-when-usign-spork-and-cache_classes-true/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 23:47:52 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[gem]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[cache_classes]]></category>
		<category><![CDATA[initializers]]></category>
		<category><![CDATA[spork]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=277</guid>
		<description><![CDATA[Speeding up you tests with Spork is very common when developping Rails applications. What Spork do is preloading your application in a server and copies a fork of that server whenever you run tests. It avoids reloading all the rails stack !! That is great !!! But the problem I ran into is that I &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2012/01/14/reloading-models-in-rails-3-1-when-usign-spork-and-cache_classes-true/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Speeding up you tests with <a href="https://github.com/sporkrb/spork">Spork</a> is very common when developping Rails applications.</p>
<p>What Spork do is preloading your application in a server and copies a fork of that server whenever you run tests.<br />
<strong><br />
It avoids reloading all the rails stack !! That is great !!!<br />
</strong><br />
But the problem I ran into is that I wanted to be able to</p>
<ul>
<li><strong>reload</strong> my application files</li>
<li>and keep <strong>cache_classes = true</strong></li>
</ul>
<p>That is why I like ruby and rails (and great coders there ! ). What I simply did :</p>
<ol>
<li>Reading and understanding the rails initialize process  (especially these lines <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/engine.rb#L433-442">engine.rb</a> and <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/application/finisher.rb#L51-56">finisher.rb</a> )</li>
<li>Adding these small pieces of code in my prefork block with <a href="https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujutsu">Spork trap method </a></li>
<li>trap eager loading in my prefork block like below</li>
</ol>

<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;">'rails/application'</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Use of https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujutsu</span>
  Spork.<span style="color:#9900CC;">trap_method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rails::Application</span>, <span style="color:#ff3333; font-weight:bold;">:reload_routes</span>!<span style="color:#006600; font-weight:bold;">&#41;</span>
  Spork.<span style="color:#9900CC;">trap_method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rails::Application::RoutesReloader</span>, <span style="color:#ff3333; font-weight:bold;">:reload</span>!<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rails/mongoid'</span>
  Spork.<span style="color:#9900CC;">trap_class_method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rails::Mongoid</span>, <span style="color:#ff3333; font-weight:bold;">:load_models</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Prevent main application to eager_load in the prefork block (do not load files in autoload_paths)</span>
  Spork.<span style="color:#9900CC;">trap_method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rails::Application</span>, <span style="color:#ff3333; font-weight:bold;">:eager_load</span>!<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Below this line it is too late...</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;../../config/environment&quot;</span>, <span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># Load all railties files</span>
  Rails.<span style="color:#9900CC;">application</span>.<span style="color:#9900CC;">railties</span>.<span style="color:#9900CC;">all</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span> r.<span style="color:#9900CC;">eager_load</span>! <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>My own benefits : specs runs faster than setting cache_classes = false</p>
<p><strong>You need small explanation of what I&#8217;ve done !</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  Spork.<span style="color:#9900CC;">trap_method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rails::Application</span>, <span style="color:#ff3333; font-weight:bold;">:eager_load</span>!<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;../../config/environment&quot;</span>, <span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  Rails.<span style="color:#9900CC;">application</span>.<span style="color:#9900CC;">railties</span>.<span style="color:#9900CC;">all</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span> r.<span style="color:#9900CC;">eager_load</span>! <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>First, we prevent Rails::Application to loads files in the application autoload_paths such as app/models, app/controllers etc &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  Spork.<span style="color:#9900CC;">trap_method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rails::Application</span>, <span style="color:#ff3333; font-weight:bold;">:eager_load</span>!<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Second, we load the rails stack :</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:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;../../config/environment&quot;</span>, <span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Last, we eager load all the engines :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  Rails.<span style="color:#9900CC;">application</span>.<span style="color:#9900CC;">railties</span>.<span style="color:#9900CC;">all</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span> r.<span style="color:#9900CC;">eager_load</span>! <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Doing that we can make change in our models and avoid restarting spork.</p>
<p>Why don&#8217;t I just set cache_classes to false ?</p>
<p>Because the ActiveSupport::Dependencies.mechanism is set to :require and not :load</p>
<p>All application files are required and not just loaded (spork required them just after forking, not before).<br />
This trick could resolve some inconsistencies in your application (such as already defined constant and else &#8230;)</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2012/01/14/reloading-models-in-rails-3-1-when-usign-spork-and-cache_classes-true/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rspec 2.x and Fixtures</title>
		<link>http://my.rails-royce.org/2011/03/17/rspec-2-and-fixtures/</link>
		<comments>http://my.rails-royce.org/2011/03/17/rspec-2-and-fixtures/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 11:48:40 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[activerecord]]></category>
		<category><![CDATA[active_record]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[test-unit]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=262</guid>
		<description><![CDATA[Hello, It&#8217;s been a while when I last updated this blog. It seems that Rspec 2 can not use fixtures from Activerecord 2.3.x ! Impossible ? Not really, and thanks to ruby it was possible for me. Let&#8217;s describe the background : First of all, I had a gem built on jeweler that uses ActiveSupport::TestCase &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2011/03/17/rspec-2-and-fixtures/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Hello, </p>
<p>It&#8217;s been a while when I last updated this blog. It seems that Rspec 2 can not use fixtures from Activerecord 2.3.x !</p>
<p>Impossible ? Not really, and thanks to ruby it was possible for me.</p>
<p>Let&#8217;s describe the background :</p>
<p>First of all, I had a gem built on jeweler that uses ActiveSupport::TestCase and ActiveRecord::Fixtures exactly implemented like in Rails<br />
But one day, I decided to switch to RSpec  (jeweler (1.5.2) uses as of my writing rspec ~> 2.3.0) </p>
<p>The conversion of test_* to &#8220;it&#8221; syntax was very quick and easy but loading fixtures was not !!!<br />
So here is my implementation if you want to know how can this be possible :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># spec/spec_helper.rb</span>
<span style="color:#008000; font-style:italic;"># .... some stuff ...</span>
 <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'activerecord'</span>
<span style="color:#9966CC; font-weight:bold;">module</span>  MyExtensions
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">extended</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> base <span style="color:#008000; font-style:italic;"># Beginning of Proxy</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> subclass_with_fixtures<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args,<span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
        child = subclass_without_fixtures<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args,<span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
        child.<span style="color:#9900CC;">module_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::Callbacks</span>
          define_callbacks <span style="color:#ff3333; font-weight:bold;">:setup</span>, <span style="color:#ff3333; font-weight:bold;">:teardown</span>
          <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::TestFixtures</span>
&nbsp;
          <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">fixture_path</span> = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;../db/fixtures&quot;</span>,<span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">use_instantiated_fixtures</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
          <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">use_transactional_fixtures</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
          <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">set_fixture_class</span> <span style="color:#ff3333; font-weight:bold;">:categories</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Categorie'</span>
&nbsp;
          <span style="color:#9966CC; font-weight:bold;">def</span> run_in_transaction?
            <span style="color:#0000FF; font-weight:bold;">true</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        child
      <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#008000; font-style:italic;"># End of method chain subclass_with_fixtures</span>
      alias_method_chain <span style="color:#ff3333; font-weight:bold;">:subclass</span>, <span style="color:#ff3333; font-weight:bold;">:fixtures</span>
    <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#008000; font-style:italic;"># End of Proxy</span>
  <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#008000; font-style:italic;"># End of extended</span>
<span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#008000; font-style:italic;"># End of MyExtensions</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">RSpec::Core::ExampleGroup</span>.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>MyExtensions<span style="color:#006600; font-weight:bold;">&#41;</span>
RSpec.<span style="color:#9900CC;">configure</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
&nbsp;
  config.<span style="color:#9900CC;">after</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    run_callbacks <span style="color:#ff3333; font-weight:bold;">:teardown</span>, <span style="color:#ff3333; font-weight:bold;">:enumerator</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:reverse_each</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  config.<span style="color:#9900CC;">before</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    run_callbacks <span style="color:#ff3333; font-weight:bold;">:setup</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In my fixtures :</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">  # spec/db/fixtures/posts.yml
  a_post:
    title: &quot;My first post&quot;</pre></div></div>

<p>Now  when I want to load fixtures :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># spec/post_spec.rb</span>
describe <span style="color:#996600;">&quot;Post&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</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;">class</span>.<span style="color:#9900CC;">fixtures</span> <span style="color:#ff3333; font-weight:bold;">:posts</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  it <span style="color:#996600;">&quot;should not save without a title&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
     post = posts<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:a_post</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    post.<span style="color:#9900CC;">title</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
    post.<span style="color:#9900CC;">save</span>.<span style="color:#9900CC;">should_not</span> be_true
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Maybe, a better way can be found &#8230; if so please inform me !!!</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2011/03/17/rspec-2-and-fixtures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveRecord like searching object in Rails form helpers</title>
		<link>http://my.rails-royce.org/2010/11/17/activerecord-like-searching-object-in-rails-form-helpers/</link>
		<comments>http://my.rails-royce.org/2010/11/17/activerecord-like-searching-object-in-rails-form-helpers/#comments</comments>
		<pubDate>Wed, 17 Nov 2010 00:29:22 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[activerecord]]></category>
		<category><![CDATA[active_record]]></category>
		<category><![CDATA[base]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[form helpers]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[novice]]></category>
		<category><![CDATA[programmation]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=242</guid>
		<description><![CDATA[Ruby is a wonderful language. And Rails is a wonderful framework. Why ? Because of Duck typing !!! Example : You build a database application for a book store Books are classified by : Genre Author Country On a search page, you can search with a title text field, author select box, genre checkboxes. So &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2010/11/17/activerecord-like-searching-object-in-rails-form-helpers/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Ruby is a wonderful language. And Rails is a wonderful framework.<br />
Why ? Because of <a href="http://en.wikipedia.org/wiki/Duck_typing">Duck typing</a> !!!</p>
<p>Example : </p>
<p>You build a database application for a book store</p>
<p>Books are classified by :</p>
<ul>
<li>Genre</li>
<li>Author</li>
<li>Country</li>
</ul>
<p>On a search page, you can search with a title text field, author select box, genre checkboxes.<br />
So we want to create a search form with all the fields described above :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= form_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/search'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;p&gt;
    &lt;label for=&quot;title&quot;&gt;title&lt;/label&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= text_field_tag <span style="color:#996600;">'title'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    &lt;label for=&quot;author&quot;&gt;Author&lt;/label&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= select_tag <span style="color:#996600;">'author'</span>,  options_for_select <span style="color:#0066ff; font-weight:bold;">@authors</span>, <span style="color:#0066ff; font-weight:bold;">@selected_author</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@genres</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>g<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    &lt;p&gt;
      &lt;label for=&quot;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#996600;">'genre_'</span> <span style="color:#006600; font-weight:bold;">+</span> g.<span style="color:#9900CC;">id</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&quot;&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= g.<span style="color:#9900CC;">label</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/label&gt;
      <span style="color:#006600; font-weight:bold;">&lt;%</span>= check_box_tag <span style="color:#996600;">'genre[]'</span>, g.<span style="color:#9900CC;">id</span>, g.<span style="color:#9900CC;">id</span> == <span style="color:#0066ff; font-weight:bold;">@selected_genre</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;genre_#{g.id}&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    &lt;/p&gt;
  <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>
<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>When I submit the code, it does not keep the value of the search fields, as it is when passing activerecord object in form helpers (with text_field not text_field_tag.)<br />
Ok!! And what if I construct an object behaving like activerecord object ?<br />
My template will be like this :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= form_for <span style="color:#0066ff; font-weight:bold;">@search</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>
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:title</span>, <span style="color:#996600;">'Title'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:author</span>, <span style="color:#996600;">'Author'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#ff3333; font-weight:bold;">:author</span>, <span style="color:#0066ff; font-weight:bold;">@authors</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@genres</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>g<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    &lt;p&gt;
      &lt;label for=&quot;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#996600;">'genre_'</span> <span style="color:#006600; font-weight:bold;">+</span> g.<span style="color:#9900CC;">id</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&quot;&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= g.<span style="color:#9900CC;">label</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/label&gt;
      <span style="color:#006600; font-weight:bold;">&lt;%</span>= check_box_tag <span style="color:#996600;">'search[genre][]'</span>, g.<span style="color:#9900CC;">id</span>, g.<span style="color:#9900CC;">id</span> == <span style="color:#0066ff; font-weight:bold;">@selected_genre</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;genre_#{g.id}&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    &lt;/p&gt;
  <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>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= submit_tag <span style="color:#996600;">&quot;Search&quot;</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>Yes we have a namespace called &#8220;search&#8221; on all parameters such as &#8220;search[title]&#8221; but I think it is not ugly, instead the code will be more readable !!!<br />
Ok, but what&#8217;s the magic ? How can I have an activerecord like search object ?</p>
<p>Simply like that :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># encoding: utf-8</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Search
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>params=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@params</span> = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'search'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span> 
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>method_sym,<span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> method_sym.<span style="color:#9900CC;">to_s</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>^\<span style="color:#006600; font-weight:bold;">&#91;</span>\<span style="color:#006600; font-weight:bold;">&#93;</span>=?$<span style="color:#006600; font-weight:bold;">/</span>
      <span style="color:#0066ff; font-weight:bold;">@params</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>method_sym,<span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>=$<span style="color:#006600; font-weight:bold;">/</span>
      <span style="color:#0066ff; font-weight:bold;">@params</span>.<span style="color:#9900CC;">update</span><span style="color:#006600; font-weight:bold;">&#40;</span>$1,<span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#0066ff; font-weight:bold;">@params</span><span style="color:#006600; font-weight:bold;">&#91;</span>method_sym.<span style="color:#9900CC;">to_s</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>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In your controller, simply instantiate a <i>@search</i> variable with your parameters :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># app/controllers/search_controller.rb</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> search
  <span style="color:#0066ff; font-weight:bold;">@search</span> = Search.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># ....</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/11/17/activerecord-like-searching-object-in-rails-form-helpers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Rails : How to bypass skip validation in Devise</title>
		<link>http://my.rails-royce.org/2010/10/20/rails-how-to-skip-or-remove-validations-in-a-model/</link>
		<comments>http://my.rails-royce.org/2010/10/20/rails-how-to-skip-or-remove-validations-in-a-model/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 16:25:34 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[activerecord]]></category>
		<category><![CDATA[active_record]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[authentification]]></category>
		<category><![CDATA[devise]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[skip]]></category>
		<category><![CDATA[validate]]></category>
		<category><![CDATA[validations]]></category>
		<category><![CDATA[validator]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=223</guid>
		<description><![CDATA[Since my last post with email validations, I was looking for a solution to integrate it with devise Sadly for me, devise implements the old way of validating email with regexp : # From devise gem version 1.1.3 validates_format_of :email, :with =&#62; email_regexp, :allow_blank =&#62; true Well, as I don&#8217;t want to hack activemodel&#8217;s code, &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2010/10/20/rails-how-to-skip-or-remove-validations-in-a-model/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Since <a href="http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/">my last post with email validations</a>, I was looking for a solution to integrate it with <a href="http://github.com/plataformatec/devise">devise</a></p>
<p>Sadly for me, devise implements the old way of validating email with regexp :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># From devise gem version 1.1.3</span>
validates_format_of     <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:with</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> email_regexp, <span style="color:#ff3333; font-weight:bold;">:allow_blank</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span></pre></div></div>

<p>Well, as I don&#8217;t want to hack activemodel&#8217;s code, I prefer to skip the validates_format_of method in User class.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#008000; font-style:italic;"># Include default devise modules. Others available are:</span>
  <span style="color:#008000; font-style:italic;"># :token_authenticatable, :confirmable, :lockable and :timeoutable</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> SkipableMethod
&nbsp;
  <span style="color:#008000; font-style:italic;"># Devise is great but uses the oldest way with regexp to validate email</span>
  skip_and_restore_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:validates_format_of</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    devise <span style="color:#ff3333; font-weight:bold;">:database_authenticatable</span>, <span style="color:#ff3333; font-weight:bold;">:registerable</span>,
      <span style="color:#ff3333; font-weight:bold;">:recoverable</span>, <span style="color:#ff3333; font-weight:bold;">:rememberable</span>, <span style="color:#ff3333; font-weight:bold;">:trackable</span>, <span style="color:#ff3333; font-weight:bold;">:validatable</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Replace with a validation of my own</span>
  validates <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:presence</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Setup accessible (or protected) attributes for your model</span>
  attr_accessible <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span>, <span style="color:#ff3333; font-weight:bold;">:remember_me</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And then the library :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> SkipableMethod
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
&nbsp;
    protected
    <span style="color:#9966CC; font-weight:bold;">def</span> __skip_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>;end
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> skip_and_restore_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>method_names,<span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
      method_names.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>method_name<span style="color:#006600; font-weight:bold;">|</span>
        instance_eval <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#CC0066; font-weight:bold;">EVAL</span>
        <span style="color:#9966CC; font-weight:bold;">alias</span> <span style="color:#008000; font-style:italic;">#{method_name}_skipped #{method_name}</span>
        <span style="color:#9966CC; font-weight:bold;">alias</span> <span style="color:#008000; font-style:italic;">#{method_name} __skip_method</span>
        <span style="color:#CC0066; font-weight:bold;">EVAL</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">yield</span>
&nbsp;
      method_names.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>method_name<span style="color:#006600; font-weight:bold;">|</span>
        instance_eval <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#CC0066; font-weight:bold;">EVAL</span>
        <span style="color:#9966CC; font-weight:bold;">alias</span> <span style="color:#008000; font-style:italic;">#{method_name} #{method_name}_skipped</span>
        <span style="color:#9966CC; font-weight:bold;">undef</span> <span style="color:#008000; font-style:italic;">#{method_name}_skipped</span>
        <span style="color:#CC0066; font-weight:bold;">EVAL</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>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>receiver<span style="color:#006600; font-weight:bold;">&#41;</span>
    receiver.<span style="color:#9900CC;">extend</span>         ClassMethods
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>TODO : </p>
<p>Some tests  <img src='http://my.rails-royce.org/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/10/20/rails-how-to-skip-or-remove-validations-in-a-model/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Enumerable grep method in Ruby</title>
		<link>http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/</link>
		<comments>http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 13:43:08 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[best practices]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=201</guid>
		<description><![CDATA[There is some methods that are not very known among ruby developpers. One of them is Enumerable#grep method. I would like to share one of the use case I have to face with, and how I implement grep. Purpose, I have a Hash representing a simple network I&#8217;d like to select all the people related &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>There is some methods that are not very known among ruby developpers.<br />
One of them is <strong>Enumerable#grep</strong> method.<br />
I would like to share one of the use case I have to face with, and how I implement grep.</p>
<p>Purpose, I have a Hash representing a simple network<br />
I&#8217;d like to select all the people related to :patrick with network label as key.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Say I have this hash</span>
h = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:work</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;">:patrick</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:football</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;">:patrick</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span> , <span style="color:#ff3333; font-weight:bold;">:john</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:paul</span>, <span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:friends</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;">:paul</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elisabeth</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># I want this result :</span>
<span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>,:elvis<span style="color:#006600; font-weight:bold;">&#93;</span>,
  <span style="color:#ff3333; font-weight:bold;">:football</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>A solution is to iterate throug each values then select :patrick</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  h.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>memo,<span style="color:#006600; font-weight:bold;">&#40;</span>k,v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">|</span>
   <span style="color:#9966CC; font-weight:bold;">if</span> v.<span style="color:#9900CC;">has_key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    memo.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span>k <span style="color:#006600; font-weight:bold;">=&gt;</span> v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">else</span> 
    memo
   <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Though it works, I think this piece code is not &#8220;rubyesque&#8221;.</p>
<p>Well, this is a basic case but imagine you have to select X number of variables or more  ( :patrick,:paul &#8230; etc ) or worse select with a more complex criteria.</p>
<p>After digging into the rdoc documentation, it seems that <strong>Enumerable#grep</strong> matches our requirements :</p>
<ul>
<li> It takes a parameter to compare with each elements of the <strong>Enumerable</strong> </li>
<li> It can take a block that returns the value of matching elements </li>
</ul>
<p>Well, how can we implement that ?<br />
This code below demonstrate why I love Ruby !</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> NetworkPattern
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@matches</span> = args
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> ===<span style="color:#006600; font-weight:bold;">&#40;</span>pair<span style="color:#006600; font-weight:bold;">&#41;</span>
     key, value = pair
     value.<span style="color:#9900CC;">values_at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>@matches<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">any</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> result<span style="color:#006600; font-weight:bold;">&#40;</span>hash<span style="color:#006600; font-weight:bold;">&#41;</span>
    hash.<span style="color:#9900CC;">values_at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">compact</span>.<span style="color:#9900CC;">flatten</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">exec</span><span style="color:#006600; font-weight:bold;">&#40;</span>hash<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span> hash.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">grep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span>  <span style="color:#006600; font-weight:bold;">&#91;</span>k,<span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_a
     <span style="color:#0066ff; font-weight:bold;">@matches</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
pattern = NetworkPattern.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span> h.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">grep</span><span style="color:#006600; font-weight:bold;">&#40;</span>pattern<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span>  <span style="color:#006600; font-weight:bold;">&#91;</span>k,pattern.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#93;</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;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:football</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># With somer refactoring, we implement NetworPattern#exec and use it </span>
pattern.<span style="color:#CC0066; font-weight:bold;">exec</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</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;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:football</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Well it may seem more complicated but if you want to select both :patrick and :paul  relationships, the code does not change <strong>howmany arguments you pass in NetworkPattern.new</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">pattern = NetworkPattern.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span>, <span style="color:#ff3333; font-weight:bold;">:paul</span><span style="color:#006600; font-weight:bold;">&#41;</span>
pattern.<span style="color:#CC0066; font-weight:bold;">exec</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</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;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:football</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:friends</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elisabeth</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>All the logic belongs to the class <strong>NetworkPattern</strong> and it uses <strong>Enumerable#grep</strong> with block return.<br />
In your project you can use something like that since it can be easily tested and more readable.</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql 4.x and Rails : RELEASE SAVEPOINT issue</title>
		<link>http://my.rails-royce.org/2010/03/11/mysql-4-x-and-rails-release-savepoint-issue/</link>
		<comments>http://my.rails-royce.org/2010/03/11/mysql-4-x-and-rails-release-savepoint-issue/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 09:32:37 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[activerecord]]></category>
		<category><![CDATA[active_record]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=140</guid>
		<description><![CDATA[Today with a project using mysql 4.1.22 that I have to implement some tests, I ran this error with rails 2.3.5 and I think for 2.3.x branch. Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#8216;RELEASE SAVEPOINT &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2010/03/11/mysql-4-x-and-rails-release-savepoint-issue/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Today with a project using mysql 4.1.22 that I have to implement some tests, I ran this error with rails 2.3.5 and I think for 2.3.x branch.</p>
<blockquote><p>
Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#8216;RELEASE SAVEPOINT active_record_1&#8242; at line 1: RELEASE SAVEPOINT active_record_1
</p></blockquote>
<p>RELEASE SAVEPOINT is for Mysql 5.x</p>
<p>It is also described here <a href="http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/be0bece7cdf052fa?fwc=1">http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/be0bece7cdf052fa?fwc=1</a></p>
<p>I get it resolved when digging into the activerecord code :<br />
ActiveRecord::ConnectionAdapters::AbstractAdapter defines <em>release_savepoint</em> method.</p>
<p>To get around with this, just add the belowing patch after Rails was loaded (at the bottom of your config/environment.rb for instance)</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># As of mysql 4.x does not support &quot;RELEASE SAVEPOINT&quot; statement</span>
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">instance_variable_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@connection&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">get_server_info</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#996600;">'5.0'</span>
  <span style="color:#6666ff; font-weight:bold;">ActiveRecord::ConnectionAdapters::MysqlAdapter</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#008000; font-style:italic;"># Unset release savepoint method </span>
    <span style="color:#9966CC; font-weight:bold;">def</span> release_savepoint
    <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>Hope that this can help !</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/03/11/mysql-4-x-and-rails-release-savepoint-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>gettext 2.0.4 and rails 2.3.4</title>
		<link>http://my.rails-royce.org/2009/10/08/gettext-2-0-4-and-rails-2-3-4/</link>
		<comments>http://my.rails-royce.org/2009/10/08/gettext-2-0-4-and-rails-2-3-4/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 08:09:04 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[2.0.4]]></category>
		<category><![CDATA[2.3.4]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[active_record]]></category>
		<category><![CDATA[gettext]]></category>
		<category><![CDATA[I18n]]></category>
		<category><![CDATA[internationalisation]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[L10n]]></category>
		<category><![CDATA[localization]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=122</guid>
		<description><![CDATA[Rails 2.3.4 broke gettext_activerecord 2.0.4 Well I did not test other gems of the mutoh gettext set of libraries but here is the gulty code that breaks rails. Since Activerecord::Errors changed its implementation a bit, an error like this is raised : NoMethodError: undefined method `gsub' for #&#60;ActiveRecord::Error:0x00000002919738&#62; from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:165:in `localize_error_message' from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:112:in `block &#40;2 &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2009/10/08/gettext-2-0-4-and-rails-2-3-4/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Rails 2.3.4 broke gettext_activerecord 2.0.4</p>
<p>Well I did not test other gems of the <a href="http://github.com/mutoh">mutoh gettext set of libraries</a> but here is the gulty code that breaks rails.<br />
Since Activerecord::Errors changed its implementation a bit, an error like this is raised :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">NoMethodError: undefined method <span style="color: #000000; font-weight: bold;">`</span>gsub<span style="color: #ff0000;">' for #&lt;ActiveRecord::Error:0x00000002919738&gt;
        from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:165:in `localize_error_message'</span>
        from <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>hallelujah<span style="color: #000000; font-weight: bold;">/</span>Jail<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>1.9.1<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord-2.0.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord<span style="color: #000000; font-weight: bold;">/</span>validations.rb:<span style="color: #000000;">112</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>block <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span> levels<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">in</span> each_with_gettext_activerecord<span style="color: #ff0000;">'
        from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:112:in `each'</span>
        from <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>hallelujah<span style="color: #000000; font-weight: bold;">/</span>Jail<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>1.9.1<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord-2.0.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord<span style="color: #000000; font-weight: bold;">/</span>validations.rb:<span style="color: #000000;">112</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>block <span style="color: #000000; font-weight: bold;">in</span> each_with_gettext_activerecord<span style="color: #ff0000;">'
        from /home/hallelujah/Jail/lib/ruby/gems/1.9.1/gems/gettext_activerecord-2.0.4/lib/gettext_activerecord/validations.rb:112:in `each_key'</span>
        from <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>hallelujah<span style="color: #000000; font-weight: bold;">/</span>Jail<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>1.9.1<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord-2.0.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gettext_activerecord<span style="color: #000000; font-weight: bold;">/</span>validations.rb:<span style="color: #000000;">112</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>each_with_gettext_activerecord<span style="color: #ff0000;">'</span></pre></div></div>

<p>You can add this piece of code after your gems and rails are loaded (e.g at the bottom of config/environment.rb )</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecord
  <span style="color:#9966CC; font-weight:bold;">class</span> Errors
    <span style="color:#9966CC; font-weight:bold;">def</span> localize_error_message_with_patch<span style="color:#006600; font-weight:bold;">&#40;</span>attr,obj, append_field<span style="color:#006600; font-weight:bold;">&#41;</span>
      obj = obj.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? obj.<span style="color:#9900CC;">message</span> : obj
      localize_error_message_without_patch<span style="color:#006600; font-weight:bold;">&#40;</span>attr,obj,append_field<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    alias_method_chain <span style="color:#ff3333; font-weight:bold;">:localize_error_message</span>, <span style="color:#ff3333; font-weight:bold;">:patch</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I think I will patch gettext_activerecord and the muto gettext set of library to fit in rails 2.3.4<br />
Maybe I may keep an eye on Rails 3.0 development.</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/10/08/gettext-2-0-4-and-rails-2-3-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Inspectable accessors</title>
		<link>http://my.rails-royce.org/2009/09/23/inspectable-accessors/</link>
		<comments>http://my.rails-royce.org/2009/09/23/inspectable-accessors/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 15:49:46 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[accessor]]></category>
		<category><![CDATA[inspect]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=65</guid>
		<description><![CDATA[Hey, it&#8217;s been a while I posted here. So what&#8217;s the purpose ? I commonly use script/console to debug my rails applications. Today I wanted to see the values of my custom attributes model without typing model.custom_attribute. So I took a look at ActiveRecord::Base#inspect method. and the default inspect method shows me database fields. Well, &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2009/09/23/inspectable-accessors/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Hey, it&#8217;s been a while I posted here.</p>
<p>So what&#8217;s the purpose ? I commonly use script/console  to debug my rails applications. Today I wanted to see the values of my custom attributes  model without typing <em><strong>model.custom_attribute</strong></em>. So I took a look at <em><strong>ActiveRecord::Base#inspect method.</strong></em> and the default inspect method shows me database fields. Well, let&#8217;s do some work to inspect custom attributes.<br />
Say you have a <em>users</em> table with some fields : login,name,encrypted_password.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:heroic_action</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">superman = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span>
superman.<span style="color:#9900CC;">heroic_action</span> = <span style="color:#996600;">&quot;Save the widow and the orphan&quot;</span>
pp superman.<span style="color:#9900CC;">inspect</span>
<span style="color:#996600;">&quot;#&lt;User login: &quot;</span>Superman<span style="color:#996600;">&quot;,  name: &quot;</span>Clark Kent<span style="color:#996600;">&quot;,
          encrypted_password: &quot;</span>da39a3ee5e6b4b0d3255bfef95601890afd80709<span style="color:#996600;">&quot;&gt;&quot;</span></pre></div></div>

<p>Well I also want to know what heroic_action is superman doing. So i have to overwrite inspect method but I would like a DRY solution. That&#8217;s why I wrote this code :</p>
<p>EDIT: Just a small change to have inheritance work.</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;">'active_support/core_ext/class'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> InspectableAttributes
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">class_inheritable_array</span> <span style="color:#ff3333; font-weight:bold;">:inspectable_attributes</span> , <span style="color:#ff3333; font-weight:bold;">:instance_writer</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    base.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>ClassMethods<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">inspectable_attributes</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    base.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">def</span> inspect
        inspected_values = respond_to?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:attributes_before_type_cast</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? attributes_before_type_cast.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;#{k.to_s}:  #{v.inspect}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> : <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        inspected_values.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">inspectable_attributes</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>attr<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;#{attr}: #{send(attr).inspect}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#996600;">&quot;#&lt;#{self.class.name} #{inspected_values.join(', ')}&gt;&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <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;">module</span> ClassMethods
    <span style="color:#9966CC; font-weight:bold;">def</span> inspectable_attr_accessor<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
      attr_accessor<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">inspectable_attributes</span> <span style="color:#006600; font-weight:bold;">|</span>= args.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:to_s<span style="color:#006600; font-weight:bold;">&#41;</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;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> InspectableAttributes
  inspectable_attr_accessor <span style="color:#ff3333; font-weight:bold;">:heroic_action</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That&#8217;s what I was looking for !!!</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">superman = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span>
superman.<span style="color:#9900CC;">heroic_action</span> = <span style="color:#996600;">&quot;Save the widow and the orphan&quot;</span>
pp superman.<span style="color:#9900CC;">inspect</span>
<span style="color:#996600;">&quot;#&lt;User login: &quot;</span>Superman<span style="color:#996600;">&quot;,  name: &quot;</span>Clark Kent<span style="color:#996600;">&quot;,
             encrypted_password: &quot;</span>da39a3ee5e6b4b0d3255bfef95601890afd80709<span style="color:#996600;">&quot;,
             heroic_action: &quot;</span>Save the widow <span style="color:#9966CC; font-weight:bold;">and</span> the orphan<span style="color:#996600;">&quot;&gt;&quot;</span></pre></div></div>

<p>Note that this code can be used in any ruby code requiring &#8216;active_support/core_ext/class&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/09/23/inspectable-accessors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blocks are Proc : Les procédures Proc et les blocks en ruby.</title>
		<link>http://my.rails-royce.org/2009/07/24/blocks-are-proc-les-procedures-proc-et-les-blocks-en-ruby/</link>
		<comments>http://my.rails-royce.org/2009/07/24/blocks-are-proc-les-procedures-proc-et-les-blocks-en-ruby/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 18:01:11 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[base]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[novice]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=48</guid>
		<description><![CDATA[( For non-french readers : I am not giving up writing in english &#8230; Read the next post! ) Si vous venez d&#8217;un autre langage, le terme procédure (associé au terme fonction) doit vous dire quelque chose. Vaguement &#8230;. Allez quelques connaissances (méconnaissances) tirées de ma mémoire pour poser les bases. Par exemple, en PHP &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2009/07/24/blocks-are-proc-les-procedures-proc-et-les-blocks-en-ruby/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>( For non-french readers : I am not giving up writing in english &#8230; Read the next post! )</p>
<p>Si vous venez d&#8217;un autre langage, le terme procédure (associé au terme fonction) doit vous dire quelque chose. Vaguement &#8230;.</p>
<p>Allez quelques connaissances (méconnaissances) tirées de ma mémoire pour poser les bases.</p>
<p>Par exemple, en PHP il existe deux types de fonctions. Les fonctions natives (built-in) et les fonctions &#8220;codées&#8221; par l&#8217;utilisateur.<br />
Ces dernières se séparent en deux types : les fonctions et les procédures.</p>
<p>La différence entre les deux ?  Une procédure est un groupe d&#8217;instructions qui ne renvoie pas de valeur. Une fonction est une procédure qui renvoie une valeur.</p>
<p>Et ruby dans tout ça ??</p>
<p>C&#8217;est complètement différent et je dirai même plus les procédures Ruby sont très éloignés des procédures PHP. En ruby il n&#8217;y a que des objets (les méthodes sont des objets) et les méthodes renvoient toujours une valeur.<br />
Vous avez déjà vu ou même codé des choses comme ceci :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span>,<span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>nb<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#008000; font-style:italic;"># Un code quelconque : des instructions ...</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>C&#8217;ette méthode est native à Enumerable (module que la classe Array inclut)<br />
La méthode <strong>Array#each</strong> prend 0 paramètre mais un block. Le block est tout ce qui se trouve entre <strong>do</strong> et <strong>end</strong>.<br />
On pourrait le représenter en Ruby comme ceci :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> each<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># Un truc compliqué à écrire en ruby</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Ne vous y trompez pas <strong>&#8220;&#038;block&#8221;</strong> n&#8217;est pas un argument. C&#8217;est la notation ruby pour dire que la méthode each s&#8217;accompagne obligatoirement d&#8217;un block.</p>
<p><strong><em>Autre exemple :</em></strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Bar
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> my_proc<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>procedure<span style="color:#006600; font-weight:bold;">&#41;</span>
     <span style="color:#0066ff; font-weight:bold;">@foo_proc</span> = procedure
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> recall
    <span style="color:#0066ff; font-weight:bold;">@foo_proc</span>.<span style="color:#9900CC;">call</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
bar = Bar.<span style="color:#9900CC;">new</span>
bar.<span style="color:#9900CC;">my_proc</span> <span style="color:#9966CC; font-weight:bold;">do</span>
<span style="color:#008000; font-style:italic;"># Pas de code</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
 <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x00000&gt;</span></pre></div></div>

<p>Il se trouve que la procédure est une instance de la classe Proc.<br />
Sans trop de surprise mêmes les procédures sont des objets. Et oui, en ruby tout est objet.</p>
<p>Quel est l&#8217;intérêt me direz-vous?</p>
<p>Cela change tout !!! En PHP les prcoédures sont codées en dur, en ruby on peut générer à la volée des suites d&#8217;instructions sans pour autant avoir à implémenter une méthode !!!</p>
<p><strong><em>Illustration :</em></strong></p>
<p>On sait que les blocks sont des instances de la classe Proc. La solution est facile, il suffit de créer des instances de Proc et les passer comme blocks d&#8217;une méthode acceptant un block.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">p1 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
 <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Je suis p1&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
p2 = <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Je suis p2&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
bar = Bar.<span style="color:#9900CC;">new</span>
bar.<span style="color:#9900CC;">my_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>p1<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># Je suis p1</span>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span>
bar.<span style="color:#9900CC;">foo_proc</span> 
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x164564ab&gt;</span>
&nbsp;
bar.<span style="color:#9900CC;">recall</span>
<span style="color:#008000; font-style:italic;"># Je suis p1</span>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span>
&nbsp;
bar.<span style="color:#9900CC;">my_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>p2<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># Je suis p2</span>
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x054ecdab&gt;</span>
&nbsp;
bar.<span style="color:#9900CC;">recall</span>
<span style="color:#008000; font-style:italic;"># Je suis p2</span>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span>
&nbsp;
bar2 = Bar.<span style="color:#9900CC;">new</span>
bar2.<span style="color:#9900CC;">my_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>p2<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># Je suis p2</span>
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x054ecdab&gt;</span>
&nbsp;
bar2.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@foo_proc&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> == bar.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@foo_proc&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
<span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></div></div>

<p>Vous avez compris l&#8217;intérêt? Les blocks sont bien des suites d&#8217;instructions mais plus que cela, ce sont des objets !!!  L&#8217;illustration précédente montre qu&#8217;on peut partager des suites d&#8217;instructions entre différents objets, générer ces instructions dynamiquement ! </p>
<p>En route pour la méta-programmation avec Ruby !!!</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/07/24/blocks-are-proc-les-procedures-proc-et-les-blocks-en-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

