<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dynamic and Concise &#187; ruby on rails</title>
	<atom:link href="http://yiwenandsoftware.wordpress.com/tag/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://yiwenandsoftware.wordpress.com</link>
	<description>Everything about my experience with Software Development</description>
	<lastBuildDate>Sat, 14 Mar 2009 22:05:25 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='yiwenandsoftware.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/fa9ad49ca1c5d02890788e46a95fe979?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Dynamic and Concise &#187; ruby on rails</title>
		<link>http://yiwenandsoftware.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://yiwenandsoftware.wordpress.com/osd.xml" title="Dynamic and Concise" />
		<item>
		<title>Run Rspec within vim and seeing result in your Firefox2</title>
		<link>http://yiwenandsoftware.wordpress.com/2009/03/11/run-rspec-within-vim-and-seeing-result-in-your-firefox2/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2009/03/11/run-rspec-within-vim-and-seeing-result-in-your-firefox2/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 19:05:19 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[IDE]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[RSpec]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/?p=75</guid>
		<description><![CDATA[Thanks to cassiomarques, we can run rspec from within vim and see the results in FF.
You might be surprised though, when you run it and wait and wait, and nothing seems happening. Mostly likely it just that the running code raise an exception and no output to either FF or you vim. If you look [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=75&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Thanks to <a href="http://cassiomarques.wordpress.com/2009/01/09/running-rspec-files-from-vim-showing-the-results-in-firefox/">cassiomarques</a>, we can run rspec from within vim and see the results in FF.</p>
<p>You might be surprised though, when you run it and wait and wait, and nothing seems happening. Mostly likely it just that the running code raise an exception and no output to either FF or you vim. If you look at the terminal window where you started you vim, you will see the backtrace though. So why not display the backtrace in FF then, just like the image below?</p>
<div id="attachment_76" class="wp-caption alignnone" style="width: 507px"><img src="http://yiwenandsoftware.files.wordpress.com/2009/03/picture-1.png?w=497&#038;h=366" alt="A failure message window" title="A failure message window" width="497" height="366" class="size-full wp-image-76" /><p class="wp-caption-text">A failure message window</p></div>
<p>Here is the part of the script modified from his original one:</p>
<pre class="brush: ruby;">
begin
  ::Spec::Runner::CommandLine.run(::Spec::Runner::OptionParser.parse(argv, STDERR, STDOUT))
rescue Exception =&gt; e
  File.open(&quot;#{report_file}&quot;, &quot;w&quot;) do |file|
    file.write(&quot;#{e.message} &lt;br /&gt;&quot;)
    file.write e.backtrace.join(&quot;&lt;br /&gt;&quot;)
  end
end
</pre>
<p>Another thing worth to mention is that handling vim type of links doesn&#8217;t work with his approach in either FF3 and FF2. I had to install a plugin called <a href="http://mozex.mozdev.org/installation.html">Mozex</a>. My experience is that it only works for FF2. Once you install it and restart your FF2, go to Add-Ons and click on &#8220;Preference&#8221; of this plugin. goto &#8220;Universal&#8221; tab. type &#8220;vim&#8221; into the first textbox and /path/to/open_vim to the second one just like the image shown below:<br />
<div id="attachment_84" class="wp-caption alignnone" style="width: 507px"><img src="http://yiwenandsoftware.files.wordpress.com/2009/03/picture-2.png?w=497&#038;h=195" alt="Mozex plugin setup" title="Mozex plugin setup" width="497" height="195" class="size-full wp-image-84" /><p class="wp-caption-text">Mozex plugin setup</p></div></p>
<p>Speaking of open_vim, since Mozex pass the whole URL (vim://Users/blah/blah.rb?line_number) back, so the regex for parsing it should be changed to:</p>
<pre>
 /vim:\/\/(.+)\?(\d+)/
</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/75/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=75&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2009/03/11/run-rspec-within-vim-and-seeing-result-in-your-firefox2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>

		<media:content url="http://yiwenandsoftware.files.wordpress.com/2009/03/picture-1.png" medium="image">
			<media:title type="html">A failure message window</media:title>
		</media:content>

		<media:content url="http://yiwenandsoftware.files.wordpress.com/2009/03/picture-2.png" medium="image">
			<media:title type="html">Mozex plugin setup</media:title>
		</media:content>
	</item>
		<item>
		<title>List Presenter</title>
		<link>http://yiwenandsoftware.wordpress.com/2008/10/28/list-presenter/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2008/10/28/list-presenter/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 04:07:42 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[DRY]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/?p=43</guid>
		<description><![CDATA[In a typical Rails app, we have a lot of &#8220;index&#8221; action in almost every controllers. And in most cases, an index is a list of same type of objects. There are a lot of similar features we need to present these lists. So it makes sense to have a concept as ListPresenter and each [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=43&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In a typical Rails app, we have a lot of &#8220;index&#8221; action in almost every controllers. And in most cases, an index is a list of same type of objects. There are a lot of similar features we need to present these lists. So it makes sense to have a concept as ListPresenter and each individual presenter who presents a list should extend from it.</p>
<p>Of course, in Ruby, we don&#8217;t have to actually have a class called ListPresenter and do all the stuff that are not necessarily related to each other. We can have as many modules as we need. Each module does one particular task. and all presenters for list can mixin those modules.</p>
<p>Why it is good to do things this way? Let&#8217;s take an example: If all lists are HTML tables, and all tables have table headers with title of columns. Now we decide to use title case (such as &#8216;Column Name&#8217;) for these headers, we could manually type in these texts into your HTML template. The approach is straightforward, but bearing three potential problems. </p>
<ol>
<li>Since developers generally do not know how to type, one could easily have typos
<li>Two developers may develop two lists and one may think, wrongly, that title should be humanized (such as &#8220;Column name&#8221;). Then we end up with inconsistent UI. This inconsistency could also happen when a developer produced lists before the team decided title case. So he used humanized words back then. He had to come back fix them one by one, even with the best IDE ever &#8211; Windows notepad&#8217;s help, it still a tedious and boring task.
<li>Later on, the business doesn&#8217;t like title case, they want humanized case. Well, 4 boring hours, again!
</ol>
<p>In this case, a simple method can help, all it takes is a module which provides a html_table_columns method, as the following:</p>
<pre class="brush: ruby;">
module HtmlTableColumns
  def html_table_columns(*args)
    self.send :define_method, :columns do
      returning([]) do |columns_hash|
        args.each do |arg|
          case arg
            when String, Symbol
              columns_hash &lt;&lt; arg.to_s.titlecase})
            when Hash
              columns_hash &lt;&lt; HashWithIndifferentAccess.new(arg)
            else
              raise ArgumentError.new(&quot;The params must be Hash, String or Symbols&quot;)
          end
        end
      end
    end
  end
end
</pre>
<p>This method defines a <i>columns</i> for class who calls this method. A call like:</p>
<pre class="brush: ruby;">
  table_columns('name', &quot;last_connected&quot;
  {'user_login' =&gt; &quot;User&quot;}, 'status')
</pre>
<p>will generate a method like this (in RSpec):</p>
<pre class="brush: ruby;">
      presenter.columns.should ==
      [{'name' =&gt; 'Name'},
       {&quot;last_connected&quot;=&gt;&quot;Last Connected&quot;},
        {'user_login' =&gt; &quot;User&quot;}, {'status' =&gt; &quot;Status&quot;}]
</pre>
<p>As long as every developer uses this method, there will be no typo, no inconsistency. And if people ever want to change from title case to humanized words, there is only one place that needs to be changed and I believe readers know where.</p>
<p>This is just an example how group similar features into one place is nice. In general I believe if I can find a feature that is across more than one places, it&#8217;s worth my effort to group them into one place, use whatever pattern that is applicable. So after all, I guess this post is not about List Presenter, it is about a case study of DRY.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/43/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=43&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2008/10/28/list-presenter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>Intergrate RSpec Story Runner into CruiseControl.rb build process with HTML report</title>
		<link>http://yiwenandsoftware.wordpress.com/2008/06/25/intergrate-rspec-story-runner-in-cruisecontrolrb-build-with-html-report/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2008/06/25/intergrate-rspec-story-runner-in-cruisecontrolrb-build-with-html-report/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 14:01:51 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[CruiseControl.rb]]></category>
		<category><![CDATA[RSpec Story]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/?p=40</guid>
		<description><![CDATA[Although still a work in progress, RSpec story runner is gaining popularity rapidly. I use it because I use Webrat for acceptance tests and Webrat integrates with the story runner seamlessly.
Then next thing I need is to create a task to integrate stories into CC.rb build process. And here it is:

desc &#34;Run all stories&#34;
task :all_stories [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=40&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Although still a work in progress, <a href="http://rspec.info/">RSpec story runner</a> is gaining popularity rapidly. I use it because I use <a href="http://github.com/brynary/webrat/tree/master">Webrat</a> for acceptance tests and Webrat integrates with the story runner seamlessly.</p>
<p>Then next thing I need is to create a task to integrate stories into CC.rb build process. And here it is:</p>
<pre class="brush: ruby;">
desc &quot;Run all stories&quot;
task :all_stories do
    if out = ENV['CC_BUILD_ARTIFACTS']
        spec_out = &quot;#{out}/Stories&quot;
        mkdir_p spec_out unless File.directory? spec_out
        report_file_name = &quot;#{spec_out}/index.html&quot;
    else
        report_file_name = 'stories.html'
    end
    if ! spec_out.blank?
        sh &quot;cp -r #{RAILS_ROOT}/vendor/plugins/rspec/story_server/prototype/javascripts/ #{spec_out}&quot;
        sh &quot;cp -r #{RAILS_ROOT}/vendor/plugins/rspec/story_server/prototype/stylesheets/ #{spec_out}&quot;
    end
    sh &quot;ruby #{RAILS_ROOT}/stories/all.rb -f html &gt; #{report_file_name}&quot;
end
</pre>
<p>Yes, ruby a_story.rb -f html will generate an html report instead of a plain text. That&#8217;s neat. But I also need to copy javascripts and stylesheets directories to <i>#{spec_out}</i> directory because the generated html report uses css and js files in these two directories. </p>
<p>Once you run this task as part of cruise task, you should see a Stories link under Custom Build Artifacts section. And once you click on it, you should see a page much like <i>#{RAILS_ROOT}/vendor/plugins/rspec/story_server/prototype/stories.html</i>.</p>
<p>The HTML report looks nice. But imagining when we have 100 stories all cozily live in this same single HTML file. It will take forever to load the page and it will be very difficult to find any useful information within it. My proposal is we actually generate a real index which contains just story names, and a color to indicate if they failed or not. Of course each story name is clickable which leads to another page for the detailed story report.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/40/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/40/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=40&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2008/06/25/intergrate-rspec-story-runner-in-cruisecontrolrb-build-with-html-report/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>My experience with Presenter Pattern</title>
		<link>http://yiwenandsoftware.wordpress.com/2008/04/06/my-experience-with-presenter-pattern/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2008/04/06/my-experience-with-presenter-pattern/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 05:02:59 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[presenter]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/?p=30</guid>
		<description><![CDATA[Default MVC layers handles one model, one form mode pretty well, but in reality, a lot of forms are not that simple. There are two classic examples: 

Multiple models are created/updated in one form
One model is created/updated in one form, but in several different ways

When these scenarios happen, MVC framework becomes awkward. We have to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=30&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Default MVC layers handles one model, one form mode pretty well, but in reality, a lot of forms are not that simple. There are two classic examples: </p>
<ol>
<li>Multiple models are created/updated in one form</li>
<li>One model is created/updated in one form, but in several different ways</li>
</ol>
<p>When these scenarios happen, MVC framework becomes awkward. We have to either leak business logic into controllers, even views, or put view logic into models, or both. In this post, I want to talk about how to use <a href="http://blog.jayfields.com/2007/03/rails-presenter-pattern.html">Presenter Pattern</a> to avoid putting view logic into the models. </p>
<p>Basically Presenter is one layer sitting between Controller and View layer, which should include all of your view logic. The first advantage we gain is that we can unit test view logic easily. Secondly, we can move all helper methods into presenters so that we can have a more elegant way to implement these methods. The third and the most important advantage we gain is that now the model is free from view logic, make its intention much more clear.</p>
<p>Now let&#8217;s see an example.</p>
<p>I have a model <i>House</i>, on creation form of House, there are some fields about this house. Some of them are required, some of them are optional. The form has two submit buttons, if a user click on button 1, she will become the owner of this house; if she clicks on button 2, she assigns another person to the owner of this house. When she assigns another person, she needs to pick a person before assigning, for the sake of simplicity, let&#8217;s assume she can pick a person from a drop-down list with all eligible person names.</p>
<p>Now if she clicks on button 1, she doesn&#8217;t need to choose any person from the drop-down list. But if she clicks on the button 2, she must select a person from that list, and if she doesn&#8217;t choose a person, she should get an error message to prompt her to do so. So we see a validation here, but this validation is totally view-specific, nothing to do with either <i>House</i> or <i>Person</i> models. In this situation, a presenter is a natural place to put this logic. </p>
<p>To implement this validation, one tool you absolutely need is the <a href="http://validatable.rubyforge.org/">Validatable</a> gem. Once a class includes a Validatable module, it has a lot of ActiveRecord-like validation logic such as <i>validates&#95;presence&#95;of</i>.</p>
<p>I will name my presenter after the form, in this case, <i>NewHousePresenter</i>, but if I know I will reuse this form in edit/update, I will simply name the presenter as <i>HousePresenter</i>.</p>
<p>Inside the presenter, I can have:</p>
<pre class="brush: ruby;">
	validates_presence_of <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> wner_id, :if =&gt; lambda {|presenter|presenter.assign_owner?}
</pre>
<p>How to implement <i>assign_owner?</i> is just a matter of implementation of your view, I like to just set a hidden field with value &#8220;assign&#95;owner&#8221; when clicking on the button 2 and &#8220;take&#95;owner&#8221; when clicking on the button 1. Then the <i>assign&#95;owner?</i> is just checking if the hidden field is string of &#8220;assign&#95;owner&#8221;.</p>
<p>Isn&#8217;t it cool?</p>
<p>In my previous post <a href="http://yiwenandsoftware.wordpress.com/2008/03/14/more-on-form_fornon_arb/">More on form&#95;for(@non_arb)</a>, I showed a trick to get polymorphic routes working so that the form can be<br />
<code><br />
	&lt;&#37; form(@presenter) &#37;&gt;<br />
</code></p>
<p>In my <i>HousePresenter</i>, I can just say:</p>
<pre class="brush: ruby;">
	polymorphic_routes_for :house, :create_path =&gt; &quot;house_owner_path&quot;, :update_path =&gt; &quot;house_owner_path&quot;
</pre>
<p>Not only is it for the sake of coolness, but also for the simplicity of the view and easiness to use the form in both create and update actions.</p>
<p>In my <i>Presenter</i> class, I also have an <i>initialize</i> declared as:</p>
<pre class="brush: ruby;">
def initialize(params={})
  params.each_pair do |key, value|
    instance_variable_set(&quot;@#{key}&quot;, value)
  end
end
</pre>
<p>All this does is just blindly set instance variables for all params passed in. The reason for doing this is that I can easily setup a presenter for unit testing. The subclass of <i>Presenter</i> should implement a <i>build</i> class method to take params from the controller, and parse it into a hash that is sensible to the presenter and initialize. So the &#8220;constructor&#8221; remains simple for convenient construction while <i>build</i> can do complex logic that can be unit tested.</p>
<p>A <i>build</i> method can look like:</p>
<pre class="brush: ruby;">
def self.build(house_params = {}, operation = &quot;take_owner&quot;)

	presenter_params = {:house =&gt; House.new(house_params), <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> peration =&gt; operation}
	HousePresenter.new presenter_params
end
</pre>
<p>After all this, my HousesController#new and create can be as simple as:</p>
<pre class="brush: ruby;">
def new
	@house_presenter = HousePresenter.build
end

def create
	@house_presenter = HousePresenter.build(params[:house], params[:operation])
	if @house_presenter.valid? &amp;&amp; @house_presenter.house.save
		redirect_to(@house_presenter.house)
	else
		render :new
	end
end
</pre>
<p>Don&#8217;t they look like controller actions again? You can infer the edit/update code from here fairly easy.</p>
<p>But for the perfectionist, this line:<br />
<code><br />
	if @house_presenter.valid? &amp;&amp; @house_presenter.house.save<br />
</code><br />
is a little bit smelly. Bigger thing is: if the creation/update require more then just a save, then we need something else. I almost want to be able to say:<br />
<code><br />
	if @house_presenter.save!<br />
	...<br />
</code></p>
<p>and in save! I do whatever I need. I actually did that and Marcel Molina looked ultimately confused and I agree with him: Presenters are not meant for business transactions. hmm. well, it&#8217;s a long enough post, I would like to talk about this issue in my next post, if it eventually comes out.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/30/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/30/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=30&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2008/04/06/my-experience-with-presenter-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>Riding Rails on IntelliJ</title>
		<link>http://yiwenandsoftware.wordpress.com/2008/01/01/riding-rails-on-intellij/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2008/01/01/riding-rails-on-intellij/#comments</comments>
		<pubDate>Tue, 01 Jan 2008 23:33:28 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[IDE]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/2008/01/01/riding-rails-on-intellij/</guid>
		<description><![CDATA[Textmate seems the &#8220;official&#8221; IDE for rails projects. But we have to develop under Windows, so no Textmate. 
We used eclipse with radrails (Aptana) for a while, believe me, it&#8217;s not fun. Then we start to try IntelliJ now. 
Ctrl+Shift+Alt+Space in IntelliJ gives you &#8220;word completion&#8221;, just like &#8220;Esc&#8221; in Textmate. And Ctrl+Space works for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=17&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://macromates.com/">Textmate</a> seems the &#8220;official&#8221; IDE for rails projects. But we have to develop under Windows, so no Textmate. </p>
<p>We used <a href="http://www.aptana.com/">eclipse with radrails (Aptana)</a> for a while, believe me, it&#8217;s not fun. Then we start to try <a href="http://www.jetbrains.com/idea/">IntelliJ</a> now. </p>
<p>Ctrl+Shift+Alt+Space in IntelliJ gives you &#8220;word completion&#8221;, just like &#8220;Esc&#8221; in Textmate. And Ctrl+Space works for local variables nicely. And it even works for instance variables, but it needs long time to think. Ctrl+Space works partially in Eclipse, but not in a RSpec file where no explicit class declarations.</p>
<p>Global search on Textmate is a pain, especially if you forget to delete huge development.log and test.log. So I just grep extensively when on Macs. In IntelliJ, you can specify which directory to search with, this helps a lot. Even better, when you have to search the whole project, it will ask you if you want to skip development.log/test.log, very intelligent.</p>
<p>The amazing thing is that renaming classes name and methods name work perfectly in IntelliJ. This feature alone will save me countless time and energy.</p>
<p>Subversion support in Texmate is not so nice, I would just use terminal for subversion operations. In both Eclipse and IntelliJ, the supports are just awesome. </p>
<p>When you have conflicting files, the diff support in Textmate is primitive. And there is no good files/directories comparison tool under MacOS. Meld diff viewer is awesome, but it may take some effort to install it on Macs.</p>
<p>The biggest problem with IntelliJ is that it&#8217;s not very responsive, compared to Textmate. But it&#8217;s definitely faster than Eclipse.</p>
<p>We use <a href="http://wtr.rubyforge.org/">Watir</a> as our scenario test tool, so moving away from Windows doesn&#8217;t seem quite possible. This being said, it seems IntelliJ is our best bet.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=17&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2008/01/01/riding-rails-on-intellij/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>A Study in Array#Collect</title>
		<link>http://yiwenandsoftware.wordpress.com/2007/12/04/collect-syntax-study/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2007/12/04/collect-syntax-study/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 16:10:47 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/2007/12/04/collect-syntax-study/</guid>
		<description><![CDATA[
Stephen Chu talked about how to beautify statements like
customers.collect { &#124;customer&#124; customer.name } in one of his posts. Dr. Nic also has a post talking about the same thing. It&#8217;s very interesting to see so many people want to get away the cumbersome collect syntax and I am one of these people.


To beautify the collect [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=15&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>
<a href="http://www.stephenchu.com/">Stephen Chu</a> talked about how to beautify statements like<br />
<code>customers.collect { |customer| customer.name }</code> in <a href="http://www.stephenchu.com/2007/03/enhance-arraycollect-to-become-magical.html">one of his posts</a>. <a href="http://drnicwilliams.com/">Dr. Nic</a> also has a <a href="http://drnicwilliams.com/2006/09/28/new-magical-version-of-symbolto_proc/">post</a> talking about the same thing. It&#8217;s very interesting to see so many people want to get away the cumbersome collect syntax and I am one of these people.
</p>
<p>
To beautify the collect syntax when we need just one field such as <code>customer.name</code>. There are several ways:<br />
to use <a href="http://www.stephenchu.com/2007/03/enhance-arraycollect-to-become-magical.html">Stephen&#8217;s</a> syntax:<br />
<code>customers.collect_name</code></p>
<p><a href="http://drnicwilliams.com/2006/09/28/new-magical-version-of-symbolto_proc/">Dr. Nic&#8217;s</a>:<br />
<code>customers.names</code>
</p>
<p>
And guess what, if you are using Rails, it comes with a Symbol extension method called <code>to_proc</code>. What the method does is to turn a symbol into a simple Proc object. so we can say:</p>
<p><code>customers.collect &amp;:name</code> which does the job.
</p>
<p>
In practice, I always just use this built-in to_proc because it&#8217;s free as long as you are on a Rails project. And there is no reason for me to justify I should spend time to make it even more magical.
</p>
<p>
<a href="http://www.stephenchu.com/">Stephen</a> also came up with syntax like:<br />
<code>customers.collect_name_and_id</code>
</p>
<p>
The method always returns an array of arrays (<code>[['Yi', 1], ['Du', 2]]</code>). The syntax looks very nice, but I think it has limited value because I may not want an array of arrays, I may want an array of hashes (<code>[{1 =&gt; 'Yi'}, {2 =&gt; 'Du']</code>), or an array of strings, then the method has no use or we have to do more magic. Most importantly, when we want multiple fields from a model object to form some kind of data structure, we probably should ask ourselves first: what&#8217;s the business meaning for this action? If we can find one, we can just add one method, returning whatever we want, to the model class we are dealing with and use that method for collect.
</p>
<p>
For example, if I want to send an email to several people and their email address should look like: <br />
<code>people.collect{|person| "person.name &lt;person.email&gt;"}</code>, then I probably will just add a method to the Person class, such as <code>Person#full_email_address</code> and return the string I need. the the syntax for collect can be simplified to <code>people.collect &amp;:full_email_address</code>.
</p>
<p>
It&#8217;s cool to see how powerful <code>method_missing</code> can be with <a href="http://www.stephenchu.com/2007/03/enhance-arraycollect-to-become-magical.html">Stephen&#8217;s</a> extension, and how lovely Ruby is.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=15&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2007/12/04/collect-syntax-study/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>When RSpec meets Validatable</title>
		<link>http://yiwenandsoftware.wordpress.com/2007/11/12/when-rspec-meets-validatable/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2007/11/12/when-rspec-meets-validatable/#comments</comments>
		<pubDate>Mon, 12 Nov 2007 16:00:30 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Validatable]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/2007/11/12/when-rspec-meets-validatable/</guid>
		<description><![CDATA[Jay Fields writes an excellent module called Validatable to provide a class who include the module the ActiveRecord like behavior. For example, you can say validates_presence_of :attribute etc. This page has the RDoc of the module.When I write RSpec for a Validatable class, I ended up writing a stattement like this:

  @email.validate_only("presence_of/body")
  @email.errors.on(:body).should_not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=9&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://blog.jayfields.com/">Jay Fields</a> writes an excellent module called <a href="http://blog.jayfields.com/2007/11/validatable-166-released.html">Validatable</a> to provide a class who include the module the ActiveRecord like behavior. For example, you can say <em>validates_presence_of :attribute</em> etc. <a href="http://validatable.rubyforge.org/">This page</a> has the RDoc of the module.When I write RSpec for a Validatable class, I ended up writing a stattement like this:</p>
<pre>
  @email.validate_only("presence_of/body")
  @email.errors.on(:body).should_not be_nil
</pre>
<p>Which is basically testing <em>body</em> is a required field. It works, but it looks complicated and hard to understand.The RSpec on Rails plugin provides a method <em>error_on</em> so that I can say something like this:
<pre> model.should have(2).errors_on(:attribute)</pre>
<p>This is much more readable than the previous one.I came up with a small extension on Validatable so that I can have a better syntax for testing Validatable.Here&#8217;s the spec:</p>
<pre class="brush: ruby;">
require File.dirname(__FILE__) + '/../../../spec_helper'

module RspecValidatableSpec
  class Thing
    include Validatable
    attr_accessor :age
    attr_accessor :name
    validates_presence_of :age
    validates_numericality_of :age
  end
end

describe RSpecValidatable, 'a validatable object' do
  before :each do
    @thing = RspecValidatableSpec::Thing.new
  end

  it &quot;should tell you it has at least one error on a field&quot; do
    @thing.should have_at_least(1).error_on(:age)
  end

  it &quot;should tell you it has one error on a required field&quot; do
    @thing.should have(1).error_on_presence_of(:age)
  end

  it &quot;should tell you it has one error on a numerical field&quot; do
    @thing.should have(1).error_on_numericality_of(:age)
  end
end

describe RSpecValidatable, 'a validatable object' do
  before :each do
    @thing = RspecValidatableSpec::Thing.new
  end

  it &quot;should tell you it has no error on a field&quot; do
    @thing.should have(:no).errors_on(:name)
  end
end
</pre>
<p>You may need a different path to your spec_helper.rb to make it work.I got the &#8220;traditional&#8221; <em>errors_on</em> just like how RSpec works on ActiveRecords. What&#8217;s more, Validatable has a <em>validates_only</em> method which allows us validating only one type of error on one attribute. so I can have even better control than RSpec on Rails giving me over ActiveRecords. For example, I can specify what type of errors I expect to have.The only problem is that <em>validates_only</em> raises an error if you check an attribute on a validation that is not specified. So a nice spec like</span> email.should have(:no).errors_on_presence_of(:attachment)</span>is not quite possible. For those of you who are interested in this, here is the actually code:</p>
<pre class="brush: ruby;">
module RSpecValidatable
  def errors_on(attribute)
    self.valid?
    [self.errors.on(attribute)].flatten.compact
  end

  def respond_to? method_name
    super(method_name) || method_name.to_s.index('error_on') == 0
  end

  def method_missing(error_on_method, attribute)
    reason = error_on_method.to_s.sub(/error_on_/, '')
    self.validate_only(&quot;#{reason.to_s}/#{attribute.to_s}&quot;)
    [self.errors.on(attribute)].flatten.compact
  end

  alias :error_on :errors_on

end

Validatable.send :include, RSpecValidatable
</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=9&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2007/11/12/when-rspec-meets-validatable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>Law of Demeter and Forwardable</title>
		<link>http://yiwenandsoftware.wordpress.com/2007/11/02/law-of-demeter-and-forwardable/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2007/11/02/law-of-demeter-and-forwardable/#comments</comments>
		<pubDate>Fri, 02 Nov 2007 18:48:00 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[fowardable]]></category>
		<category><![CDATA[law of demeter]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[OO programming]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/2007/11/02/law-of-demeter-and-forwardable/</guid>
		<description><![CDATA[Dan Manges has an excellent post explaining Law of Demeter and when it makes sense to use Forwardable to comply the principle, or I should say when it doesn&#8217;t make sense to use the module.
The point is, use it when it makes sense. Take his example, customer_name is just not a sensible method for an [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=8&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Dan Manges has an excellent <a href="http://www.dcmanges.com/blog/37">post</a> explaining Law of Demeter and when it makes sense to use Forwardable to comply the principle, or I should say when it doesn&#8217;t make sense to use the module.</p>
<p>The point is, use it when it makes sense. Take his example, customer_name is just not a sensible method for an order. Asking an order for its customer name is a wrong question because all the order should care is who is its customer. The name is irrelevant to the order.</p>
<p>In fact, I think by putting <i>cusomer_name</i> method in an order. the encapsulation is actually broken because now an order knows the inside details of a customer, thus tightly coupled these two classes.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=8&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2007/11/02/law-of-demeter-and-forwardable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple Captcha and Validatable</title>
		<link>http://yiwenandsoftware.wordpress.com/2007/09/10/simple-captcha-and-validatable/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2007/09/10/simple-captcha-and-validatable/#comments</comments>
		<pubDate>Mon, 10 Sep 2007 16:03:00 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[simple captcha]]></category>
		<category><![CDATA[Validatable]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/2007/09/10/simple-captcha-and-validatable/</guid>
		<description><![CDATA[I need a captcha in a form, whose associated model is a Validatable.   After googling, I decided to go with Simple Captcha. The Simple captcha is extremely easy to use, see its documentation, you&#8217;ll know. But it works fine with a ActiveRecord::Base, not a Validatable. There has to be some hack to make [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=6&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I need a captcha in a form, whose associated model is a <a href="http://blog.jayfields.com/2007/04/ruby-validatable-122-released.html">Validatable.</a>   After googling, I decided to go with <a href="http://expressica.com/2007/03/23/simple_captcha_1_0/">Simple Captcha</a>. The Simple captcha is extremely easy to use, see its documentation, you&#8217;ll know. But it works fine with a ActiveRecord::Base, not a Validatable. There has to be some hack to make it work. I could use &#8220;Controller mode&#8221; of it. But it will be nice to put it into model, make it part of validation process. and here&#8217;s my code:</p>
<p>First, include SimpleCaptcha into your Validatable model:</p>
<pre class="brush: ruby;">
YourValidatable.module_eval do
    class &lt;&lt; self; include SimpleCaptcha::ModelHelpers; end
end
</pre>
<p>And overwrite <i>apply_simple_captcha</i></p>
<pre class="brush: ruby;">
  def self.apply_simple_captcha(options = {})
    module_eval do
      require 'pstore'
      include SimpleCaptcha::ConfigTasks
      attr_accessor :captcha, :captcha_code,
      :authenticate_with_captcha
      alias_method :valid_without_captcha?, :valid?
    end
    @captcha_invalid_message =
 (options[:message].nil? || options[:message].empty?) ?
  &quot; image did not match with text&quot; : options[:message]
    module_eval(turing_valid_method)
    module_eval(turing_save_method)
  end
</pre>
<p>Finally, call <code>apply_simple_captcha</code></p>
<p>That&#8217;s it.</p>
<p>In the controller, instead of calling <code>valid?</code> to decide if the object is valid, you call <code>valid_with_captcha?</code></p>
<p>BTW: <a href="http://blog.inlet-media.de/rmagick-does-not-play-well-on-ubuntu">Follow this link</a> to see how to install RMagick on Ubuntu</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=6&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2007/09/10/simple-captcha-and-validatable/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>Checking if a method called in transaction</title>
		<link>http://yiwenandsoftware.wordpress.com/2007/04/27/checking-if-a-method-called-in-transaction/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2007/04/27/checking-if-a-method-called-in-transaction/#comments</comments>
		<pubDate>Fri, 27 Apr 2007 22:15:00 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[transaction]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/2007/04/27/checking-if-a-method-called-in-transaction/</guid>
		<description><![CDATA[Sometimes it&#8217;s comforting to know your method is called within a transaction. 
if Thread.current['open_transactions'].nil? &#124;&#124;    Thread.current['open_transactions'] == 0
will do. Any better idea?
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=5&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometimes it&#8217;s comforting to know your method is called within a transaction. </p>
<pre>if Thread.current['open_transactions'].nil? ||    Thread.current['open_transactions'] == 0</pre>
<p>will do. Any better idea?</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=5&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2007/04/27/checking-if-a-method-called-in-transaction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
	</channel>
</rss>