<?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>Sat, 25 Jun 2011 20:33:32 +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>Monit and Phusion Passenger</title>
		<link>http://blog.gugl.org/archives/120</link>
		<comments>http://blog.gugl.org/archives/120#comments</comments>
		<pubDate>Fri, 29 Apr 2011 22:21:24 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[integration]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[monit]]></category>
		<category><![CDATA[passenger]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=120</guid>
		<description><![CDATA[While there&#8217;re multiple tools available for monitoring, ranging from Dan Bernshtein&#8217;s daemontools to God, I found that Monit is a pretty balanced solution. On one hand, it&#8217;s quite powerful, and on the other &#8211; it&#8217;s not resource hungry. The only drawback that I found, is that for monitoring a process, the process must have a [...]]]></description>
			<content:encoded><![CDATA[<p>While there&#8217;re multiple tools available for monitoring, ranging from Dan Bernshtein&#8217;s <a href="http://cr.yp.to/daemontools.html">daemontools</a> to <a href="http://god.rubyforge.org/">God</a>, I found that <a href="http://mmonit.com/monit/">Monit</a> is a pretty balanced solution. On one hand, it&#8217;s quite powerful, and on the other &#8211; it&#8217;s not resource hungry. The only drawback that I found, is that for monitoring a process, the process must have a pid-file saved somewhere. The Monit FAQ states that the programs which don&#8217;t have pid-file support should be run with some wrapper which will create it on their behalf.</p>
<p>I wanted to use Monit to monitor Rails&#8217; instances, so if they grow too fat, Monit will take care of that. The problem is that the pid file that the <a href="http://www.modrails.com/">Passenger</a> creates a) doesn&#8217;t have a predictable location (there&#8217;s something called &#8220;generation&#8221; or something like that), and b) doesn&#8217;t have children, as those processes are detached.</p>
<p>Of course there exists a possibility of patching Passenger for providing such pid files. But, luckily, Passenger provides extension points in form of callbacks which are fired when the application instance is created and when it&#8217;s taken down.</p>
<p>It was trivial to use this API to provide pid-file managing. The result of this work was the &#8220;passenger_monit&#8221; plugin.</p>
<p>So, go ahead, add &#8216;gem &#8220;passenger_monit&#8221;&#8216; to your Gemfile and give it a try!</p>
<p>The source is available from:<br />
<a href="https://github.com/romanbsd/passenger_monit">https://github.com/romanbsd/passenger_monit</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/120/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title></title>
		<link>http://blog.gugl.org/archives/114</link>
		<comments>http://blog.gugl.org/archives/114#comments</comments>
		<pubDate>Tue, 05 Apr 2011 09:46:54 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=114</guid>
		<description><![CDATA[Logging line numbers in Ruby Logger can be done with a simple decorator: class LoggerDecorator def initialize&#40;logger&#41; @logger = logger end &#160; %w&#123;debug info warn error fatal&#125;.each do &#124;method&#124; eval&#40;&#60;&#60;-eomethod&#41; def #{method}(msg) @logger.#{method}(position) {msg} end eomethod end &#160; private def position caller.at&#40;1&#41;.sub&#40;%r&#123;.*/&#125;,''&#41;.sub&#40;%r&#123;:in\s.*&#125;,''&#41; end end]]></description>
			<content:encoded><![CDATA[<p>Logging line numbers in Ruby Logger can be done with a simple decorator:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">class</span> LoggerDecorator
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>logger<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@logger</span> = logger
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>debug info warn error fatal<span style="color:#006600; font-weight:bold;">&#125;</span>.<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>
      <span style="color:#CC0066; font-weight:bold;">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>eomethod<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;">#{method}(msg)</span>
          <span style="color:#0066ff; font-weight:bold;">@logger</span>.<span style="color:#008000; font-style:italic;">#{method}(position) {msg}</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      eomethod
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    private
    <span style="color:#9966CC; font-weight:bold;">def</span> position
      <span style="color:#CC0066; font-weight:bold;">caller</span>.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">sub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span>.<span style="color:#006600; font-weight:bold;">*/</span><span style="color:#006600; font-weight:bold;">&#125;</span>,<span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">sub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span>:<span style="color:#9966CC; font-weight:bold;">in</span>\s.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#125;</span>,<span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/114/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ExtJS with Rails 3</title>
		<link>http://blog.gugl.org/archives/106</link>
		<comments>http://blog.gugl.org/archives/106#comments</comments>
		<pubDate>Sun, 06 Mar 2011 10:37:41 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[extjs]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://blog.gugl.org/?p=106</guid>
		<description><![CDATA[In order to make ExtJS play nicely with Rails, the following tweaks are needed: On the ExtJS javascript side, the following things are needed: 1. Ask server to server JSON: Ext.Ajax.defaultHeaders = &#123;'Accept': 'application/json'&#125;; 2. RailsJsonStore: Ext.data.RailsJsonStore = Ext.extend&#40;Ext.data.JsonStore, &#123; constructor: function&#40;config&#41; &#123; Ext.data.RailsJsonStore.superclass.constructor.call&#40;this, Ext.applyIf&#40;config, &#123; messageProperty: 'message', //for store.reader.getMessage() restful: true, url: '/'+ config&#91;'root'&#93; [...]]]></description>
			<content:encoded><![CDATA[<p>In order to make ExtJS play nicely with Rails, the following tweaks are needed:</p>
<p>On the ExtJS javascript side, the following things are needed:</p>
<p>1. Ask server to server JSON:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">Ajax</span>.<span style="color: #660066;">defaultHeaders</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Accept'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'application/json'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>2. RailsJsonStore:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">data</span>.<span style="color: #660066;">RailsJsonStore</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">data</span>.<span style="color: #660066;">JsonStore</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    constructor<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Ext.<span style="color: #660066;">data</span>.<span style="color: #660066;">RailsJsonStore</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">constructor</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> Ext.<span style="color: #660066;">applyIf</span><span style="color: #009900;">&#40;</span>config<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
            messageProperty<span style="color: #339933;">:</span> <span style="color: #3366CC;">'message'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">//for store.reader.getMessage()</span>
            restful<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
            url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">+</span> config<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'root'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'s'</span><span style="color: #339933;">,</span>
            writer<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>encode<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Ext.<span style="color: #660066;">reg</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'railsjsonstore'</span><span style="color: #339933;">,</span> Ext.<span style="color: #660066;">data</span>.<span style="color: #660066;">RailsJsonStore</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>3. XSRF protection:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">Ajax</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'beforerequest'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> csrf <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;meta[name='csrf-token']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">first</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>csrf<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                o.<span style="color: #660066;">defaultHeaders</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">defaultHeaders</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'X-CSRF-Token'</span><span style="color: #339933;">:</span> csrf.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>On Rails side:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">active_record</span>.<span style="color:#9900CC;">include_root_in_json</span> = <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.gugl.org/archives/106/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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[i18n]]></category>
		<category><![CDATA[Rails]]></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 403<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;">@@ -114,7 +114,7 @@</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;">@@ -123,7 +123,7 @@</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> <span style="color:#006666;">10</span>.<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;">@@ -65,7 +65,11 @@ 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;= 2 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> <span style="color:#006666;">10</span>.<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>3</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>
	</channel>
</rss>

