<?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>Roman&#039;s tech blog &#187; Rails</title>
	<atom:link href="http://blog.gugl.org/archives/category/ruby/rails/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.gugl.org</link>
	<description>ruby, perl and other stuff</description>
	<lastBuildDate>Thu, 17 Jun 2010 12:19:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>I18n support for validates_date_time</title>
		<link>http://blog.gugl.org/archives/77</link>
		<comments>http://blog.gugl.org/archives/77#comments</comments>
		<pubDate>Sun, 08 Feb 2009 10:39:29 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[i18n]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=77</guid>
		<description><![CDATA[This simple fix uses the fact, that if the second argument to the &#8216;add&#8217; method is a symbol, then a generate_message is called, which does all the voodoo of Rails 2.2 I18n. I tried to contact the original author to no avail, therefore I&#8217;ll post it here, hoping that if anyone needs it, it&#8217;ll surface [...]]]></description>
			<content:encoded><![CDATA[<p>This simple fix uses the fact, that if the second argument to the &#8216;add&#8217; method is a symbol, then a generate_message is called, which does all the voodoo of Rails 2.2 I18n.<br />
I tried to contact the original author to no avail, therefore I&#8217;ll post it here, hoping that if anyone needs it, it&#8217;ll surface on a Google search.<br />
The fix itself is quite simple:</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;">Index: lib/validates_date_time.rb
===================================================================
<span style="color: #888822;">--- lib/validates_date_time.rb  <span style="">&#40;</span>revision <span style="">403</span><span style="">&#41;</span></span>
<span style="color: #888822;">+++ lib/validates_date_time.rb  <span style="">&#40;</span>working copy<span style="">&#41;</span></span>
<span style="color: #440088;">@@ -<span style="">114</span>,<span style="">7</span> +<span style="">114</span>,<span style="">7</span> @@</span>
       if options<span style="">&#91;</span>:before<span style="">&#93;</span>
         options<span style="">&#91;</span>:before<span style="">&#93;</span>.each do |r|
           if r.value<span style="">&#40;</span>record<span style="">&#41;</span> and value &gt;= r.last_value
<span style="color: #991111;">-            record.errors.add<span style="">&#40;</span>attr_name, options<span style="">&#91;</span>:before_message<span style="">&#93;</span> % r<span style="">&#41;</span></span>
<span style="color: #00b000;">+            record.errors.add<span style="">&#40;</span>attr_name, :before, :default =&gt; options<span style="">&#91;</span>:before_message<span style="">&#93;</span> % r<span style="">&#41;</span></span>
             break
           end
         end
<span style="color: #440088;">@@ -<span style="">123</span>,<span style="">7</span> +<span style="">123</span>,<span style="">7</span> @@</span>
       if options<span style="">&#91;</span>:after<span style="">&#93;</span>
         options<span style="">&#91;</span>:after<span style="">&#93;</span>.each do |r|
           if r.value<span style="">&#40;</span>record<span style="">&#41;</span> and value &lt;= r.last_value
<span style="color: #991111;">-            record.errors.add<span style="">&#40;</span>attr_name, options<span style="">&#91;</span>:after_message<span style="">&#93;</span> % r<span style="">&#41;</span></span>
<span style="color: #00b000;">+            record.errors.add<span style="">&#40;</span>attr_name, :after, :default =&gt; options<span style="">&#91;</span>:after_message<span style="">&#93;</span> % r<span style="">&#41;</span></span>
             break
           end
         end</pre></div></div>

<p>I&#8217;ve a fork of the git-svn mirror here:<br />
<a href='http://github.com/romanbsd/validates_date_time/tree/master'>http://github.com/romanbsd/validates_date_time/tree/master</a></p>
<p>Then, for using it, you&#8217;ll just need to have the following path in your locale yml files (for example):</p>
<pre>
  activerecord:
    errors:
      models:
        user:
          attributes:
            birth_date:
              before: 'is wrong'
              after: 'is wrong'
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/77/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Displaying recaptcha inside ModalBox</title>
		<link>http://blog.gugl.org/archives/72</link>
		<comments>http://blog.gugl.org/archives/72#comments</comments>
		<pubDate>Fri, 30 Jan 2009 13:22:45 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[modalbox]]></category>
		<category><![CDATA[recaptcha]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=72</guid>
		<description><![CDATA[I just wanted to display reCAPTCHA inside a ModalBox. This simple task turned out to be quite complicated. After playing with innerHTML, creating &#60;script&#62; nodes manually and attaching them and other voodoo and black magic and failing to accomplish the desired effect, I settled with displaying the recaptcha inside iframe inside ModalBox. Modalbox.show&#40;'&#60;iframe src=&#34;' + [...]]]></description>
			<content:encoded><![CDATA[<p>I just wanted to display <a href='http://www.recaptcha.net/'>reCAPTCHA</a> inside a <a href='http://www.wildbit.com/labs/modalbox/'>ModalBox</a>.<br />
This simple task turned out to be quite complicated.<br />
After playing with innerHTML, creating &lt;script&gt; nodes manually and attaching them and other voodoo and black magic and failing to accomplish the desired effect, I settled with displaying the recaptcha inside iframe inside ModalBox.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> Modalbox.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;iframe src=&quot;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'?iframe=1&quot;&gt;&lt;/iframe&gt;'</span><span style="color: #339933;">,</span> 
  <span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">title</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">600</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(The iframe parameter helps to select another layout).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/72/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Expire cache item in Rails ActiveSupport::Cache::FileStore</title>
		<link>http://blog.gugl.org/archives/65</link>
		<comments>http://blog.gugl.org/archives/65#comments</comments>
		<pubDate>Fri, 30 Jan 2009 13:11:51 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[activesupport]]></category>
		<category><![CDATA[caching]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=65</guid>
		<description><![CDATA[Wouldn&#8217;t it be nice to cache some item (e.g. action) for some amount of time, before expiring it? For example, if you&#8217;re displaying some page that parts of it change very often, but you don&#8217;t want to expire cache every time, since you prefer to serve stale information instead of expiring the page? The answer [...]]]></description>
			<content:encoded><![CDATA[<p>Wouldn&#8217;t it be nice to cache some item (e.g. action) for some amount of time, before expiring it? For example, if you&#8217;re displaying some page that parts of it change very often, but you don&#8217;t want to expire cache every time, since you prefer to serve stale information instead of expiring the page?<br />
The answer is the options[:expires_in] which is supported by the Rails&#8217; ActiveSupport::Cache::MemCacheStore.<br />
The current implementation of ActiveSupport::Cache::FileStore, however, lacks such feature, despite the fact that it&#8217;s really easy to implement.</p>
<p>This is the fix accompanied by a unit-test.<br />
<a href='http://blog.gugl.org/wp-content/uploads/2009/01/cache_fix.diff'>cache_fix</a></p>
<p>And then you can happily add a line like this in your controllers:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">caches_action <span style="color:#ff3333; font-weight:bold;">:find</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 10.<span style="color:#9900CC;">minute</span></pre></div></div>

<p>The check, by the way, adds about 7usec to cache read request, which IMHO is negligible.</p>
<p>The Rails core team <a href='http://rails.lighthouseapp.com/projects/8994/tickets/1693'>doesn&#8217;t seem to be interested</a> in this patch, though.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/65/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I18n support for recaptcha</title>
		<link>http://blog.gugl.org/archives/58</link>
		<comments>http://blog.gugl.org/archives/58#comments</comments>
		<pubDate>Thu, 22 Jan 2009 22:46:13 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[recaptcha]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=58</guid>
		<description><![CDATA[There&#8217;s a nice plugin which adds helper for the reCAPTCHA. The downside is that it doesn&#8217;t support I18n at the moment. This is a straight forward approach patch that adds this functionality. (I don&#8217;t know what will be the fate of I18n in Rails 3, thus the simple check for version). The error message path [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a nice <a href='http://ambethia.com/recaptcha/'>plugin</a> which adds helper for the reCAPTCHA. The downside is that it doesn&#8217;t support I18n at the moment.<br />
This is a straight forward approach patch that adds this functionality. (I don&#8217;t know what will be the fate of I18n in Rails 3, thus the simple check for version).<br />
The error message path then will be:<br />
activerecord.errors.models.<i>model_name</i>.captcha</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;">diff --git a/lib/recaptcha.rb b/lib/recaptcha.rb
index <span style="color: #440088;">87c26</span>e9..44413bc <span style="">100644</span>
<span style="color: #888822;">--- a/lib/recaptcha.rb</span>
<span style="color: #888822;">+++ b/lib/recaptcha.rb</span>
<span style="color: #440088;">@@ -<span style="">65</span>,<span style="">7</span> +<span style="">65</span>,<span style="">11</span> @@ module Ambethia</span>
             session<span style="">&#91;</span>:recaptcha_error<span style="">&#93;</span> = error
             if model = options<span style="">&#91;</span>:model<span style="">&#93;</span>
               model.valid?
<span style="color: #991111;">-              model.errors.add_to_base &quot;Captcha response is incorrect, please try again.&quot;</span>
<span style="color: #00b000;">+              if Rails::VERSION::MAJOR &gt;= <span style="">2</span> and Rails::VERSION::MINOR &gt;= 2</span>
<span style="color: #00b000;">+                model.errors.add_to_base I18n.translate<span style="">&#40;</span>&quot;#<span style="">&#123;</span>model.class.name.underscore<span style="">&#125;</span>.captcha&quot;, :scope =&gt; %w<span style="">&#40;</span>activerecord errors models<span style="">&#41;</span>, :default =&gt; &quot;Captcha response is incorrect, please try again.&quot;<span style="">&#41;</span></span>
<span style="color: #00b000;">+              else</span>
<span style="color: #00b000;">+                model.errors.add_to_base &quot;Captcha response is incorrect, please try again.&quot;</span>
<span style="color: #00b000;">+              end</span>
             end
             return false
           else</pre></div></div>

<p>I wonder if this is the proper way of adding I18n support&#8230;<br />
P.S. I reported this <a href='http://ambethia.lighthouseapp.com/projects/11072-recaptcha/tickets/7'>on lighthouse.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/58/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Caching of complex queries</title>
		<link>http://blog.gugl.org/archives/51</link>
		<comments>http://blog.gugl.org/archives/51#comments</comments>
		<pubDate>Tue, 06 Jan 2009 15:58:23 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[actioncontroller]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=51</guid>
		<description><![CDATA[This trick is great for actions which receive lots of parameters, for example &#8211; search controller. Obviously, you wouldn&#8217;t want to do that with something that receives a POST, but I omitted the check for it (using :if => Proc.new {&#124;controller&#124; controller.request.get?} , for example), since I think that I know what I&#8217;m doing. So [...]]]></description>
			<content:encoded><![CDATA[<p>This trick is great for actions which receive lots of parameters, for example &#8211; search controller. Obviously, you wouldn&#8217;t want to do that with something that receives a POST, but I omitted the check for it (using :if => Proc.new {|controller| controller.request.get?} , for example), since I think that I know what I&#8217;m doing.<br />
So the idea is to map all the parameters to some unique key, and then use that key as an :id for the url_for.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># This caches the find action by the parameters</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'digest/sha1'</span>
  caches_action <span style="color:#ff3333; font-weight:bold;">:find</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 10.<span style="color:#9900CC;">minute</span>,
    <span style="color:#ff3333; font-weight:bold;">:cache_path</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>controller<span style="color:#006600; font-weight:bold;">|</span>
        search_key = <span style="color:#6666ff; font-weight:bold;">Digest::SHA1</span>.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span>controller.<span style="color:#9900CC;">params</span>.<span style="color:#9900CC;">inspect</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        logger.<span style="color:#9900CC;">debug</span> <span style="color:#996600;">&quot;Current search key: #{search_key}&quot;</span>
        controller.<span style="color:#9900CC;">url_for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'find'</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> search_key<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The only minor issue that might arise, is that if you&#8217;re using pagination, the page number 1 when reached from the pagination links will have page=1 parameter, but when it&#8217;s initially presented, the parameter is absent. Adding such parameter won&#8217;t do any good, since the order of keys in hash in undefined. It&#8217;s possible, though, to build the key using some other technique, for example by massaging the request string. But since the case is quite rare, I think that any fix will be a waste of CPU cycles.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/51/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Better logging in Rails in production mode</title>
		<link>http://blog.gugl.org/archives/47</link>
		<comments>http://blog.gugl.org/archives/47#comments</comments>
		<pubDate>Sun, 28 Dec 2008 15:03:22 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[logger]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=47</guid>
		<description><![CDATA[I&#8217;m quite surprised, that even of version 2.2.2 of Rails, the logs are written in a way that makes them almost useless, as there&#8217;s no time stamp or severity level. The solution I&#8217;ve come up with, is (instead of monkey patching the BufferedLogger) to create a new type of logger which inherits from the BufferedLogger [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m quite surprised, that even of version 2.2.2 of Rails, the logs are written in a way that makes them almost useless, as there&#8217;s no time stamp or severity level.<br />
The solution I&#8217;ve come up with, is (instead of monkey patching the BufferedLogger) to create a new type of logger which inherits from the BufferedLogger and then change the &#8216;add&#8217; method in order to produce nicer logs.<br />
This file is lib/audit_logger.rb :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> AuditLogger <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::BufferedLogger</span>
&nbsp;
  SEVERITIES = Severity.<span style="color:#9900CC;">constants</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</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;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>arr,c<span style="color:#006600; font-weight:bold;">|</span> arr<span style="color:#006600; font-weight:bold;">&#91;</span>Severity.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = c; arr<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Redefine the ActiveSupport::BufferedLogger#add</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> add<span style="color:#006600; font-weight:bold;">&#40;</span>severity, message = <span style="color:#0000FF; font-weight:bold;">nil</span>, progname = <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@level</span> <span style="color:#006600; font-weight:bold;">&gt;</span> severity
    message = <span style="color:#006600; font-weight:bold;">&#40;</span>message <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#40;</span>block <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> block.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> progname<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span>
    <span style="color:#008000; font-style:italic;"># This is the line that was changed:</span>
    message = <span style="color:#996600;">&quot;#{Time.now.to_formatted_s(:db)} #{SEVERITIES[severity]} #{message.strip}<span style="color:#000099;">\n</span>&quot;</span>
    buffer <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> message
    auto_flush
    message
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then, in config/environments/production.rb you need to add:</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;">'audit_logger'</span>
config.<span style="color:#9900CC;">logger</span> = AuditLogger.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>config.<span style="color:#9900CC;">log_path</span><span style="color:#006600; font-weight:bold;">&#41;</span>
config.<span style="color:#9900CC;">logger</span>.<span style="color:#9900CC;">level</span> = <span style="color:#6666ff; font-weight:bold;">AuditLogger::INFO</span> <span style="color:#008000; font-style:italic;">#or other level</span>
<span style="color:#008000; font-style:italic;"># This is what Rails does by default in production mode when using the default logger</span>
config.<span style="color:#9900CC;">logger</span>.<span style="color:#9900CC;">auto_flushing</span> = <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/47/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Improved Rails&#8217; delegate method</title>
		<link>http://blog.gugl.org/archives/41</link>
		<comments>http://blog.gugl.org/archives/41#comments</comments>
		<pubDate>Sun, 07 Sep 2008 11:38:43 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[delegation]]></category>
		<category><![CDATA[demeter]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=41</guid>
		<description><![CDATA[The Rails framework includes a simple &#8220;delegate&#8221; method, which is mixed in the models, and can be used in order to delegate methods to other classes. However it&#8217;s limited at least in the following: 1. It doesn&#8217;t prevent &#8220;train wrecks&#8221; &#8211; e.g. if the class that the method is delegated to is nil, then there [...]]]></description>
			<content:encoded><![CDATA[<p>The Rails framework includes a simple &#8220;delegate&#8221; method, which is mixed in the models, and can be used in order to delegate methods to other classes. However it&#8217;s limited at least in the following:<br />
1. It doesn&#8217;t prevent &#8220;train wrecks&#8221; &#8211; e.g. if the class that the method is delegated to is nil, then there will be an attempt to call this method on the nil class.<br />
2. It doesn&#8217;t allow renaming of the methods, e.g. if you have a method called &#8220;name&#8221; in your class, and a method called &#8220;name&#8221; in other class you want to delegate to, you have no means of doing this.<br />
The implementation below remedies these shortcomings.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#9966CC; font-weight:bold;">Module</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> delegate<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>methods<span style="color:#006600; font-weight:bold;">&#41;</span>
    options = methods.<span style="color:#9900CC;">pop</span>
    <span style="color:#9966CC; font-weight:bold;">unless</span> options.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> to = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:to</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>, <span style="color:#996600;">&quot;Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to =&gt; :greeter).&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    prefix = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:prefix</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">''</span>
    orig_prefix = prefix
    <span style="color:#9966CC; font-weight:bold;">if</span> prefix != <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">and</span> !prefix.<span style="color:#9900CC;">empty</span>? 
      prefix <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#996600;">'_'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    methods.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>method<span style="color:#006600; font-weight:bold;">|</span>
      prefix = to.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'_'</span> <span style="color:#9966CC; font-weight:bold;">if</span> orig_prefix == <span style="color:#0000FF; font-weight:bold;">true</span>
      module_eval<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS, <span style="color:#996600;">&quot;(__DELEGATION__)&quot;</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{prefix}#{method}(*args, &amp;block)</span>
          <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#008000; font-style:italic;">#{to}</span>
          <span style="color:#008000; font-style:italic;">#{to}.__send__(#{method.inspect}, *args, &amp;block)</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      EOS
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">delegate <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:to</span><span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:other</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span><span style="color:#006600; font-weight:bold;">=&gt;</span>true</pre></div></div>

<p>and it will delegate a method &#8220;name&#8221; in &#8220;other&#8221; class as &#8220;other_name&#8221; in your class.<br />
Or, alternatively</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">delegate <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:to</span><span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:other</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'some'</span></pre></div></div>

<p>will delegate &#8220;some_name&#8221; to class &#8220;other&#8221;, method &#8220;name&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/41/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Extensions for acts_as_tree</title>
		<link>http://blog.gugl.org/archives/38</link>
		<comments>http://blog.gugl.org/archives/38#comments</comments>
		<pubDate>Sat, 30 Aug 2008 16:51:40 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[acts_as_tree]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=38</guid>
		<description><![CDATA[While the acts_as_tree rails plugin ( http://dev.rubyonrails.com/svn/rails/plugins/acts_as_tree ) provides some basic methods for accessing tree like data structure, I found that it lacks some basic ones, for example &#8211; has_children? and has_siblings? Other than that, it would have been nice to know the tree width and depth (especially if you need to draw it, and [...]]]></description>
			<content:encoded><![CDATA[<p>While the acts_as_tree rails plugin ( <a href="http://dev.rubyonrails.com/svn/rails/plugins/acts_as_tree">http://dev.rubyonrails.com/svn/rails/plugins/acts_as_tree</a> ) provides some basic methods for accessing tree like data structure, I found that it lacks some basic ones, for example &#8211; has_children? and has_siblings?<br />
Other than that, it would have been nice to know the tree width and depth (especially if you need to draw it, and need to calculate the scaling.</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;">module</span> Acts
    <span style="color:#9966CC; font-weight:bold;">module</span> Tree
      <span style="color:#9966CC; font-weight:bold;">module</span> InstanceMethods
        <span style="color:#9966CC; font-weight:bold;">def</span> has_children?
          !<span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">children</span>.<span style="color:#9900CC;">size</span>.<span style="color:#9900CC;">zero</span>?
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> has_siblings?
          !<span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">siblings</span>.<span style="color:#9900CC;">size</span>.<span style="color:#9900CC;">zero</span>?
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># Return the tree depth</span>
        <span style="color:#9966CC; font-weight:bold;">def</span> depth
          <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">unless</span> has_children?
          children.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</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>dep,c<span style="color:#006600; font-weight:bold;">|</span> dep <span style="color:#006600; font-weight:bold;">&gt;</span> c.<span style="color:#9900CC;">depth</span> ? dep : c.<span style="color:#9900CC;">depth</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># Return the tree width</span>
        <span style="color:#9966CC; font-weight:bold;">def</span> width
          <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">unless</span> has_children?
          children.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</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>sum,c<span style="color:#006600; font-weight:bold;">|</span> sum <span style="color:#006600; font-weight:bold;">+</span> c.<span style="color:#9900CC;">width</span><span style="color:#006600; font-weight:bold;">&#125;</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>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Maybe if I won&#8217;t be lazy, I&#8217;ll write some tests and send this for inclusion in the plugin&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/38/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integrating Rails and dhtmlxTabbar</title>
		<link>http://blog.gugl.org/archives/14</link>
		<comments>http://blog.gugl.org/archives/14#comments</comments>
		<pubDate>Tue, 24 Jun 2008 08:29:37 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[dhtmlx]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/archives/14</guid>
		<description><![CDATA[The dhtmlxTabbar is a nice tab-bar component from http://www.dhtmlx.com/ In order to use the AJAX loading feature I had to make a small change in the dhtmlxcommon.js so it mimics the behavior of Prototype by adding a X-Requested-With HTTP header. Thus, it&#8217;s possible to use request.xhr? in Rails. dhtmlxcommon.js]]></description>
			<content:encoded><![CDATA[<p>The <strong>dhtmlxTabbar</strong> is a nice tab-bar component from http://www.dhtmlx.com/<br />
In order to use the AJAX loading feature I had to make a small change in the <em>dhtmlxcommon.js</em> so it mimics the behavior of Prototype by adding a X-Requested-With HTTP header. Thus, it&#8217;s possible to use request.xhr? in Rails.<br />
<a href='http://blog.gugl.org/wp-content/uploads/dhtmlxcommonjs.txt'>dhtmlxcommon.js</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/14/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Accessing Subversion from Rails in an ActiveRecord way</title>
		<link>http://blog.gugl.org/archives/8</link>
		<comments>http://blog.gugl.org/archives/8#comments</comments>
		<pubDate>Mon, 27 Aug 2007 14:10:23 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=8</guid>
		<description><![CDATA[I needed to present products and their corresponding branches and tags in Subversion in a Rails app. So I&#8217;ve come up with this quite simple solution. The GenericModel class (attached) is ihnerited by Tag, Branch and Product. The Product class supports methods find, branches and tags for which it uses &#8216;svn/client&#8217;. The config.yml (which is [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to present products and their corresponding branches and tags in Subversion in a Rails app. So I&#8217;ve come up with this quite simple solution.</p>
<p>The GenericModel class (attached) is ihnerited by Tag, Branch and Product.<br />
The Product class supports methods find, branches and tags for which it uses &#8216;svn/client&#8217;.</p>
<p>The config.yml (which is read into OpenStruct) is very simple:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">svn:
  url: http:<span style="color:#006600; font-weight:bold;">//</span>svn<span style="color:#006600; font-weight:bold;">/</span>svn<span style="color:#006600; font-weight:bold;">/</span>repos
  user: roman
  password: roman</pre></div></div>

<p>The Product class tries to provide interface similar to the ActiveRecord (of course I didn&#8217;t immitate everything, just the basic stuff). Implementing interfaces in a contract-less language is an adventure <img src='http://blog.gugl.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href='http://blog.gugl.org/wp-content/uploads/2007/08/generic_modelrb.txt' title='generic_modelrb.txt'>generic_model.rb</a></p>
<p><a href='http://blog.gugl.org/wp-content/uploads/2007/08/productrb.txt' title='productrb.txt'>product.rb</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/8/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
