<?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; Uncategorized</title>
	<atom:link href="http://my.rails-royce.org/category/uncategorized/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>HTTPClient and SSL verify certificate</title>
		<link>http://my.rails-royce.org/2011/01/12/httpclient-and-ssl-verify-certificate/</link>
		<comments>http://my.rails-royce.org/2011/01/12/httpclient-and-ssl-verify-certificate/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 11:58:51 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[gem]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[certificate]]></category>
		<category><![CDATA[httpclient]]></category>
		<category><![CDATA[openssl]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=255</guid>
		<description><![CDATA[If you as us in my company use self signed certificate and ran into an OpenSSL::SSL::SSLError when using httpclient gem. Here is how to bypass ssl certificate verification : The issue : require 'rubygems' require 'httpclient' &#160; client = HTTPClient.new url = &#34;https://www.server1.com&#34; client.get&#40;url&#41; at depth 1 - 19: self signed certificate in certificate chain &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2011/01/12/httpclient-and-ssl-verify-certificate/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>If you as us in my company use self signed certificate and ran into an OpenSSL::SSL::SSLError when using httpclient gem.</p>
<p>Here is how to bypass ssl certificate verification : </p>
<p>The issue :</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;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'httpclient'</span>
&nbsp;
client = HTTPClient.<span style="color:#9900CC;">new</span>
url = <span style="color:#996600;">&quot;https://www.server1.com&quot;</span>
client.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span>
at depth <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">19</span>: <span style="color:#0000FF; font-weight:bold;">self</span> signed certificate <span style="color:#9966CC; font-weight:bold;">in</span> certificate chain
<span style="color:#6666ff; font-weight:bold;">OpenSSL::SSL::SSLError</span>: SSL_connect returned=<span style="color:#006666;">1</span> errno=<span style="color:#006666;">0</span> state=SSLv3 read server certificate B: certificate verify failed
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">/</span>session.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">247</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`connect'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient/session.rb:247:in `</span>ssl_connect<span style="color:#996600;">'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient/session.rb:639:in `connect'</span>
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">/</span>timeout.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">128</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`timeout'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient/session.rb:631:in `</span>connect<span style="color:#996600;">'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient/session.rb:522:in `query'</span>
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">/</span>session.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">147</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`query'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient.rb:953:in `</span>do_get_block<span style="color:#996600;">'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient.rb:765:in `do_request'</span>
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">848</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`protect_keep_alive_disconnected'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient.rb:764:in `</span>do_request<span style="color:#996600;">'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient.rb:666:in `request'</span>
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">591</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`get'
	from (irb):5</span></pre></div></div>

<p>Solution :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">client.<span style="color:#9900CC;">ssl_config</span>.<span style="color:#9900CC;">verify_mode</span> = <span style="color:#6666ff; font-weight:bold;">OpenSSL::SSL::VERIFY_NONE</span>
client.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2011/01/12/httpclient-and-ssl-verify-certificate/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>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>Ordered Hash by keys</title>
		<link>http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/</link>
		<comments>http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 09:15:50 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[activesupport]]></category>
		<category><![CDATA[active_support]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=111</guid>
		<description><![CDATA[Recently, I have to implement my own Hash sorting method. Not really with Hash but with ActiveSupport::OrderedHash I don&#8217;t like the way ActiveSupport::OrderedHash behaves with sorting functionality. It just behaves like a simple Hash I would like that sort return an ActiveSupport::OrderedHash not an Arrray!!! Why is it called OrderedHash in this case ? I &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Recently, I have to implement my own Hash sorting method. Not really with Hash but with <strong><em>ActiveSupport::OrderedHash</em></strong></p>
<ul>
<ol>I don&#8217;t like the way <strong><em>ActiveSupport::OrderedHash</em></strong> behaves with sorting functionality.</ol>
<ol>  It just behaves like a simple <strong><em>Hash</em></strong></ol>
<ol>I would like that sort return an  <strong><em>ActiveSupport::OrderedHash</em></strong> not an <strong><em>Arrray</em></strong>!!! Why is it called OrderedHash in this case ?</ol>
<ol>I would like to sort with keys only, not with an array [key, value]</ol>
</ul>
<p>Ok no matter, I implemented my own new <strong><em>SimpleOrderedHash</em></strong> :</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/ordered_hash'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> SimpleOrderedHash <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::OrderedHash</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Lets reimplement sort method</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> sort<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>
    h = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">dup</span>
    h.<span style="color:#9900CC;">sort</span>!<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>
    h
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Lets implement sort! method</span>
  <span style="color:#008000; font-style:italic;"># It uses @keys variable to do the sorting</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> sort!<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:#0066ff; font-weight:bold;">@keys</span>.<span style="color:#9900CC;">sort</span>!<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>
    sync_keys!
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># A fancy inspect   </span>
  <span style="color:#9966CC; font-weight:bold;">def</span> inspect
    <span style="color:#996600;">&quot;#&lt;#{self.class.name} #{Hash.instance_method(:inspect).bind(self).call}&gt;&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can now use it like that :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">h = SimpleOrderedHash.<span style="color:#9900CC;">new</span>
h.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">'second'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#996600;">'first'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">5</span>, <span style="color:#996600;">'third'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2.5</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;SimpleOrderedHash {&quot;third&quot;=&gt;2.5, &quot;second&quot;=&gt;1, &quot;first&quot;=&gt;5}&gt;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># sort return a SimpleOrderedHash</span>
result = h.<span style="color:#9900CC;">sort</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span>
  h<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>b<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;SimpleOrderedHash {&quot;third&quot;=&gt;2.5, &quot;second&quot;=&gt;1, &quot;first&quot;=&gt;5}&gt;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># and order as I wanted</span>
result.<span style="color:#9900CC;">each</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:#CC0066; font-weight:bold;">puts</span> v <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006666;">1</span>
<span style="color:#006666;">2.5</span>
<span style="color:#006666;">5</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;second&quot;</span>, <span style="color:#996600;">&quot;third&quot;</span>, <span style="color:#996600;">&quot;first&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># sort! change internally the order of a SimpleOrderedHash</span>
h.<span style="color:#9900CC;">sort</span>! <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span>
  h<span style="color:#006600; font-weight:bold;">&#91;</span>b<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
h.<span style="color:#9900CC;">each</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:#CC0066; font-weight:bold;">puts</span> v <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006666;">5</span>
<span style="color:#006666;">2.5</span>
<span style="color:#006666;">1</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;first&quot;</span>, <span style="color:#996600;">&quot;third&quot;</span>, <span style="color:#996600;">&quot;second&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

</pre>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/feed/</wfw:commentRss>
		<slash:comments>0</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>
	</channel>
</rss>

