<?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; ruby on rails</title>
	<atom:link href="http://my.rails-royce.org/tag/ruby-on-rails/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>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>Email validation in Ruby On Rails 3 or Active model without regexp</title>
		<link>http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/</link>
		<comments>http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 08:21:42 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[email]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[3.0]]></category>
		<category><![CDATA[active model]]></category>
		<category><![CDATA[activemodel]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[rails-3]]></category>
		<category><![CDATA[rails-3.0]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[rails3.0]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[validate]]></category>
		<category><![CDATA[validations]]></category>
		<category><![CDATA[validator]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=186</guid>
		<description><![CDATA[In Rails 3.0, you can use custom validator in your active_record model. So I wanted to manage email validations without regexp matching like others do. I find a new way to make this work thanks to ruby mail gem, a dependency of Rails 3.0 I use this with devise, see my blogpost here : Ruby &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>In Rails 3.0, you can use custom validator in your active_record model.<br />
So I wanted to manage email validations without regexp matching like others do.<br />
I find a new way to make this work thanks to ruby mail gem, a dependency of Rails 3.0</p>
<p>I use this with devise, see my blogpost here : <a href="http://my.rails-royce.org/2010/10/20/rails-how-to-skip-or-remove-validations-in-a-model/">Ruby Rails : How to bypass skip validation in Devise</a></p>
<p>Edit : Here is a gem <a href="https://github.com/hallelujah/valid_email" title="Github repository">https://github.com/hallelujah/valid_email</a></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>
  validates <span style="color:#ff3333; font-weight:bold;">:email</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>, <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:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Just put a file in app/validators/email_validator.rb</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;">'mail'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> EmailValidator <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::EachValidator</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> validate_each<span style="color:#006600; font-weight:bold;">&#40;</span>record,attribute,value<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      m = <span style="color:#6666ff; font-weight:bold;">Mail::Address</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We must check that value contains a domain and that value is an email address</span>
      r = m.<span style="color:#9900CC;">domain</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> m.<span style="color:#9900CC;">address</span> == value
      t = m.__send__<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:tree</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We need to dig into treetop</span>
      <span style="color:#008000; font-style:italic;"># A valid domain must have dot_atom_text elements size &gt; 1</span>
      <span style="color:#008000; font-style:italic;"># user@localhost is excluded</span>
      <span style="color:#008000; font-style:italic;"># treetop must respond to domain</span>
      <span style="color:#008000; font-style:italic;"># We exclude valid email values like &lt;user@localhost.com&gt;</span>
      <span style="color:#008000; font-style:italic;"># Hence we use m.__send__(tree).domain</span>
      r <span style="color:#006600; font-weight:bold;">&amp;&amp;</span>= <span style="color:#006600; font-weight:bold;">&#40;</span>t.<span style="color:#9900CC;">domain</span>.<span style="color:#9900CC;">dot_atom_text</span>.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e   
      r = <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    record.<span style="color:#9900CC;">errors</span><span style="color:#006600; font-weight:bold;">&#91;</span>attribute<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;is invalid&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> r
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>No regexp !! And beautiful !!</p>
<p>Here is the version without activerecord</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;">'active_model'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_support/all'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_model/validations'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mail'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> EmailValidator <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::EachValidator</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> validate_each<span style="color:#006600; font-weight:bold;">&#40;</span>record,attribute,value<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      m = <span style="color:#6666ff; font-weight:bold;">Mail::Address</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We must check that value contains a domain and that value is an email address</span>
      r = m.<span style="color:#9900CC;">domain</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> m.<span style="color:#9900CC;">address</span> == value
      t = m.__send__<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:tree</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># We need to dig into treetop</span>
      <span style="color:#008000; font-style:italic;"># A valid domain must have dot_atom_text elements size &gt; 1</span>
      <span style="color:#008000; font-style:italic;"># user@localhost is excluded</span>
      <span style="color:#008000; font-style:italic;"># treetop must respond to domain</span>
      <span style="color:#008000; font-style:italic;"># We exclude valid email values like &lt;user@localhost.com&gt;</span>
      <span style="color:#008000; font-style:italic;"># Hence we use m.__send__(tree).domain</span>
      r <span style="color:#006600; font-weight:bold;">&amp;&amp;</span>= <span style="color:#006600; font-weight:bold;">&#40;</span>t.<span style="color:#9900CC;">domain</span>.<span style="color:#9900CC;">dot_atom_text</span>.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      r = <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    record.<span style="color:#9900CC;">errors</span><span style="color:#006600; font-weight:bold;">&#91;</span>attribute<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;is invalid&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> r
  <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;">class</span> Person
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::Validations</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>
&nbsp;
  validates <span style="color:#ff3333; font-weight:bold;">:name</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>, <span style="color:#ff3333; font-weight:bold;">:length</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;">:maximum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">100</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  validates <span style="color:#ff3333; font-weight:bold;">:email</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>, <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:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>

