<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>DevInterface Blog</title>
	
	<link>http://blog.devinterface.com</link>
	<description>Indipendent Software &amp; Web Developers Team</description>
	<lastBuildDate>Mon, 06 Sep 2010 10:30:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/devinterfaceblog" /><feedburner:info uri="devinterfaceblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>devinterfaceblog</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Rails Best Practices 6: Filters and helpers file</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/WvPQswkRrZI/</link>
		<comments>http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 10:30:46 +0000</pubDate>
		<dc:creator>Claudio</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Metodologies]]></category>
		<category><![CDATA[after_filter]]></category>
		<category><![CDATA[before_filter]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=869</guid>
		<description><![CDATA[With the today&#8217;s post I shall conclude my series on Ruby On Rails Best Practices . This is not because the required topics are completed, but rather because after the release of  Rails 3.0, some constructs have been changed and should be reviewed.
Probably in future we will return to talk about best practices, but [...]]]></description>
			<content:encoded><![CDATA[<p>With the today&#8217;s post I shall conclude my series on Ruby On Rails Best Practices . This is not because the required topics are completed, but rather because after the release of  Rails 3.0, some constructs have been changed and should be reviewed.<br />
Probably in future we will return to talk about best practices, but starting from the new features introduced in the current Rails version.<br />
But now I will describe two techniques that are valid and which are mainly related to code organization.</p>
<p><strong> 1. Use filters </strong><br />
In order to remove duplicate code within the controller is good practice to use the filters to perform tasks that are common to most if not to all methods.<br />
A classic case in which you  use filters is user authentication.<br />
Another example may be a function that makes the log of user activity.<br />
I show here a very simple example for demonstration purposes only</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span><br />
<br />
&nbsp;<span style="color:#9966CC; font-weight:bold;">def</span> add_log<br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;">#create new log</span><br />
&nbsp; &nbsp; log = ActivityLog.<span style="color:#9900CC;">new</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># read data from request</span><br />
&nbsp; &nbsp; log.<span style="color:#9900CC;">session_id</span> = request.<span style="color:#9900CC;">session_options</span><span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span> <br />
&nbsp; &nbsp; log.<span style="color:#9900CC;">user_id</span> = current_user.<span style="color:#9900CC;">id</span><br />
&nbsp; &nbsp; log.<span style="color:#9900CC;">browser</span> = request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'HTTP_USER_AGENT'</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; log.<span style="color:#9900CC;">ip_address</span> = request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'REMOTE_ADDR'</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; log.<span style="color:#9900CC;">controller</span> = controller_name<br />
&nbsp; &nbsp; log.<span style="color:#9900CC;">action</span> = action_name<br />
&nbsp; &nbsp; log.<span style="color:#9900CC;">request_at</span> = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Save the log</span><br />
&nbsp; &nbsp; log.<span style="color:#9900CC;">save</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#008000; font-style:italic;">#other methods here</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>We have defined our function add_log that saves data request in a log on db.<br />
Now in our controller we will do something like this:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> PostController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> index<br />
&nbsp; &nbsp; add_log<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@posts</span> = Post.<span style="color:#9900CC;">all</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> new<br />
&nbsp; &nbsp; add_log<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">new</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> show<br />
&nbsp; &nbsp; add_log<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> edit<br />
&nbsp; &nbsp; add_log<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<br />
&nbsp; &nbsp; add_log<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:post</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> update<br />
&nbsp; &nbsp; add_log<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">update</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:post</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> destroy<br />
&nbsp; &nbsp; add_log<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">destroy</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>The PostController can be optimized using a before_filter in the following way</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> PostController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
<br />
&nbsp; before_filter <span style="color:#ff3333; font-weight:bold;">:add_log</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> index<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@posts</span> = Post.<span style="color:#9900CC;">all</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> new<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">new</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> show<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> edit<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:post</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> update<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">update</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:post</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> destroy<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">destroy</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Then if we want the calls to some methods (eg show and index) can not be traced we can simply define the filter as follows:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> PostController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
<br />
&nbsp; before_filter <span style="color:#ff3333; font-weight:bold;">:add_log</span>, <span style="color:#ff3333; font-weight:bold;">:except</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#ff3333; font-weight:bold;">:index</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
<br />
<span style="color:#008000; font-style:italic;"># methods</span></div></td></tr></tbody></table></div>
<p>Obviously the use of filters should be done in a careful way because excessive use of before_filter or after_filter for operations not so general can make the code less immediate to understand.</p>
<p><strong> 2. Organize helpers by functionality </strong><br />
The second technique that I introduce today is a better system (in my opinion) to organize helper files.</p>
<p>Rails typically generates an helper file for each controller. So it&#8217;s easy that in a short time you have a large number of helpers and often you use only a few of them.<br />
For example:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; app<span style="color:#006600; font-weight:bold;">/</span>helpers<span style="color:#006600; font-weight:bold;">/</span>application_helper.<span style="color:#9900CC;">rb</span><br />
&nbsp; app<span style="color:#006600; font-weight:bold;">/</span>helpers<span style="color:#006600; font-weight:bold;">/</span>comments_helper.<span style="color:#9900CC;">rb</span><br />
&nbsp; app<span style="color:#006600; font-weight:bold;">/</span>helpers<span style="color:#006600; font-weight:bold;">/</span>posts_helper.<span style="color:#9900CC;">rb</span><br />
&nbsp; app<span style="color:#006600; font-weight:bold;">/</span>helpers<span style="color:#006600; font-weight:bold;">/</span>users_helper.<span style="color:#9900CC;">rb</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># ...</span></div></td></tr></tbody></table></div>
<p>And in the ApplicationController:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span><br />
&nbsp; &nbsp; helper <span style="color:#ff3333; font-weight:bold;">:all</span> <span style="color:#008000; font-style:italic;"># include all helpers, all the time</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>A system I find very convenient is rather to organize helpers for functionality, removing all unnecessary and empty files.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; app<span style="color:#006600; font-weight:bold;">/</span>helpers<span style="color:#006600; font-weight:bold;">/</span>application_helper.<span style="color:#9900CC;">rb</span> <br />
&nbsp; app<span style="color:#006600; font-weight:bold;">/</span>helpers<span style="color:#006600; font-weight:bold;">/</span>buttons_helper.<span style="color:#9900CC;">rb</span><br />
&nbsp; app<span style="color:#006600; font-weight:bold;">/</span>helpers<span style="color:#006600; font-weight:bold;">/</span>treeviews_helper.<span style="color:#9900CC;">rb</span></div></td></tr></tbody></table></div>
<p>In the ApplicationController you not need to change anything. As before it loads all the helpers in the helpers folder. The advantage of this solution is to have fewer helper files and know immediately where to enter or search for a method when you have to add or change it.<!-- Social Bookmarking Reloaded BEGIN -->
<div class="social_bookmark"><em>Share on:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/&amp;title=Rails+Best+Practices+6%3A+Filters+and+helpers+file" title="Add 'Rails Best Practices 6: Filters and helpers file' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to Del.icio.us" alt="Add 'Rails Best Practices 6: Filters and helpers file' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/&amp;title=Rails+Best+Practices+6%3A+Filters+and+helpers+file" title="Add 'Rails Best Practices 6: Filters and helpers file' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to digg" alt="Add 'Rails Best Practices 6: Filters and helpers file' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/&amp;title=Rails+Best+Practices+6%3A+Filters+and+helpers+file" title="Add 'Rails Best Practices 6: Filters and helpers file' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to reddit" alt="Add 'Rails Best Practices 6: Filters and helpers file' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="Add 'Rails Best Practices 6: Filters and helpers file' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to Technorati" alt="Add 'Rails Best Practices 6: Filters and helpers file' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/&amp;title=Rails+Best+Practices+6%3A+Filters+and+helpers+file" title="Add 'Rails Best Practices 6: Filters and helpers file' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to Stumble Upon" alt="Add 'Rails Best Practices 6: Filters and helpers file' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/&amp;title=Rails+Best+Practices+6%3A+Filters+and+helpers+file" title="Add 'Rails Best Practices 6: Filters and helpers file' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to Google Bookmarks" alt="Add 'Rails Best Practices 6: Filters and helpers file' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="Add 'Rails Best Practices 6: Filters and helpers file' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to Rojo" alt="Add 'Rails Best Practices 6: Filters and helpers file' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/&amp;title=Rails+Best+Practices+6%3A+Filters+and+helpers+file" title="Add 'Rails Best Practices 6: Filters and helpers file' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'Rails Best Practices 6: Filters and helpers file' to OKnotizie" alt="Add 'Rails Best Practices 6: Filters and helpers file' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=Rails+Best+Practices+6%3A+Filters+and+helpers+file&amp;url=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="Add 'Rails Best Practices 6: Filters and helpers file' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to Diggita" alt="Add 'Rails Best Practices 6: Filters and helpers file' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/&amp;t=Rails+Best+Practices+6%3A+Filters+and+helpers+file" title="Add 'Rails Best Practices 6: Filters and helpers file' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to FaceBook" alt="Add 'Rails Best Practices 6: Filters and helpers file' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/&amp;title=Rails+Best+Practices+6%3A+Filters+and+helpers+file" title="Add 'Rails Best Practices 6: Filters and helpers file' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to Upnews" alt="Add 'Rails Best Practices 6: Filters and helpers file' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="Add 'Rails Best Practices 6: Filters and helpers file' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to Twitter" alt="Add 'Rails Best Practices 6: Filters and helpers file' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&amp;title=Rails+Best+Practices+6%3A+Filters+and+helpers+file&amp;url=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="Add 'Rails Best Practices 6: Filters and helpers file' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to Technotizie" alt="Add 'Rails Best Practices 6: Filters and helpers file' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=Rails+Best+Practices+6%3A+Filters+and+helpers+file&amp;url=http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="Add 'Rails Best Practices 6: Filters and helpers file' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'Rails Best Practices 6: Filters and helpers file' to FriendFeed" alt="Add 'Rails Best Practices 6: Filters and helpers file' to FriendFeed" /></a></div>
<p><!-- Social Bookmarking Reloaded END --></p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-introduction/" title="<!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:-->"><!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/" title="<!--:en-->Rails Best Practices 5: Optimize Migration<!--:--><!--:it-->Rails Best Practices 5: Ottimizzare le Migration<!--:-->"><!--:en-->Rails Best Practices 5: Optimize Migration<!--:--><!--:it-->Rails Best Practices 5: Ottimizzare le Migration<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="<!--:en-->Rails Best Practices 4: Put method in the right model<!--:--><!--:it-->Rails Best Practices 4: Scrivere i metodi nel model corretto<!--:-->"><!--:en-->Rails Best Practices 4: Put method in the right model<!--:--><!--:it-->Rails Best Practices 4: Scrivere i metodi nel model corretto<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="<!--:en-->Rails Best Practices 3: Increase Controller&#8217;s readability<!--:--><!--:it-->Rails Best Practices 3: Aumentare la leggibilità dei controller<!--:-->"><!--:en-->Rails Best Practices 3: Increase Controller&#8217;s readability<!--:--><!--:it-->Rails Best Practices 3: Aumentare la leggibilità dei controller<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/" title="<!--:en-->Rails Best Practices 2: Move logic to model<!--:--><!--:it-->Rails Best Practices 2: Spostare la logica nel model<!--:-->"><!--:en-->Rails Best Practices 2: Move logic to model<!--:--><!--:it-->Rails Best Practices 2: Spostare la logica nel model<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/Rh6kWeBmWj2n06gFW-ldfYLcT3g/0/da"><img src="http://feedads.g.doubleclick.net/~a/Rh6kWeBmWj2n06gFW-ldfYLcT3g/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Rh6kWeBmWj2n06gFW-ldfYLcT3g/1/da"><img src="http://feedads.g.doubleclick.net/~a/Rh6kWeBmWj2n06gFW-ldfYLcT3g/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=WvPQswkRrZI:RlDnFaOnAzg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=WvPQswkRrZI:RlDnFaOnAzg:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=WvPQswkRrZI:RlDnFaOnAzg:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=WvPQswkRrZI:RlDnFaOnAzg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=WvPQswkRrZI:RlDnFaOnAzg:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/WvPQswkRrZI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/</feedburner:origLink></item>
		<item>
		<title>How to create multiple Django environments using virtualenv</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/sfQkEauF4Bk/</link>
		<comments>http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 06:10:05 +0000</pubDate>
		<dc:creator>Stefano</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[virtualenv]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=851</guid>
		<description><![CDATA[	Often we must work on various Django applications, each one dependent on different set of  libraries.

	For examples, we are working on a Django 1.1 project and and we need to start a new one with the latest version of the framework.

	The best solution is to keep the various environments separate, in order to ensure [...]]]></description>
			<content:encoded><![CDATA[	<p>Often we must work on various <a href="http://www.djangoproject.com/" title="">Django</a> applications, each one dependent on different set of  libraries.</p>

	<p>For examples, we are working on a Django 1.1 project and and we need to start a new one with the latest version of the framework.</p>

	<p>The best solution is to keep the various environments separate, in order to ensure that each project accesses only the libraries which it depends.</p>

	<p>Python offers us <a href="http://pypi.python.org/pypi/virtualenv" title="">virtualenv</a>, a tool to create multiple isolated environments. Each environment is completely independent from the others and from the installed packages in system&#8217;s site-packages folder.</p>

	<p>Let&#8217;s then see how to create a virtualenv and install the latest version of Django (today 1.2.1).</p>

	<h1>1- Install python-setuptools</h1>

	<p>The first thing to do is to install python-setuptools in order to have access to the command <a href="http://pypi.python.org/pypi/setuptools" title="">easy_install</a> and to easly download packages. Alternatively, you can get the same results using the package <a href="http://pypi.python.org/pypi/pip" title="">pip</a>.</p>

	<p>Let us open a console and type the following commands:</p>

	<p><div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> python-setuptools<br />
Reading package lists... Done<br />
Building dependency <span style="color: #c20cb9; font-weight: bold;">tree</span> &nbsp; &nbsp; &nbsp; <br />
Reading state information... Done<br />
python-setuptools is already the newest version.<br />
<span style="color: #000000;">0</span> upgraded, <span style="color: #000000;">0</span> newly installed, <span style="color: #000000;">0</span> to remove and <span style="color: #000000;">132</span> not upgraded.<br />
stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~$</div></td></tr></tbody></table></div></p>

	<h1>2- Install virtualenv</h1>

	<p>The second step is to install the package virtualenv using easy_install:</p>

	<p><div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> easy_install virtualenv<br />
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">sudo</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> password <span style="color: #000000; font-weight: bold;">for</span> stefano: <br />
Searching <span style="color: #000000; font-weight: bold;">for</span> virtualenv<br />
Reading http:<span style="color: #000000; font-weight: bold;">//</span>pypi.python.org<span style="color: #000000; font-weight: bold;">/</span>simple<span style="color: #000000; font-weight: bold;">/</span>virtualenv<span style="color: #000000; font-weight: bold;">/</span><br />
Reading http:<span style="color: #000000; font-weight: bold;">//</span>virtualenv.openplans.org<br />
Best match: virtualenv 1.4.9<br />
Downloading http:<span style="color: #000000; font-weight: bold;">//</span>pypi.python.org<span style="color: #000000; font-weight: bold;">/</span>packages<span style="color: #000000; font-weight: bold;">/</span>source<span style="color: #000000; font-weight: bold;">/</span>v<span style="color: #000000; font-weight: bold;">/</span>virtualenv<span style="color: #000000; font-weight: bold;">/</span>virtualenv-1.4.9.tar.gz<span style="color: #666666; font-style: italic;">#md5=c49067cab242b5ff8c7b681a5a99533a</span><br />
Processing virtualenv-1.4.9.tar.gz<br />
Running virtualenv-1.4.9<span style="color: #000000; font-weight: bold;">/</span>setup.py <span style="color: #660033;">-q</span> bdist_egg <span style="color: #660033;">--dist-dir</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>easy_install-3D0IWT<span style="color: #000000; font-weight: bold;">/</span>virtualenv-1.4.9<span style="color: #000000; font-weight: bold;">/</span>egg-dist-tmp-xe4LRm<br />
warning: no previously-included files matching <span style="color: #ff0000;">'*.*'</span> found under directory <span style="color: #ff0000;">'docs/_templates'</span><br />
Adding virtualenv 1.4.9 to easy-install.pth <span style="color: #c20cb9; font-weight: bold;">file</span><br />
Installing virtualenv script to <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<br />
<br />
Installed <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>python2.6<span style="color: #000000; font-weight: bold;">/</span>dist-packages<span style="color: #000000; font-weight: bold;">/</span>virtualenv-1.4.9-py2.6.egg<br />
Processing dependencies <span style="color: #000000; font-weight: bold;">for</span> virtualenv<br />
Finished processing dependencies <span style="color: #000000; font-weight: bold;">for</span> virtualenv</div></td></tr></tbody></table></div></p>

	<h1>3- Create the virtual environment</h1>

	<p>Once installed the package virtualenv we will need to create a space to host multiple environments. In my case, I&#8217;ve chose to create a directory in my home rather than in another writable folder in the filesystem.</p>

	<p><div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~$ <span style="color: #7a0874; font-weight: bold;">cd</span> Progetti<span style="color: #000000; font-weight: bold;">/</span>Python<span style="color: #000000; font-weight: bold;">/</span><br />
stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python$<br />
stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> virtualenvs<br />
stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python$ <span style="color: #7a0874; font-weight: bold;">cd</span> virtualenvs<span style="color: #000000; font-weight: bold;">/</span><br />
stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python<span style="color: #000000; font-weight: bold;">/</span>virtualenvs$</div></td></tr></tbody></table></div></p>

	<p>Now we create the real virtualenv, called &#8220;Django-1.2-env. Note the argument added to the command virtualenv, <em>&#8212;no-site-packages</em>: so our virtual environment will have only a minimal set of libraries in his site-packages folder. We also note that all next commands will not need access as super user via sudo.</p>

	<p><div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python<span style="color: #000000; font-weight: bold;">/</span>virtualenvs$ virtualenv <span style="color: #660033;">--no-site-packages</span> django-<span style="color: #000000;">1.2</span>-env<br />
New python executable <span style="color: #000000; font-weight: bold;">in</span> django-<span style="color: #000000;">1.2</span>-env<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python<br />
Installing setuptools............done.</div></td></tr></tbody></table></div></p>

	<h1>4- Download Django in /tmp</h1>

	<p>Open a new console and download the tarball with the latest version of the framework Django. For simplicity, I&#8217;ve downloaded the package to /tmp.</p>

	<p><div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span><br />
stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:<span style="color: #000000; font-weight: bold;">/</span>tmp$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.djangoproject.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>1.2.1<span style="color: #000000; font-weight: bold;">/</span>tarball<span style="color: #000000; font-weight: bold;">/</span><br />
<span style="color: #660033;">--2010-08-28</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">20</span>:<span style="color: #000000;">58</span>-- &nbsp;http:<span style="color: #000000; font-weight: bold;">//</span>www.djangoproject.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>1.2.1<span style="color: #000000; font-weight: bold;">/</span>tarball<span style="color: #000000; font-weight: bold;">/</span><br />
Resolving www.djangoproject.com... 64.207.133.18<br />
Connecting to www.djangoproject.com<span style="color: #000000; font-weight: bold;">|</span>64.207.133.18<span style="color: #000000; font-weight: bold;">|</span>:<span style="color: #000000;">80</span>... connected.<br />
HTTP request sent, awaiting response... <span style="color: #000000;">301</span> Moved Permanently<br />
Location: http:<span style="color: #000000; font-weight: bold;">//</span>media.djangoproject.com<span style="color: #000000; font-weight: bold;">/</span>releases<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.2</span><span style="color: #000000; font-weight: bold;">/</span>Django-1.2.1.tar.gz <span style="color: #7a0874; font-weight: bold;">&#91;</span>following<span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
<span style="color: #660033;">--2010-08-28</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">20</span>:<span style="color: #000000;">59</span>-- &nbsp;http:<span style="color: #000000; font-weight: bold;">//</span>media.djangoproject.com<span style="color: #000000; font-weight: bold;">/</span>releases<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.2</span><span style="color: #000000; font-weight: bold;">/</span>Django-1.2.1.tar.gz<br />
Resolving media.djangoproject.com... 64.207.133.30<br />
Connecting to media.djangoproject.com<span style="color: #000000; font-weight: bold;">|</span>64.207.133.30<span style="color: #000000; font-weight: bold;">|</span>:<span style="color: #000000;">80</span>... connected.<br />
HTTP request sent, awaiting response... <span style="color: #000000;">200</span> OK<br />
Length: <span style="color: #000000;">6248006</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>6.0M<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>application<span style="color: #000000; font-weight: bold;">/</span>octet-stream<span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
Saving to: <span style="color: #000000; font-weight: bold;">`</span>Django-1.2.1.tar.gz<span style="color: #ff0000;">'<br />
<br />
100%[======================================&gt;] 6,248,006 &nbsp; &nbsp;590K/s &nbsp; in 12s &nbsp; &nbsp; <br />
<br />
2010-08-28 11:21:11 (523 KB/s) - `Django-1.2.1.tar.gz'</span> saved <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">6248006</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">6248006</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></div></td></tr></tbody></table></div></p>

	<h1>5- Installing Django in virtualenv</h1>

	<p>At this point, unpack Django and activate the virtualenv (via the command &#8220;source  virtualenvdir/bin/activate&#8221;). Once activated virtualenv, we&#8217;ll find his name in the shell: (django-1.2-env)stefano@stefano-laptop</p>

	<p><div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:<span style="color: #000000; font-weight: bold;">/</span>tmp$ <span style="color: #c20cb9; font-weight: bold;">tar</span> xzfv Django-1.2.1.tar.gz<br />
stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>Django-1.2.1$ <span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>stefano<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python<span style="color: #000000; font-weight: bold;">/</span>virtualenvs<span style="color: #000000; font-weight: bold;">/</span>django-<span style="color: #000000;">1.2</span>-env<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>activate<br />
<span style="color: #7a0874; font-weight: bold;">&#40;</span>django-<span style="color: #000000;">1.2</span>-env<span style="color: #7a0874; font-weight: bold;">&#41;</span>stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>Django-1.2.1$<br />
<span style="color: #7a0874; font-weight: bold;">&#40;</span>django-<span style="color: #000000;">1.2</span>-env<span style="color: #7a0874; font-weight: bold;">&#41;</span>stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>Django-1.2.1$ python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></div></td></tr></tbody></table></div></p>

	<p>We verify here that Django is installed properly in the virtual site-packages:</p>

	<p><div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python<span style="color: #000000; font-weight: bold;">/</span>virtualenvs<span style="color: #000000; font-weight: bold;">/</span>django-<span style="color: #000000;">1.2</span>-env$ <span style="color: #c20cb9; font-weight: bold;">ls</span> lib<span style="color: #000000; font-weight: bold;">/</span>python2.6<span style="color: #000000; font-weight: bold;">/</span>site-packages<span style="color: #000000; font-weight: bold;">/</span><br />
django&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;easy-install.pth &nbsp; &nbsp; setuptools-0.6c11-py2.6.egg<br />
Django-1.2.1-py2.6.egg-info &nbsp;pip-0.7.2-py2.6.egg &nbsp;setuptools.pth</div></td></tr></tbody></table></div></p>

	<h1>6- Create a new project</h1>

	<p>At this point, we can create a new project using the command django-admin.py as required by the framework.</p>

	<p><div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">&#40;</span>django-<span style="color: #000000;">1.2</span>-env<span style="color: #7a0874; font-weight: bold;">&#41;</span>stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>Django-1.2.1$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>stefano<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python<span style="color: #000000; font-weight: bold;">/</span><br />
<span style="color: #7a0874; font-weight: bold;">&#40;</span>django-<span style="color: #000000;">1.2</span>-env<span style="color: #7a0874; font-weight: bold;">&#41;</span>stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python$ <br />
<span style="color: #7a0874; font-weight: bold;">&#40;</span>django-<span style="color: #000000;">1.2</span>-env<span style="color: #7a0874; font-weight: bold;">&#41;</span>stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python$ django-admin.py startproject newdjangoapp<br />
<span style="color: #7a0874; font-weight: bold;">&#40;</span>django-<span style="color: #000000;">1.2</span>-env<span style="color: #7a0874; font-weight: bold;">&#41;</span>stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python$ <span style="color: #7a0874; font-weight: bold;">cd</span> newdjangoapp<span style="color: #000000; font-weight: bold;">/</span><br />
<span style="color: #7a0874; font-weight: bold;">&#40;</span>django-<span style="color: #000000;">1.2</span>-env<span style="color: #7a0874; font-weight: bold;">&#41;</span>stefano<span style="color: #000000; font-weight: bold;">@</span>stefano-laptop:~<span style="color: #000000; font-weight: bold;">/</span>Progetti<span style="color: #000000; font-weight: bold;">/</span>Python<span style="color: #000000; font-weight: bold;">/</span>newdjangoapp$ <span style="color: #c20cb9; font-weight: bold;">ls</span><br />
__init__.py &nbsp;manage.py&nbsp; settings.py &nbsp;urls.py</div></td></tr></tbody></table></div></p>

	<p>In conclusion, we can say that virtualenv is an excellent solution to create environments fully independent and designed to accommodate each individual project. In this way, we avoid conflicts of libraries and have the opportunity to try new configurations without creating problems for other applications.<!-- Social Bookmarking Reloaded BEGIN --><div class="social_bookmark"><em>Share on:</em><br />
<a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/&#038;title=How+to+create+multiple+Django+environments+using+virtualenv" title="Add 'How to create multiple Django environments using virtualenv' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'How to create multiple Django environments using virtualenv' to Del.icio.us" alt="Add 'How to create multiple Django environments using virtualenv' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/&#038;title=How+to+create+multiple+Django+environments+using+virtualenv" title="Add 'How to create multiple Django environments using virtualenv' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'How to create multiple Django environments using virtualenv' to digg" alt="Add 'How to create multiple Django environments using virtualenv' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/&#038;title=How+to+create+multiple+Django+environments+using+virtualenv" title="Add 'How to create multiple Django environments using virtualenv' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'How to create multiple Django environments using virtualenv' to reddit" alt="Add 'How to create multiple Django environments using virtualenv' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/" title="Add 'How to create multiple Django environments using virtualenv' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'How to create multiple Django environments using virtualenv' to Technorati" alt="Add 'How to create multiple Django environments using virtualenv' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/&#038;title=How+to+create+multiple+Django+environments+using+virtualenv" title="Add 'How to create multiple Django environments using virtualenv' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'How to create multiple Django environments using virtualenv' to Stumble Upon" alt="Add 'How to create multiple Django environments using virtualenv' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/&#038;title=How+to+create+multiple+Django+environments+using+virtualenv" title="Add 'How to create multiple Django environments using virtualenv' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'How to create multiple Django environments using virtualenv' to Google Bookmarks" alt="Add 'How to create multiple Django environments using virtualenv' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/" title="Add 'How to create multiple Django environments using virtualenv' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'How to create multiple Django environments using virtualenv' to Rojo" alt="Add 'How to create multiple Django environments using virtualenv' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/&#038;title=How+to+create+multiple+Django+environments+using+virtualenv" title="Add 'How to create multiple Django environments using virtualenv' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'How to create multiple Django environments using virtualenv' to OKnotizie" alt="Add 'How to create multiple Django environments using virtualenv' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=How+to+create+multiple+Django+environments+using+virtualenv&#038;url=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/" title="Add 'How to create multiple Django environments using virtualenv' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'How to create multiple Django environments using virtualenv' to Diggita" alt="Add 'How to create multiple Django environments using virtualenv' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/&#038;t=How+to+create+multiple+Django+environments+using+virtualenv" title="Add 'How to create multiple Django environments using virtualenv' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'How to create multiple Django environments using virtualenv' to FaceBook" alt="Add 'How to create multiple Django environments using virtualenv' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/&#038;title=How+to+create+multiple+Django+environments+using+virtualenv" title="Add 'How to create multiple Django environments using virtualenv' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'How to create multiple Django environments using virtualenv' to Upnews" alt="Add 'How to create multiple Django environments using virtualenv' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/" title="Add 'How to create multiple Django environments using virtualenv' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'How to create multiple Django environments using virtualenv' to Twitter" alt="Add 'How to create multiple Django environments using virtualenv' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&#038;title=How+to+create+multiple+Django+environments+using+virtualenv&#038;url=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/" title="Add 'How to create multiple Django environments using virtualenv' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'How to create multiple Django environments using virtualenv' to Technotizie" alt="Add 'How to create multiple Django environments using virtualenv' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=How+to+create+multiple+Django+environments+using+virtualenv&#038;url=http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/" title="Add 'How to create multiple Django environments using virtualenv' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'How to create multiple Django environments using virtualenv' to FriendFeed" alt="Add 'How to create multiple Django environments using virtualenv' to FriendFeed" /></a></div><br />
<!-- Social Bookmarking Reloaded END --></p>
 <h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2009/10/system-information-application-for-nodia-s60v5-phones/" title="<!--:en-->System Information application for Nokia S60v5 phones<!--:--><!--:it-->Applicazione System Information per telefoni Nokia S60v5<!--:-->"><!--:en-->System Information application for Nokia S60v5 phones<!--:--><!--:it-->Applicazione System Information per telefoni Nokia S60v5<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="<!--:en-->Rails Best Practices 3: Increase Controller&#8217;s readability<!--:--><!--:it-->Rails Best Practices 3: Aumentare la leggibilità dei controller<!--:-->"><!--:en-->Rails Best Practices 3: Increase Controller&#8217;s readability<!--:--><!--:it-->Rails Best Practices 3: Aumentare la leggibilità dei controller<!--:--></a></li><li><a href="http://blog.devinterface.com/2009/12/add-current_user-to-cucumber-step-definitions/" title="<!--:en-->Add current_user to Cucumber step definitions<!--:--><!--:it-->Accedere a current_user negli step definitions di Cucumber<!--:-->"><!--:en-->Add current_user to Cucumber step definitions<!--:--><!--:it-->Accedere a current_user negli step definitions di Cucumber<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/02/new-projects/" title="<!--:en-->New Projects<!--:--><!--:it-->Nuovi progetti<!--:-->"><!--:en-->New Projects<!--:--><!--:it-->Nuovi progetti<!--:--></a></li><li><a href="http://blog.devinterface.com/2009/11/connect-a-dataset-instantiaded-by-code-to-a-crystalreport-reporting-tool/" title="<!--:en-->Connect a Dataset instantiaded by code to a CrystalReport reporting tool<!--:--><!--:it-->Connettere un Dataset istanziato da codice al tool di reportistica CrystalReport<!--:-->"><!--:en-->Connect a Dataset instantiaded by code to a CrystalReport reporting tool<!--:--><!--:it-->Connettere un Dataset istanziato da codice al tool di reportistica CrystalReport<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/PSN7HuvhBt0sx4Xt28OOiVqS1mQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/PSN7HuvhBt0sx4Xt28OOiVqS1mQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/PSN7HuvhBt0sx4Xt28OOiVqS1mQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/PSN7HuvhBt0sx4Xt28OOiVqS1mQ/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=sfQkEauF4Bk:exaNq04NmWA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=sfQkEauF4Bk:exaNq04NmWA:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=sfQkEauF4Bk:exaNq04NmWA:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=sfQkEauF4Bk:exaNq04NmWA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=sfQkEauF4Bk:exaNq04NmWA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/sfQkEauF4Bk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/08/how-to-create-multiple-django-environments-using-virtualenv/</feedburner:origLink></item>
		<item>
		<title>Rails Best Practices 5: Optimize Migration</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/h8S6QgXe_ns/</link>
		<comments>http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 09:35:39 +0000</pubDate>
		<dc:creator>Claudio</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Metodologies]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[seed]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=832</guid>
		<description><![CDATA[Migrations, in my opinion, are one of the best things in Rails since these allow the creation and populating the database using ruby code without having to worry about which type of db run below.
That said, even writing the migration is better to follow some best practices.

1. DB Index
The first practice I strongly recommend is [...]]]></description>
			<content:encoded><![CDATA[<p>Migrations, in my opinion, are one of the best things in Rails since these allow the creation and populating the database using ruby code without having to worry about which type of db run below.<br />
That said, even writing the migration is better to follow some best practices.<br />
<br/><br />
<strong>1. DB Index</strong><br />
The first practice I strongly recommend is to define indices for the external keys and for all those columns on which you will make sort, search and groups.<br />
Let&#8217;s take a sample migration:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> CreateInvoices <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span><br />
&nbsp; &nbsp; create_table <span style="color:#ff3333; font-weight:bold;">:invoices</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:number</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:year</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">decimal</span> <span style="color:#ff3333; font-weight:bold;">:total_amount</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">date</span> <span style="color:#ff3333; font-weight:bold;">:invoice_date</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:company_id</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:client_id</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">timestamps</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span><br />
&nbsp; &nbsp; drop_table <span style="color:#ff3333; font-weight:bold;">:invoices</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>This is a tipically  migration that will be generated by rails after execution of commands like generate Model or generate Scaffold.<br />
Now, let&#8217;s add indexes for the foreign keys and the sort fields.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> CreateInvoices <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span><br />
&nbsp; &nbsp; create_table <span style="color:#ff3333; font-weight:bold;">:invoices</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:number</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:year</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">decimal</span> <span style="color:#ff3333; font-weight:bold;">:total_amount</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">date</span> <span style="color:#ff3333; font-weight:bold;">:invoice_date</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:company_id</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:client_id</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">timestamps</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; &nbsp; add_index <span style="color:#ff3333; font-weight:bold;">:invoices</span>, <span style="color:#ff3333; font-weight:bold;">:company_id</span><br />
&nbsp; &nbsp; add_index <span style="color:#ff3333; font-weight:bold;">:invoices</span>, <span style="color:#ff3333; font-weight:bold;">:client_id</span><br />
&nbsp; &nbsp; add_index <span style="color:#ff3333; font-weight:bold;">:invoices</span>, <span style="color:#ff3333; font-weight:bold;">:number</span><br />
&nbsp; &nbsp; add_index <span style="color:#ff3333; font-weight:bold;">:invoices</span>, <span style="color:#ff3333; font-weight:bold;">:year</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span><br />
&nbsp; &nbsp; drop_table <span style="color:#ff3333; font-weight:bold;">:invoices</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Indexes definition is important for performance.<br />
A table indexed increase the performance of SQL queries.<br />
My advice therefore is: write indexes!<br />
There are also some plugins that help to identify the fields to index.<br />
By doing a quick search on github you can find different, I will point out just a few:</p>
<ul>
<li><a href="http://github.com/eladmeidar/rails_indexes">http://github.com/eladmeidar/rails_indexes</a></li>
<li><a href="http://github.com/mlomnicki/automatic_foreign_key">http://github.com/mlomnicki/automatic_foreign_key</a></li>
<li><a href="http://github.com/samdanavia/ambitious_query_indexer">http://github.com/samdanavia/ambitious_query_indexer</a></li>
</ul>
<p><strong>2. Write data seed</strong><br />
Since version 2.3.4 Rails introduced the concept of seed.<br />
In fact the idea was also present in earlier versions, but starting from release 2.3.4  was created a separate file and also an appropriate command to run the seed (ie the population) of data.<br />
When you start to develop web applications with Ruby on Rails, happen often to write migrations that contains both instructions for creating tables and those for populate the database with default values.<br />
A migration of this type is shown in the following example:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> CreateCompanies <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span><br />
&nbsp; &nbsp; create_table <span style="color:#ff3333; font-weight:bold;">:companies</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:name</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:street</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:city</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:country</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:email</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">timestamps</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; Company.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;DevInterface&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:street</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Via Postale Vecchia&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:city</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Verona&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:country</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Italy&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;info@devinterface.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:fax</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;045/1234567&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; Company.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;CDF Tech Solutions&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:street</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Via XX Settembre&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:city</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Verona&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:country</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Italy&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;info@cdf.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:fax</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;045/1234567&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span><br />
&nbsp; &nbsp; drop_table <span style="color:#ff3333; font-weight:bold;">:companies</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>A migration written in this way will run without error but it is always advisable to keep separate the structure of the db from data. This in order to repopulate the database with test data rather than real data as needed without having to recreate it each time.<br />
We see below how to write the file migration and seed file to separate the data from the schema.<br />
<em>Migration</em></p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> CreateCompanies <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span><br />
&nbsp; &nbsp; create_table <span style="color:#ff3333; font-weight:bold;">:companies</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:name</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:street</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:city</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:country</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:email</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:fax</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">timestamps</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span><br />
&nbsp; &nbsp; drop_table <span style="color:#ff3333; font-weight:bold;">:companies</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p><em>seeds.rb</em></p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; Company.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;DevInterface&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:street</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Via Postale Vecchia&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:city</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Verona&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:country</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Italy&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;info@devinterface.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:fax</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;045/1234567&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; Company.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;CDF Tech Solutions&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:street</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Via XX Settembre&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:city</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Verona&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:country</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Italy&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;info@cdf.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:fax</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;045/1234567&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></td></tr></tbody></table></div>
<p>Now after you create the database you will simply run the command rake db:seed to populate it.</p>
<p>Use the seed file is also useful to populate the database with large amounts of data generated at random or with specific data to simulate specific conditions.<br />
For this purpose there are many plugins for generating pseudo-random data.<br />
We list the ones I use most:</p>
<ul>
<li>Faker: <a href="http://faker.rubyforge.org/">http://faker.rubyforge.org/</a></li>
<li>Populator: <a href="http://github.com/ryanb/populator">http://github.com/ryanb/populator</a></li>
<li>Random Data: <a href="http://random-data.rubyforge.org/">http://random-data.rubyforge.org/</a></li>
</ul>
<p>I close this post about migrations with a sample seed file that fill the database with 5 company  pseudo-random generated by using the three plugins listed above:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'populator'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'faker'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'random_data'</span><br />
<br />
<span style="color:#006600; font-weight:bold;">&#91;</span>Company<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span> <span style="color:#ff3333; font-weight:bold;">:delete_all</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
Company.<span style="color:#9900CC;">populate</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>comp<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; comp.<span style="color:#9900CC;">name</span>= <span style="color:#6666ff; font-weight:bold;">Faker::Lorem</span>.<span style="color:#9900CC;">words</span><br />
&nbsp; comp.<span style="color:#9900CC;">street</span>= <span style="color:#6666ff; font-weight:bold;">Faker::Address</span>.<span style="color:#9900CC;">street_address</span><br />
&nbsp; comp.<span style="color:#9900CC;">city</span>= <span style="color:#996600;">&quot;Verona&quot;</span><br />
&nbsp; comp.<span style="color:#9900CC;">fax</span> = <span style="color:#996600;">'045 / '</span> <span style="color:#006600; font-weight:bold;">+</span> Random.<span style="color:#9900CC;">number</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">+</span> Random.<span style="color:#9900CC;">number</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">450</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span><br />
&nbsp; comp.<span style="color:#9900CC;">email</span> = <span style="color:#6666ff; font-weight:bold;">Faker::Internet</span>.<span style="color:#9900CC;">email</span></div></td></tr></tbody></table></div>
<p><!-- Social Bookmarking Reloaded BEGIN -->
<div class="social_bookmark"><em>Share on:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/&amp;title=Rails+Best+Practices+5%3A+Optimize+Migration" title="Add 'Rails Best Practices 5: Optimize Migration' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'Rails Best Practices 5: Optimize Migration' to Del.icio.us" alt="Add 'Rails Best Practices 5: Optimize Migration' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/&amp;title=Rails+Best+Practices+5%3A+Optimize+Migration" title="Add 'Rails Best Practices 5: Optimize Migration' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Rails Best Practices 5: Optimize Migration' to digg" alt="Add 'Rails Best Practices 5: Optimize Migration' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/&amp;title=Rails+Best+Practices+5%3A+Optimize+Migration" title="Add 'Rails Best Practices 5: Optimize Migration' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Rails Best Practices 5: Optimize Migration' to reddit" alt="Add 'Rails Best Practices 5: Optimize Migration' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/" title="Add 'Rails Best Practices 5: Optimize Migration' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Rails Best Practices 5: Optimize Migration' to Technorati" alt="Add 'Rails Best Practices 5: Optimize Migration' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/&amp;title=Rails+Best+Practices+5%3A+Optimize+Migration" title="Add 'Rails Best Practices 5: Optimize Migration' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Rails Best Practices 5: Optimize Migration' to Stumble Upon" alt="Add 'Rails Best Practices 5: Optimize Migration' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/&amp;title=Rails+Best+Practices+5%3A+Optimize+Migration" title="Add 'Rails Best Practices 5: Optimize Migration' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'Rails Best Practices 5: Optimize Migration' to Google Bookmarks" alt="Add 'Rails Best Practices 5: Optimize Migration' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/" title="Add 'Rails Best Practices 5: Optimize Migration' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'Rails Best Practices 5: Optimize Migration' to Rojo" alt="Add 'Rails Best Practices 5: Optimize Migration' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/&amp;title=Rails+Best+Practices+5%3A+Optimize+Migration" title="Add 'Rails Best Practices 5: Optimize Migration' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'Rails Best Practices 5: Optimize Migration' to OKnotizie" alt="Add 'Rails Best Practices 5: Optimize Migration' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=Rails+Best+Practices+5%3A+Optimize+Migration&amp;url=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/" title="Add 'Rails Best Practices 5: Optimize Migration' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'Rails Best Practices 5: Optimize Migration' to Diggita" alt="Add 'Rails Best Practices 5: Optimize Migration' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/&amp;t=Rails+Best+Practices+5%3A+Optimize+Migration" title="Add 'Rails Best Practices 5: Optimize Migration' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Rails Best Practices 5: Optimize Migration' to FaceBook" alt="Add 'Rails Best Practices 5: Optimize Migration' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/&amp;title=Rails+Best+Practices+5%3A+Optimize+Migration" title="Add 'Rails Best Practices 5: Optimize Migration' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'Rails Best Practices 5: Optimize Migration' to Upnews" alt="Add 'Rails Best Practices 5: Optimize Migration' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/" title="Add 'Rails Best Practices 5: Optimize Migration' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'Rails Best Practices 5: Optimize Migration' to Twitter" alt="Add 'Rails Best Practices 5: Optimize Migration' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&amp;title=Rails+Best+Practices+5%3A+Optimize+Migration&amp;url=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/" title="Add 'Rails Best Practices 5: Optimize Migration' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'Rails Best Practices 5: Optimize Migration' to Technotizie" alt="Add 'Rails Best Practices 5: Optimize Migration' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=Rails+Best+Practices+5%3A+Optimize+Migration&amp;url=http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/" title="Add 'Rails Best Practices 5: Optimize Migration' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'Rails Best Practices 5: Optimize Migration' to FriendFeed" alt="Add 'Rails Best Practices 5: Optimize Migration' to FriendFeed" /></a></div>
<p><!-- Social Bookmarking Reloaded END --></p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="<!--:en-->Rails Best Practices 6: Filters and helpers file<!--:--><!--:it-->Rails Best Practices 6: Filtri e file helper<!--:-->"><!--:en-->Rails Best Practices 6: Filters and helpers file<!--:--><!--:it-->Rails Best Practices 6: Filtri e file helper<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-introduction/" title="<!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:-->"><!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/partial-validation-in-rails/" title="<!--:en-->Partial Validation in Rails<!--:--><!--:it-->Validazione Parziale in Rails<!--:-->"><!--:en-->Partial Validation in Rails<!--:--><!--:it-->Validazione Parziale in Rails<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/05/how-to-model-a-custom-search-form-in-rails/" title="<!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:-->"><!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="<!--:en-->Rails Best Practices 4: Put method in the right model<!--:--><!--:it-->Rails Best Practices 4: Scrivere i metodi nel model corretto<!--:-->"><!--:en-->Rails Best Practices 4: Put method in the right model<!--:--><!--:it-->Rails Best Practices 4: Scrivere i metodi nel model corretto<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/3JWfCoHyXWzkNDSaQQyGvm5Es7I/0/da"><img src="http://feedads.g.doubleclick.net/~a/3JWfCoHyXWzkNDSaQQyGvm5Es7I/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/3JWfCoHyXWzkNDSaQQyGvm5Es7I/1/da"><img src="http://feedads.g.doubleclick.net/~a/3JWfCoHyXWzkNDSaQQyGvm5Es7I/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=h8S6QgXe_ns:xwJaapNcrLA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=h8S6QgXe_ns:xwJaapNcrLA:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=h8S6QgXe_ns:xwJaapNcrLA:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=h8S6QgXe_ns:xwJaapNcrLA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=h8S6QgXe_ns:xwJaapNcrLA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/h8S6QgXe_ns" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/08/rails-best-practices-5-optimize-migration/</feedburner:origLink></item>
		<item>
		<title>Rails Best Practices 4: Put method in the right model</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/3RhKUxCxkuw/</link>
		<comments>http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 09:35:51 +0000</pubDate>
		<dc:creator>Claudio</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[named_scope]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=802</guid>
		<description><![CDATA[In today&#8217;s post I will show some optimization we can do for models. I&#8217;ll focus on how to put methods inside the right model and delegation to get a better code.
1.  Put method in the right model
In our example, suppose we want to represent the animal world by creating a model Kind that represents [...]]]></description>
			<content:encoded><![CDATA[<p>In today&#8217;s post I will show some optimization we can do for models. I&#8217;ll focus on how to put methods inside the right model and delegation to get a better code.</p>
<p><strong>1.  Put method in the right model</strong></p>
<p>In our example, suppose we want to represent the animal world by creating a model Kind that represents types of animal and an Animal models representing animals.<br />
For each type (quadrupedal, bipedal, bird) there are different animals that justify  the  has_many: animals  relation defined in the following code.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Kind <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><br />
&nbsp; has_many <span style="color:#ff3333; font-weight:bold;">:animals</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> find_herbivores<br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">animal</span>.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:eat_herb</span> &nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> Animal <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><br />
&nbsp; belongs_to <span style="color:#ff3333; font-weight:bold;">:kind</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Next, we define a method &#8220;herbivores&#8221; within the AnimalsController that, given a type of animal return all the herbivorous contained in that type.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> AnimalsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> herbivores<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@kind</span> = Kind.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@animals</span> = <span style="color:#0066ff; font-weight:bold;">@kind</span>.<span style="color:#9900CC;">find_herbivores</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>The flaw in this code is to have defined the method find_herbivores inside the Kind model when in fact refers to a property of the Animal class.</p>
<p>Let&#8217;s see how to rewrite it in proper way, using a named_scope in the Animal model.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Kind <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><br />
&nbsp; has_many <span style="color:#ff3333; font-weight:bold;">:animals</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> Animal <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><br />
&nbsp; belongs_to <span style="color:#ff3333; font-weight:bold;">:kind</span><br />
&nbsp; named_scope <span style="color:#ff3333; font-weight:bold;">:herbivores</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:eat_herb</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> AnimalsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> herbivores<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@kind</span> = Kind.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@animals</span> = <span style="color:#0066ff; font-weight:bold;">@kind</span>.<span style="color:#9900CC;">animals</span>.<span style="color:#9900CC;">herbivores</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p><strong>2. Delegate</strong></p>
<p>In this second example, suppose we have a Location class associated with an owner and want to show owner&#8217;s data in the Location&#8217;s view.<br />
A first simple implementation is the following:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Location <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><br />
&nbsp; belongs_to <span style="color:#ff3333; font-weight:bold;">:owner</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>And in the view:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">owner</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">owner</span>.<span style="color:#9900CC;">city</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">owner</span>.<span style="color:#9900CC;">street</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">owner</span>.<span style="color:#9900CC;">email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></td></tr></tbody></table></div>
<p>This type of implementation is very common in Rails applications, but you can make it more elegant by using the construct &#8220;delegate&#8221; as follows:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Location <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><br />
&nbsp; belongs_to <span style="color:#ff3333; font-weight:bold;">:owner</span><br />
&nbsp; delegate <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:city</span> <span style="color:#ff3333; font-weight:bold;">:street</span>, <span style="color:#ff3333; font-weight:bold;">:email</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;">:owner</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:prefix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Now we can rewrite the view as follows:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">owner_name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">owner_city</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">owner_street</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">owner_email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></td></tr></tbody></table></div>
<p>The result does not change, but the code is certainly more elegant.<!-- Social Bookmarking Reloaded BEGIN -->
<div class="social_bookmark"><em>Share on:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/&amp;title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model" title="Add 'Rails Best Practices 4: Put method in the right model' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'Rails Best Practices 4: Put method in the right model' to Del.icio.us" alt="Add 'Rails Best Practices 4: Put method in the right model' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/&amp;title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model" title="Add 'Rails Best Practices 4: Put method in the right model' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Rails Best Practices 4: Put method in the right model' to digg" alt="Add 'Rails Best Practices 4: Put method in the right model' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/&amp;title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model" title="Add 'Rails Best Practices 4: Put method in the right model' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Rails Best Practices 4: Put method in the right model' to reddit" alt="Add 'Rails Best Practices 4: Put method in the right model' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="Add 'Rails Best Practices 4: Put method in the right model' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Rails Best Practices 4: Put method in the right model' to Technorati" alt="Add 'Rails Best Practices 4: Put method in the right model' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/&amp;title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model" title="Add 'Rails Best Practices 4: Put method in the right model' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Rails Best Practices 4: Put method in the right model' to Stumble Upon" alt="Add 'Rails Best Practices 4: Put method in the right model' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/&amp;title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model" title="Add 'Rails Best Practices 4: Put method in the right model' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'Rails Best Practices 4: Put method in the right model' to Google Bookmarks" alt="Add 'Rails Best Practices 4: Put method in the right model' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="Add 'Rails Best Practices 4: Put method in the right model' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'Rails Best Practices 4: Put method in the right model' to Rojo" alt="Add 'Rails Best Practices 4: Put method in the right model' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/&amp;title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model" title="Add 'Rails Best Practices 4: Put method in the right model' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'Rails Best Practices 4: Put method in the right model' to OKnotizie" alt="Add 'Rails Best Practices 4: Put method in the right model' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model&amp;url=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="Add 'Rails Best Practices 4: Put method in the right model' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'Rails Best Practices 4: Put method in the right model' to Diggita" alt="Add 'Rails Best Practices 4: Put method in the right model' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/&amp;t=Rails+Best+Practices+4%3A+Put+method+in+the+right+model" title="Add 'Rails Best Practices 4: Put method in the right model' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Rails Best Practices 4: Put method in the right model' to FaceBook" alt="Add 'Rails Best Practices 4: Put method in the right model' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/&amp;title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model" title="Add 'Rails Best Practices 4: Put method in the right model' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'Rails Best Practices 4: Put method in the right model' to Upnews" alt="Add 'Rails Best Practices 4: Put method in the right model' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="Add 'Rails Best Practices 4: Put method in the right model' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'Rails Best Practices 4: Put method in the right model' to Twitter" alt="Add 'Rails Best Practices 4: Put method in the right model' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&amp;title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model&amp;url=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="Add 'Rails Best Practices 4: Put method in the right model' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'Rails Best Practices 4: Put method in the right model' to Technotizie" alt="Add 'Rails Best Practices 4: Put method in the right model' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=Rails+Best+Practices+4%3A+Put+method+in+the+right+model&amp;url=http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="Add 'Rails Best Practices 4: Put method in the right model' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'Rails Best Practices 4: Put method in the right model' to FriendFeed" alt="Add 'Rails Best Practices 4: Put method in the right model' to FriendFeed" /></a></div>
<p><!-- Social Bookmarking Reloaded END --></p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="<!--:en-->Rails Best Practices 6: Filters and helpers file<!--:--><!--:it-->Rails Best Practices 6: Filtri e file helper<!--:-->"><!--:en-->Rails Best Practices 6: Filters and helpers file<!--:--><!--:it-->Rails Best Practices 6: Filtri e file helper<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="<!--:en-->Rails Best Practices 3: Increase Controller&#8217;s readability<!--:--><!--:it-->Rails Best Practices 3: Aumentare la leggibilità dei controller<!--:-->"><!--:en-->Rails Best Practices 3: Increase Controller&#8217;s readability<!--:--><!--:it-->Rails Best Practices 3: Aumentare la leggibilità dei controller<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/" title="<!--:en-->Rails Best Practices 2: Move logic to model<!--:--><!--:it-->Rails Best Practices 2: Spostare la logica nel model<!--:-->"><!--:en-->Rails Best Practices 2: Move logic to model<!--:--><!--:it-->Rails Best Practices 2: Spostare la logica nel model<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-1-fat-model-skinny-controller/" title="<!--:en-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:--><!--:it-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:-->"><!--:en-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:--><!--:it-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-introduction/" title="<!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:-->"><!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/_6TB3QXvp32kIXArCCI0S_MkU1Y/0/da"><img src="http://feedads.g.doubleclick.net/~a/_6TB3QXvp32kIXArCCI0S_MkU1Y/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/_6TB3QXvp32kIXArCCI0S_MkU1Y/1/da"><img src="http://feedads.g.doubleclick.net/~a/_6TB3QXvp32kIXArCCI0S_MkU1Y/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=3RhKUxCxkuw:FY-Hcytx8fY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=3RhKUxCxkuw:FY-Hcytx8fY:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=3RhKUxCxkuw:FY-Hcytx8fY:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=3RhKUxCxkuw:FY-Hcytx8fY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=3RhKUxCxkuw:FY-Hcytx8fY:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/3RhKUxCxkuw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/</feedburner:origLink></item>
		<item>
		<title>Design Patterns in Ruby: Chain of Responsibility</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/zt0p3IDjBWw/</link>
		<comments>http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 07:53:29 +0000</pubDate>
		<dc:creator>Stefano</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Metodologies]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=791</guid>
		<description><![CDATA[	Today&#8217;s post discusses the first of the behavioral pattern shown by the GoF, the chain of responsibility.

	This pattern expects a series of commands to be executed and a set of objects capable to handle them.
Each of these &#8220;handler&#8221; objects can send the command to the next handler in the chain if it is not able [...]]]></description>
			<content:encoded><![CDATA[	<p>Today&#8217;s post discusses the first of the behavioral pattern shown by the GoF, the chain of responsibility.</p>

	<p>This pattern expects a series of commands to be executed and a set of objects capable to handle them.<br />
Each of these &#8220;handler&#8221; objects can send the command to the next handler in the chain if it is not able to carry it out.<br />
A mechanism also exists for adding new handler objects to the end of this chain.</p>

	<p>To show this pattern, we take as example a real situation very familiar to web developers.</p>

	<p>Suppose that the manager must deliver a new web project.</p>

	<p>To realize the entire project should be conducted several heterogeneous activities such as design the user interface, develop the application, write the user manual, deploy the application.</p>

	<p>The manager doesn&#8217;t have all required skills but he can rely on a pool of developers.</p>

	<p>When the activity reaches a developer, this can solve it or, if he is unable to, send it to a colleague.<br />
In this way a chain of responsibility is formed, where each actor specializes in solving only certain types of requests.</p>

	<p>Let&#8217;s see how to carry out this scenario in Ruby.</p>

	<p>Is therefore necessary that every element in the chain has the ability to &#8220;forward&#8221; the request to the next if he is not able to manage it.</p>

	<p>So we create a module that defines this common logic and removes code duplication.</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#models.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">module</span> Chainable<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> next_in_chain<span style="color:#006600; font-weight:bold;">&#40;</span>link<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; @<span style="color:#9966CC; font-weight:bold;">next</span> = link<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>method, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> @<span style="color:#9966CC; font-weight:bold;">next</span> == <span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;This request cannot be handled!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; @<span style="color:#9966CC; font-weight:bold;">next</span>.__send__<span style="color:#006600; font-weight:bold;">&#40;</span>method, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div></p>

	<p>As you can see, the next_in_chain method provides the next element in the chain.<br />
To meet demands that can not be managed, I&#8217;ve used the method_missing &#8220;pattern&#8221; : when the request can not be managed by the actor (e.g. the invoked method is not defined in the class), it will be forwarded to the next.</p>

	<p>Now define the players of our example:</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#models.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">class</span> WebManager<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> Chainable<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>link = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; next_in_chain<span style="color:#006600; font-weight:bold;">&#40;</span>link<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> deliver_application<br />
&nbsp; &nbsp; design_interface<br />
&nbsp; &nbsp; build_application<br />
&nbsp; &nbsp; write_documentation<br />
&nbsp; &nbsp; deploy_application<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{self.class.to_s}: Application delivered&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> WebDeveloper<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> Chainable<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>link = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; next_in_chain<span style="color:#006600; font-weight:bold;">&#40;</span>link<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> build_application<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{self.class.to_s}: I'm building the application&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> deploy_application<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{self.class.to_s}: I'm deploying the application&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> WebDesigner<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> Chainable<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>link = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; next_in_chain<span style="color:#006600; font-weight:bold;">&#40;</span>link<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> design_interface<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{self.class.to_s}: I'm designing the interface&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> TechnicalWriter<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> Chainable<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>link = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; next_in_chain<span style="color:#006600; font-weight:bold;">&#40;</span>link<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> write_documentation<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{self.class.to_s}: I'm writing the documentation&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div></p>

	<p>At this point we simulate our chain by running the following code:</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#main.rb</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'models.rb'</span><br />
<br />
provider = WebManager.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>WebDeveloper.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>WebDesigner.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>TechnicalWriter.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
provider.<span style="color:#9900CC;">deliver_application</span><br />
provider.<span style="color:#9900CC;">make_support</span></div></td></tr></tbody></table></div></p>

	<p>and the output will be:</p>

	<p>WebDesigner: I&#8217;m designing the interface<br />
WebDeveloper: I&#8217;m building the application<br />
TechnicalWriter: I&#8217;m writing documentation<br />
WebDeveloper: I&#8217;m deploying the application<br />
WebManager: Application delivered<br />
This request cannot be handled!</p>

	<p>In conclusion, the use of this pattern is useful when we deal with heterogeneous requests and we want to make sure that these are best handled by a specific handler.<br />
It can also be used when we run commands in sequence, where each element forwards the coming command to the next handler.</p>

	<p>An alternative to this pattern could be a decorator, able to add capacity to a specific handler. In this case a single handler will completely manage all requests.</p>

	<p>In these first three articles of the series we have analyzed the first pattern shown by the GoF for each category.</p>

	<p>In the coming articles I will no more follow the order set by the GoF but I&#8217;ll analyze the most useful patterns to address the most common needs.</p>

	<p>Source code <a href="http://github.com/devinterface/design_patterns_in_ruby" title="">here</a></p>

	<p>Previous posts from this series:<br />
<a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="">Design Patterns in Ruby: Introduction</a><br />
<a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="">Design Patterns in Ruby: Abstract Factory</a><br />
<a href="http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="">Design Patterns in Ruby: Adapter</a><!-- Social Bookmarking Reloaded BEGIN --><div class="social_bookmark"><em>Share on:</em><br />
<a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/&#038;title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Del.icio.us" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/&#038;title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to digg" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/&#038;title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to reddit" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Technorati" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/&#038;title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Stumble Upon" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/&#038;title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Google Bookmarks" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Rojo" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/&#038;title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to OKnotizie" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility&#038;url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Diggita" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/&#038;t=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to FaceBook" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/&#038;title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Upnews" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Twitter" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&#038;title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility&#038;url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to Technotizie" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=Design+Patterns+in+Ruby%3A+Chain+of+Responsibility&#038;url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'Design Patterns in Ruby: Chain of Responsibility' to FriendFeed" alt="Add 'Design Patterns in Ruby: Chain of Responsibility' to FriendFeed" /></a></div><br />
<!-- Social Bookmarking Reloaded END --></p>
 <h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="<!--:en-->Design Patterns in Ruby: Adapter<!--:--><!--:it-->Design Patterns in Ruby: Adapter<!--:-->"><!--:en-->Design Patterns in Ruby: Adapter<!--:--><!--:it-->Design Patterns in Ruby: Adapter<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="<!--:en-->Design Patterns in Ruby: Abstract Factory<!--:--><!--:it-->Design Patterns in Ruby: Abstract Factory<!--:-->"><!--:en-->Design Patterns in Ruby: Abstract Factory<!--:--><!--:it-->Design Patterns in Ruby: Abstract Factory<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="<!--:en-->Design Patterns in Ruby: Introduction <!--:--><!--:it-->Design Patterns in Ruby: Introduzione<!--:-->"><!--:en-->Design Patterns in Ruby: Introduction <!--:--><!--:it-->Design Patterns in Ruby: Introduzione<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/05/how-to-model-a-custom-search-form-in-rails/" title="<!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:-->"><!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/02/create-zip-files-on-the-fly/" title="<!--:en-->Create zip files on the fly with ruby<!--:--><!--:it-->Creare un file zip al volo con ruby<!--:-->"><!--:en-->Create zip files on the fly with ruby<!--:--><!--:it-->Creare un file zip al volo con ruby<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/BztW-zeAQ_MoVA5MFHMZeTN4wsI/0/da"><img src="http://feedads.g.doubleclick.net/~a/BztW-zeAQ_MoVA5MFHMZeTN4wsI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/BztW-zeAQ_MoVA5MFHMZeTN4wsI/1/da"><img src="http://feedads.g.doubleclick.net/~a/BztW-zeAQ_MoVA5MFHMZeTN4wsI/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=zt0p3IDjBWw:HQZqlTihPv0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=zt0p3IDjBWw:HQZqlTihPv0:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=zt0p3IDjBWw:HQZqlTihPv0:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=zt0p3IDjBWw:HQZqlTihPv0:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=zt0p3IDjBWw:HQZqlTihPv0:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/zt0p3IDjBWw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/</feedburner:origLink></item>
		<item>
		<title>Rails Best Practices 3: Increase Controller’s readability</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/sbvAUOf-Tro/</link>
		<comments>http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 08:45:55 +0000</pubDate>
		<dc:creator>Claudio</dc:creator>
				<category><![CDATA[Metodologies]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[factory method]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=767</guid>
		<description><![CDATA[[POST UPDATE ON 19/07/2010 - 15:45]
Continuing our analysis of the Rails Best Practices today we&#8217;ll see two other tricks to make more readable method of the controller.
1. Methods within model
Suppose we have the classic user registry defined in such a way that an admin user can enable or disable other users.
The deactivate method inside user [...]]]></description>
			<content:encoded><![CDATA[<p>[POST UPDATE ON 19/07/2010 - 15:45]<br />
Continuing our analysis of the Rails Best Practices today we&#8217;ll see two other tricks to make more readable method of the controller.</p>
<p><strong>1. Methods within model</strong></p>
<p>Suppose we have the classic user registry defined in such a way that an admin user can enable or disable other users.</p>
<p>The deactivate method inside user Controller may be defined as follows:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> UserController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> deactivate<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">admin</span>?<br />
&nbsp; &nbsp; &nbsp; flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;You can not deactivate Admin user&quot;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">update_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:status</span>, <span style="color:#996600;">&quot;N&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">modified_by</span> = current_user<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">save</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Error during user deactivation&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; redirect_to <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'index'</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>What is clear is that this method has a lot of business logic inside. We see then how to define a method in the User Model to implement business logic so we can streamline this method.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> ActiveRecord:Base<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> deactivate<span style="color:#006600; font-weight:bold;">&#40;</span>user<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; ret = <span style="color:#0000FF; font-weight:bold;">false</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">update_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:status</span>, <span style="color:#996600;">&quot;N&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">modified_by</span> = user<br />
&nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">save</span><br />
&nbsp; &nbsp; &nbsp; ret = <span style="color:#0000FF; font-weight:bold;">true</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> <br />
&nbsp; &nbsp; ret<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> UserController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> deactivate<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">admin</span>?<br />
&nbsp; &nbsp; &nbsp; flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;You can not deactivate Admin user&quot;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> !@user.<span style="color:#9900CC;">deactivate</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_user<span style="color:#006600; font-weight:bold;">&#41;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Error during user deactivation&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; redirect_to <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'index'</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>This rewrite makes the deactivate method within Controller more readable and more easily testable.</p>
<p><strong>2. Factory Method</strong><br />
In a similar way to what has been done for the deactivate method, sometimes can be handy while writing a create method, to define a Factory Method in the model that contains the logic of creation / initialization of the object and then call it from the controller.<br />
As usual, let&#8217;s see an example.<br />
Imagine you are developing a new management system where a news must be associated with the user who created it and it must also be visible only for a week.<br />
A solution, without the Factory Method is the following:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> PostController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:post</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">user</span> = current_user<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">expiring_date</span> = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">7</span>.<span style="color:#9900CC;">day</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">save</span>&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Let us now see how to define a proper method (the Factory Method) of creating the news in the model so, once again, we simplify the controller</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Post <span style="color:#006600; font-weight:bold;">&lt;</span> &nbsp;ActiveRecord:Base<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> publish_new_post<span style="color:#006600; font-weight:bold;">&#40;</span>params, user<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; post = Post.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; post.<span style="color:#9900CC;">user</span> = user<br />
&nbsp; &nbsp; post.<span style="color:#9900CC;">expiring_date</span> = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">7</span>.<span style="color:#9900CC;">day</span><br />
&nbsp; &nbsp; post.<span style="color:#9900CC;">save</span> &nbsp; &nbsp; &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> PostController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">publish_new_post</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:post</span><span style="color:#006600; font-weight:bold;">&#93;</span>, current_user<span style="color:#006600; font-weight:bold;">&#41;</span> &nbsp; &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Once again we managed to write a better code in the Controller by moving the logic in the Model.</p>
<p>This post concludes the first part of this series when I tried to illustrate some best practices that allow to simplify the controller and move the business logic in the Model.</p>
<p>From the next post I will start to illustrate some Best Practices applicable to the Models.<!-- Social Bookmarking Reloaded BEGIN -->
<div class="social_bookmark"><em>Share on:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/&amp;title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Del.icio.us" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/&amp;title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to digg" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/&amp;title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to reddit" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Technorati" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/&amp;title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Stumble Upon" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/&amp;title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Google Bookmarks" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Rojo" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/&amp;title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to OKnotizie" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability&amp;url=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Diggita" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/&amp;t=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to FaceBook" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/&amp;title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Upnews" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Twitter" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&amp;title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability&amp;url=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Technotizie" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=Rails+Best+Practices+3%3A+Increase+Controller%26%238217%3Bs+readability&amp;url=http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to FriendFeed" alt="Add 'Rails Best Practices 3: Increase Controller&#8217;s readability' to FriendFeed" /></a></div>
<p><!-- Social Bookmarking Reloaded END --></p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="<!--:en-->Rails Best Practices 6: Filters and helpers file<!--:--><!--:it-->Rails Best Practices 6: Filtri e file helper<!--:-->"><!--:en-->Rails Best Practices 6: Filters and helpers file<!--:--><!--:it-->Rails Best Practices 6: Filtri e file helper<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="<!--:en-->Rails Best Practices 4: Put method in the right model<!--:--><!--:it-->Rails Best Practices 4: Scrivere i metodi nel model corretto<!--:-->"><!--:en-->Rails Best Practices 4: Put method in the right model<!--:--><!--:it-->Rails Best Practices 4: Scrivere i metodi nel model corretto<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/" title="<!--:en-->Rails Best Practices 2: Move logic to model<!--:--><!--:it-->Rails Best Practices 2: Spostare la logica nel model<!--:-->"><!--:en-->Rails Best Practices 2: Move logic to model<!--:--><!--:it-->Rails Best Practices 2: Spostare la logica nel model<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-1-fat-model-skinny-controller/" title="<!--:en-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:--><!--:it-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:-->"><!--:en-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:--><!--:it-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-introduction/" title="<!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:-->"><!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/SsLG16yV9uiD0Hipi_K-Pi9vLtg/0/da"><img src="http://feedads.g.doubleclick.net/~a/SsLG16yV9uiD0Hipi_K-Pi9vLtg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/SsLG16yV9uiD0Hipi_K-Pi9vLtg/1/da"><img src="http://feedads.g.doubleclick.net/~a/SsLG16yV9uiD0Hipi_K-Pi9vLtg/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=sbvAUOf-Tro:YpSKzdDH7Og:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=sbvAUOf-Tro:YpSKzdDH7Og:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=sbvAUOf-Tro:YpSKzdDH7Og:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=sbvAUOf-Tro:YpSKzdDH7Og:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=sbvAUOf-Tro:YpSKzdDH7Og:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/sbvAUOf-Tro" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/</feedburner:origLink></item>
		<item>
		<title>Design Patterns in Ruby: Adapter</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/Cgj1l10Rkgk/</link>
		<comments>http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 10:13:26 +0000</pubDate>
		<dc:creator>Stefano</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Metodologies]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=754</guid>
		<description><![CDATA[	This second post of the series leaves for a moment the creational patterns and speaks about one of the most important structural pattern: the Adapter.

	The purpose of an adapter is &#8220;to convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn&#8217;t otherwise because of incompatible interfaces.&#8221;

	Suppose therefore [...]]]></description>
			<content:encoded><![CDATA[	<p>This second post of the series leaves for a moment the creational patterns and speaks about one of the most important structural pattern: the Adapter.</p>

	<p>The purpose of an adapter is &#8220;to convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn&#8217;t otherwise because of incompatible interfaces.&#8221;</p>

	<p>Suppose therefore to have two classes, PalGame and NtscGame, that extend a superclass Game. These subclasses respectively expose two methods: <em>play</em> and <em>run</em>.</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#models.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">class</span> Game<br />
&nbsp; attr_accessor <span style="color:#ff3333; font-weight:bold;">:title</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>title<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@title</span> = title<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> PalGame <span style="color:#006600; font-weight:bold;">&lt;</span> Game<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> play<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I am the Pal version of #{@title} and I am running!&quot;</span> &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> NtscGame <span style="color:#006600; font-weight:bold;">&lt;</span> Game &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> run<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I am the NTSC version of #{@title} and I am running!&quot;</span> &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div></p>

	<p>We then subclass a Console class with PalConsole and NtscConsole. These two classes are expected to talk with, respectively, games of kind PalGame and NtscGame.</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#models.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">class</span> Console<br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> PalConsole <span style="color:#006600; font-weight:bold;">&lt;</span> Console<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> play_game<span style="color:#006600; font-weight:bold;">&#40;</span>game<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; game.<span style="color:#9900CC;">play</span> &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> NtscConsole <span style="color:#006600; font-weight:bold;">&lt;</span> Console<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> run_game<span style="color:#006600; font-weight:bold;">&#40;</span>game<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; game.<span style="color:#9900CC;">run</span> &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div></p>

	<p>How can see, the method <i>play_game</i> of a PalConsole will call the <em>play</em> method of the game, the NtscConsole instead will invoke the <em>run</em> method.</p>

	<p>Our goal is to let a PalConsole to run games of kind NtscGame.</p>

	<p>Below we&#8217;ll follow the line traced by the GoF and we&#8217;ll build an Adapter class that provides the <em>play</em> method needed by the PalConsole&#8217;s interface:</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#adapters.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">class</span> NtscToPalAdatper<br />
&nbsp; attr_accessor <span style="color:#ff3333; font-weight:bold;">:game</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>game<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@game</span> = game<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> play<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@game</span>.<span style="color:#9900CC;">run</span> &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> &nbsp;<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div></p>

	<p>As you can see, the adapter exposes a simple <em>play</em> method that calls the <em>run</em> method of the <span class="caps">NTSC</span>Game.</p>

	<p>The following code:</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#main.rb</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'models.rb'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'adapters.rb'</span><br />
<br />
console = PalConsole.<span style="color:#9900CC;">new</span><br />
<br />
final_fantasy = NtscGame.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Final Fantasy&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
adapter = NtscToPalAdatper.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>final_fantasy<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
console.<span style="color:#9900CC;">play_game</span><span style="color:#006600; font-weight:bold;">&#40;</span>adapter<span style="color:#006600; font-weight:bold;">&#41;</span></div></td></tr></tbody></table></div></p>

	<p>will produce this output:<br />
I am the <span class="caps">NTSC</span> version of Final Fantasy and I am running!</p>


	<p>We see at this point some alternatives to further exploit the potential of Ruby.</p>

	<p>One possibility is to take advantage of the Ruby&#8217;s &#8220;open classes&#8221;. The language makes possible to add methods to an already loaded class in this way:</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#main2.rb</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'models.rb'</span><br />
<br />
console = PalConsole.<span style="color:#9900CC;">new</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> NtscGame <span style="color:#006600; font-weight:bold;">&lt;</span> Game &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> play<br />
&nbsp; &nbsp; run <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#008000; font-style:italic;"># alternatively for this simple example we can define an alias:</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># alias play run</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span> <br />
<br />
final_fantasy = NtscGame.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Final Fantasy&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
double_dragon = NtscGame.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Double Dragon&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
console.<span style="color:#9900CC;">play_game</span><span style="color:#006600; font-weight:bold;">&#40;</span>final_fantasy<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
console.<span style="color:#9900CC;">play_game</span><span style="color:#006600; font-weight:bold;">&#40;</span>double_dragon<span style="color:#006600; font-weight:bold;">&#41;</span></div></td></tr></tbody></table></div></p>

	<p>As we can see, we&#8217;ve added at runtime the method <em>play</em> for a NtscGame game.</p>

	<p>The above code produces the following output:<br />
I am the <span class="caps">NTSC</span> version of Final Fantasy and I am running!<br />
I am the <span class="caps">NTSC</span> version of Double Dragon and I am running!</p>

	<p>Note however that with this solution we added the <em>play</em> method to the entire class NtscGame. All instances that are created will be equipped with the <em>play</em> method.<br />
This type of action is very risky because it could not guarantee compatibility with other libraries, for example because of name clash.</p>

	<p>Another alternative would be to use the &#8220;singleton classes&#8221;.<br />
Ruby makes it possible to change a single instance of a class, by creating an anonymous class as its superclass that implements the new defined method.</p>

	<p>There are several possibilities to implement these singleton classes. Let&#8217;s see in the following snippet some implementations.</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#main3.rb</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'models.rb'</span><br />
<br />
console = PalConsole.<span style="color:#9900CC;">new</span><br />
<br />
<span style="color:#008000; font-style:italic;">#1 - creating a singleton class</span><br />
final_fantasy = NtscGame.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Final Fantasy&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">def</span> final_fantasy.<span style="color:#9900CC;">play</span><br />
&nbsp; run &nbsp;<br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
console.<span style="color:#9900CC;">play_game</span><span style="color:#006600; font-weight:bold;">&#40;</span>final_fantasy<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<br />
<span style="color:#008000; font-style:italic;">#2 - adding methods opening the singleton class directly</span><br />
winning_eleven = NtscGame.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Winning Eleven&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> winning_eleven<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> play<br />
&nbsp; &nbsp; run<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
console.<span style="color:#9900CC;">play_game</span><span style="color:#006600; font-weight:bold;">&#40;</span>winning_eleven<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<br />
<span style="color:#008000; font-style:italic;">#3 - adding methods from a module</span><br />
thunderforce = NtscGame.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Thunderforce&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">module</span> Foo<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> play<br />
&nbsp; &nbsp; run<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
thunderforce.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>Foo<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
console.<span style="color:#9900CC;">play_game</span><span style="color:#006600; font-weight:bold;">&#40;</span>thunderforce<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<br />
<span style="color:#008000; font-style:italic;">#4 - adding methods inside an instance_eval call </span><br />
dragons_lair = NtscGame.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Dragons Lair&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
dragons_lair.<span style="color:#9900CC;">instance_eval</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span>EOT<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> play<br />
&nbsp; &nbsp; run<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
EOT<br />
<br />
console.<span style="color:#9900CC;">play_game</span><span style="color:#006600; font-weight:bold;">&#40;</span>dragons_lair<span style="color:#006600; font-weight:bold;">&#41;</span></div></td></tr></tbody></table></div></p>

	<p>All implementations will provide the same type of output:<br />
I am the <span class="caps">NTSC</span> version of Final Fantasy and I am running!<br />
I am the <span class="caps">NTSC</span> version of Winning Eleven and I am running!<br />
I am the <span class="caps">NTSC</span> version of Thunderforce and I am running!<br />
I am the <span class="caps">NTSC</span> version of Dragons Lair and I am running!</p>


	<p>In this post we have therefore seen the usefulness of this design patterns and how Ruby allows the developer to &#8220;leave&#8221; the classic implementation suggested in order to better take advantage of the potentiality of the language.</p>

	<p>Source code <a href="http://github.com/devinterface/design_patterns_in_ruby" title="">here</a></p>

	<p>Previous posts from this series:<br />
<a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="">Design Patterns in Ruby: Introduction</a><br />
<a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="">Design Patterns in Ruby: Abstract Factory</a><!-- Social Bookmarking Reloaded BEGIN --><div class="social_bookmark"><em>Share on:</em><br />
<a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/&#038;title=Design+Patterns+in+Ruby%3A+Adapter" title="Add 'Design Patterns in Ruby: Adapter' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'Design Patterns in Ruby: Adapter' to Del.icio.us" alt="Add 'Design Patterns in Ruby: Adapter' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/&#038;title=Design+Patterns+in+Ruby%3A+Adapter" title="Add 'Design Patterns in Ruby: Adapter' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Design Patterns in Ruby: Adapter' to digg" alt="Add 'Design Patterns in Ruby: Adapter' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/&#038;title=Design+Patterns+in+Ruby%3A+Adapter" title="Add 'Design Patterns in Ruby: Adapter' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Design Patterns in Ruby: Adapter' to reddit" alt="Add 'Design Patterns in Ruby: Adapter' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="Add 'Design Patterns in Ruby: Adapter' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Design Patterns in Ruby: Adapter' to Technorati" alt="Add 'Design Patterns in Ruby: Adapter' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/&#038;title=Design+Patterns+in+Ruby%3A+Adapter" title="Add 'Design Patterns in Ruby: Adapter' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Design Patterns in Ruby: Adapter' to Stumble Upon" alt="Add 'Design Patterns in Ruby: Adapter' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/&#038;title=Design+Patterns+in+Ruby%3A+Adapter" title="Add 'Design Patterns in Ruby: Adapter' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'Design Patterns in Ruby: Adapter' to Google Bookmarks" alt="Add 'Design Patterns in Ruby: Adapter' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="Add 'Design Patterns in Ruby: Adapter' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'Design Patterns in Ruby: Adapter' to Rojo" alt="Add 'Design Patterns in Ruby: Adapter' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/&#038;title=Design+Patterns+in+Ruby%3A+Adapter" title="Add 'Design Patterns in Ruby: Adapter' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'Design Patterns in Ruby: Adapter' to OKnotizie" alt="Add 'Design Patterns in Ruby: Adapter' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=Design+Patterns+in+Ruby%3A+Adapter&#038;url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="Add 'Design Patterns in Ruby: Adapter' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'Design Patterns in Ruby: Adapter' to Diggita" alt="Add 'Design Patterns in Ruby: Adapter' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/&#038;t=Design+Patterns+in+Ruby%3A+Adapter" title="Add 'Design Patterns in Ruby: Adapter' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Design Patterns in Ruby: Adapter' to FaceBook" alt="Add 'Design Patterns in Ruby: Adapter' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/&#038;title=Design+Patterns+in+Ruby%3A+Adapter" title="Add 'Design Patterns in Ruby: Adapter' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'Design Patterns in Ruby: Adapter' to Upnews" alt="Add 'Design Patterns in Ruby: Adapter' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="Add 'Design Patterns in Ruby: Adapter' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'Design Patterns in Ruby: Adapter' to Twitter" alt="Add 'Design Patterns in Ruby: Adapter' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&#038;title=Design+Patterns+in+Ruby%3A+Adapter&#038;url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="Add 'Design Patterns in Ruby: Adapter' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'Design Patterns in Ruby: Adapter' to Technotizie" alt="Add 'Design Patterns in Ruby: Adapter' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=Design+Patterns+in+Ruby%3A+Adapter&#038;url=http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="Add 'Design Patterns in Ruby: Adapter' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'Design Patterns in Ruby: Adapter' to FriendFeed" alt="Add 'Design Patterns in Ruby: Adapter' to FriendFeed" /></a></div><br />
<!-- Social Bookmarking Reloaded END --></p>
 <h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/" title="<!--:en-->Design Patterns in Ruby: Chain of Responsibility<!--:--><!--:it-->Design Patterns in Ruby: Chain of Responsibility<!--:-->"><!--:en-->Design Patterns in Ruby: Chain of Responsibility<!--:--><!--:it-->Design Patterns in Ruby: Chain of Responsibility<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="<!--:en-->Design Patterns in Ruby: Abstract Factory<!--:--><!--:it-->Design Patterns in Ruby: Abstract Factory<!--:-->"><!--:en-->Design Patterns in Ruby: Abstract Factory<!--:--><!--:it-->Design Patterns in Ruby: Abstract Factory<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="<!--:en-->Design Patterns in Ruby: Introduction <!--:--><!--:it-->Design Patterns in Ruby: Introduzione<!--:-->"><!--:en-->Design Patterns in Ruby: Introduction <!--:--><!--:it-->Design Patterns in Ruby: Introduzione<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/05/how-to-model-a-custom-search-form-in-rails/" title="<!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:-->"><!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/02/create-zip-files-on-the-fly/" title="<!--:en-->Create zip files on the fly with ruby<!--:--><!--:it-->Creare un file zip al volo con ruby<!--:-->"><!--:en-->Create zip files on the fly with ruby<!--:--><!--:it-->Creare un file zip al volo con ruby<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/md8Wrew5Uk4TkzMKCPTSNN5B_SM/0/da"><img src="http://feedads.g.doubleclick.net/~a/md8Wrew5Uk4TkzMKCPTSNN5B_SM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/md8Wrew5Uk4TkzMKCPTSNN5B_SM/1/da"><img src="http://feedads.g.doubleclick.net/~a/md8Wrew5Uk4TkzMKCPTSNN5B_SM/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=Cgj1l10Rkgk:CqGsmnsWXWI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=Cgj1l10Rkgk:CqGsmnsWXWI:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=Cgj1l10Rkgk:CqGsmnsWXWI:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=Cgj1l10Rkgk:CqGsmnsWXWI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=Cgj1l10Rkgk:CqGsmnsWXWI:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/Cgj1l10Rkgk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/</feedburner:origLink></item>
		<item>
		<title>Rails Best Practices 2: Move logic to model</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/CSTNMiguzqM/</link>
		<comments>http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 12:43:29 +0000</pubDate>
		<dc:creator>Claudio</dc:creator>
				<category><![CDATA[Metodologies]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[model callback]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[virtual attributes]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=738</guid>
		<description><![CDATA[Today we continue our analysis of the Rails Best Practices.
In the previous post we saw Named Scope, Model Association and
Following the same direction, in today&#8217;s post we&#8217;ll examine the use of Callback Model and Virtual Attribute.
1. Virtual Attribute
Suppose we have a customers list table defined as follows
123456create_table &#34;clients&#34;, :force =&#62; true do &#124;t&#124; &#160;
&#160; t.string [...]]]></description>
			<content:encoded><![CDATA[<p>Today we continue our analysis of the Rails Best Practices.<br />
In the <a href="http://blog.devinterface.com/2010/06/rails-best-practices-1-fat-model-skinny-controller/">previous post</a> we saw Named Scope, Model Association and<br />
Following the same direction, in today&#8217;s post we&#8217;ll examine the use of Callback Model and Virtual Attribute.</p>
<p><strong>1. Virtual Attribute</strong></p>
<p>Suppose we have a customers list table defined as follows</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">create_table <span style="color:#996600;">&quot;clients&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:force</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span> &nbsp;<br />
&nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> &nbsp; <span style="color:#996600;">&quot;first_name&quot;</span> &nbsp;<br />
&nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> &nbsp; <span style="color:#996600;">&quot;last_name&quot;</span> &nbsp;<br />
&nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> &nbsp; <span style="color:#996600;">&quot;street&quot;</span> &nbsp;<br />
&nbsp; t.<span style="color:#CC0066; font-weight:bold;">string</span> &nbsp; <span style="color:#996600;">&quot;city&quot;</span> &nbsp;<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Suppose we have a customers list table defined as follows but we want to define an input mask where street and city are grouped into one field called &#8220;address&#8221;.<br />
The form with the  &#8220;address&#8221; field will be defined as follows:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;h1&gt;Create Client&lt;/h1&gt; &nbsp;<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@client</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &lt;ol class=&quot;formList&quot;&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;li&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:first_name</span>, <span style="color:#996600;">'First Name'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:first_name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;li&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:last_name</span>, <span style="color:#996600;">'Last Name'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:last_name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;/li&gt; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;li&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:address</span>, <span style="color:#996600;">'Address'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= text_field_tag <span style="color:#ff3333; font-weight:bold;">:address</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;/li&gt; &nbsp; &nbsp;<br />
&nbsp; &nbsp;&lt;/ol&gt; &nbsp;<br />
&nbsp;<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></td></tr></tbody></table></div>
<p>Now in the create method we&#8217;ll have something like:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> ClientsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@client</span> = Client.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:client</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@client</span>.<span style="color:#9900CC;">street</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:address</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span>, <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@client</span>.<span style="color:#9900CC;">city</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:address</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span>, <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">last</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@client</span>.<span style="color:#9900CC;">save</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
...<br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>We can now improve this method by defining the address field as a virtual attribute of the Client model.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Client <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> address<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span>street, city<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> address=<span style="color:#006600; font-weight:bold;">&#40;</span>addr<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">split</span> = addr.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span>, <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">street</span> = <span style="color:#CC0066; font-weight:bold;">split</span>.<span style="color:#9900CC;">first</span><br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">city</span> = <span style="color:#CC0066; font-weight:bold;">split</span>.<span style="color:#9900CC;">last</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>In the form we can now define the address field as <em> f.field_tag </em> instead of as <em> text_field_tag </em>:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;h1&gt;Create Client&lt;/h1&gt; &nbsp;<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@client</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &lt;ol class=&quot;formList&quot;&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;li&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:first_name</span>, <span style="color:#996600;">'First Name'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:first_name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;li&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:last_name</span>, <span style="color:#996600;">'Last Name'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:last_name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;/li&gt; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;li&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:address</span>, <span style="color:#996600;">'Address'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:address</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &lt;/li&gt; &nbsp; &nbsp;<br />
&nbsp; &nbsp;&lt;/ol&gt; &nbsp;<br />
&nbsp;<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></td></tr></tbody></table></div>
<p>Finally, the controller will become simpler:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> ClientsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@client</span> = Client.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:client</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@client</span>.<span style="color:#9900CC;">save</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
...<br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p><strong>2. Model Callback</strong><br />
Now we see a method, called Model Callback that allows us, as with the virtual attribute just seen, to simplify the form and to move the controller&#8217;s logic inside the model.</p>
<p>Suppose we have to implement a feature that automatically associates a set of tags to a post.<br />
This feature, called calculate_tags return a list of tags based on the most frequent words contained in the post.<br />
We don&#8217;t see the code of this function and instead see how and when to invoke the automatic generation of tags.</p>
<p>A first implementation can be as follows. Given the following form:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@post</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:content</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= check_box_tag <span style="color:#996600;">'calculate_tags'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></td></tr></tbody></table></div>
<p>the corresponding create method will be:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> PostController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:post</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:calculate_tags</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'1'</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">tags</span> = TagGenerator.<span style="color:#9900CC;">generate</span><span style="color:#006600; font-weight:bold;">&#40;</span>@post.<span style="color:#9900CC;">content</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">tags</span> = <span style="color:#996600;">&quot;&quot;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">save</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Editing the Post model,  let&#8217;s see how to improve the code we&#8217;ve just wrote.<br />
First we introduce in our model an attribute, &#8220;calculate_tags&#8221;, and a filter &#8220;generate_tags&#8221;, of type before_save</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Post <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><br />
&nbsp; attr_accessor <span style="color:#ff3333; font-weight:bold;">:calculate_tags</span><br />
&nbsp; before_save <span style="color:#ff3333; font-weight:bold;">:generate_tags</span><br />
&nbsp; <br />
&nbsp; private<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> generate_tags<br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">unless</span> calculate_tags == <span style="color:#996600;">'1'</span><br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">tags</span> = TagGenerator.<span style="color:#9900CC;">generate</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">content</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Returning to the form we can now redefine the field calculate_tags as f.check_box</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@post</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:content</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">check_box</span> <span style="color:#996600;">'calculate_tags'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></td></tr></tbody></table></div>
<p>Finally, our create method in the controller will return to its simplest form.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;<span style="color:#9966CC; font-weight:bold;">class</span> PostController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:post</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">save</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p><!-- Social Bookmarking Reloaded BEGIN -->
<div class="social_bookmark"><em>Share on:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/&amp;title=Rails+Best+Practices+2%3A+Move+logic+to+model" title="Add 'Rails Best Practices 2: Move logic to model' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'Rails Best Practices 2: Move logic to model' to Del.icio.us" alt="Add 'Rails Best Practices 2: Move logic to model' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/&amp;title=Rails+Best+Practices+2%3A+Move+logic+to+model" title="Add 'Rails Best Practices 2: Move logic to model' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Rails Best Practices 2: Move logic to model' to digg" alt="Add 'Rails Best Practices 2: Move logic to model' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/&amp;title=Rails+Best+Practices+2%3A+Move+logic+to+model" title="Add 'Rails Best Practices 2: Move logic to model' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Rails Best Practices 2: Move logic to model' to reddit" alt="Add 'Rails Best Practices 2: Move logic to model' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/" title="Add 'Rails Best Practices 2: Move logic to model' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Rails Best Practices 2: Move logic to model' to Technorati" alt="Add 'Rails Best Practices 2: Move logic to model' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/&amp;title=Rails+Best+Practices+2%3A+Move+logic+to+model" title="Add 'Rails Best Practices 2: Move logic to model' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Rails Best Practices 2: Move logic to model' to Stumble Upon" alt="Add 'Rails Best Practices 2: Move logic to model' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/&amp;title=Rails+Best+Practices+2%3A+Move+logic+to+model" title="Add 'Rails Best Practices 2: Move logic to model' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'Rails Best Practices 2: Move logic to model' to Google Bookmarks" alt="Add 'Rails Best Practices 2: Move logic to model' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/" title="Add 'Rails Best Practices 2: Move logic to model' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'Rails Best Practices 2: Move logic to model' to Rojo" alt="Add 'Rails Best Practices 2: Move logic to model' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/&amp;title=Rails+Best+Practices+2%3A+Move+logic+to+model" title="Add 'Rails Best Practices 2: Move logic to model' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'Rails Best Practices 2: Move logic to model' to OKnotizie" alt="Add 'Rails Best Practices 2: Move logic to model' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=Rails+Best+Practices+2%3A+Move+logic+to+model&amp;url=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/" title="Add 'Rails Best Practices 2: Move logic to model' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'Rails Best Practices 2: Move logic to model' to Diggita" alt="Add 'Rails Best Practices 2: Move logic to model' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/&amp;t=Rails+Best+Practices+2%3A+Move+logic+to+model" title="Add 'Rails Best Practices 2: Move logic to model' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Rails Best Practices 2: Move logic to model' to FaceBook" alt="Add 'Rails Best Practices 2: Move logic to model' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/&amp;title=Rails+Best+Practices+2%3A+Move+logic+to+model" title="Add 'Rails Best Practices 2: Move logic to model' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'Rails Best Practices 2: Move logic to model' to Upnews" alt="Add 'Rails Best Practices 2: Move logic to model' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/" title="Add 'Rails Best Practices 2: Move logic to model' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'Rails Best Practices 2: Move logic to model' to Twitter" alt="Add 'Rails Best Practices 2: Move logic to model' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&amp;title=Rails+Best+Practices+2%3A+Move+logic+to+model&amp;url=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/" title="Add 'Rails Best Practices 2: Move logic to model' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'Rails Best Practices 2: Move logic to model' to Technotizie" alt="Add 'Rails Best Practices 2: Move logic to model' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=Rails+Best+Practices+2%3A+Move+logic+to+model&amp;url=http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/" title="Add 'Rails Best Practices 2: Move logic to model' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'Rails Best Practices 2: Move logic to model' to FriendFeed" alt="Add 'Rails Best Practices 2: Move logic to model' to FriendFeed" /></a></div>
<p><!-- Social Bookmarking Reloaded END --></p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2010/09/rails-best-practices-6-filters-and-helpers-file/" title="<!--:en-->Rails Best Practices 6: Filters and helpers file<!--:--><!--:it-->Rails Best Practices 6: Filtri e file helper<!--:-->"><!--:en-->Rails Best Practices 6: Filters and helpers file<!--:--><!--:it-->Rails Best Practices 6: Filtri e file helper<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/08/rails-best-practices-4-put-method-in-the-right-model/" title="<!--:en-->Rails Best Practices 4: Put method in the right model<!--:--><!--:it-->Rails Best Practices 4: Scrivere i metodi nel model corretto<!--:-->"><!--:en-->Rails Best Practices 4: Put method in the right model<!--:--><!--:it-->Rails Best Practices 4: Scrivere i metodi nel model corretto<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/07/rails-best-practices-3-increase-controllers-readability/" title="<!--:en-->Rails Best Practices 3: Increase Controller&#8217;s readability<!--:--><!--:it-->Rails Best Practices 3: Aumentare la leggibilità dei controller<!--:-->"><!--:en-->Rails Best Practices 3: Increase Controller&#8217;s readability<!--:--><!--:it-->Rails Best Practices 3: Aumentare la leggibilità dei controller<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-1-fat-model-skinny-controller/" title="<!--:en-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:--><!--:it-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:-->"><!--:en-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:--><!--:it-->Rails Best Practices 1: Fat Model – Skinny Controller<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/rails-best-practices-introduction/" title="<!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:-->"><!--:en-->Rails Best Practices: introduction<!--:--><!--:it-->Rails Best Practices: introduzione<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/V_ghkjOGt8XbvNU5RpAC8tLELy8/0/da"><img src="http://feedads.g.doubleclick.net/~a/V_ghkjOGt8XbvNU5RpAC8tLELy8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/V_ghkjOGt8XbvNU5RpAC8tLELy8/1/da"><img src="http://feedads.g.doubleclick.net/~a/V_ghkjOGt8XbvNU5RpAC8tLELy8/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=CSTNMiguzqM:htNNG38DBq4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=CSTNMiguzqM:htNNG38DBq4:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=CSTNMiguzqM:htNNG38DBq4:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=CSTNMiguzqM:htNNG38DBq4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=CSTNMiguzqM:htNNG38DBq4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/CSTNMiguzqM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/06/rails-best-practices-2-move-logic-to-model/</feedburner:origLink></item>
		<item>
		<title>Design Patterns in Ruby: Abstract Factory</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/L2HQorVA-j4/</link>
		<comments>http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 12:18:53 +0000</pubDate>
		<dc:creator>Stefano</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Metodologies]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=723</guid>
		<description><![CDATA[	An abstract Factory provides a common interface for creating families of related objects together.
The client object does not bother to build objects directly, but it calls the methods provided by this common interface.

	Below is showed one possible implementation of an abstract Factory and its concrete Factories that implement it.

	Suppose we have two categories of games [...]]]></description>
			<content:encoded><![CDATA[	<p>An abstract Factory provides a common interface for creating families of related objects together.<br />
The client object does not bother to build objects directly, but it calls the methods provided by this common interface.</p>

	<p>Below is showed one possible implementation of an abstract Factory and its concrete Factories that implement it.</p>

	<p>Suppose we have two categories of games as a model classes:</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#models.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">class</span> Game<br />
&nbsp; attr_accessor <span style="color:#ff3333; font-weight:bold;">:title</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>title<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@title</span> = title<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> Rpg <span style="color:#006600; font-weight:bold;">&lt;</span> Game<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> description<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I am a RPG named #{@title}&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> Arcade <span style="color:#006600; font-weight:bold;">&lt;</span> Game<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> description<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I am an Arcade named #{@title}&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div></p>

	<p>How we can see, both models derive from a common superclass Game.</p>

	<p>Let&#8217;s define the Factories delegate to build these objects:</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#factories.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">module</span> MyAbstractGameFactory<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<span style="color:#006600; font-weight:bold;">&#40;</span>title<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">NotImplementedError</span>, <span style="color:#996600;">&quot;You should implement this method&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> RpgFactory<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> MyAbstractGameFactory<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<span style="color:#006600; font-weight:bold;">&#40;</span>title<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; Rpg.<span style="color:#9900CC;">new</span> title<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> ArcadeFactory<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> MyAbstractGameFactory<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<span style="color:#006600; font-weight:bold;">&#40;</span>title<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; Arcade.<span style="color:#9900CC;">new</span> title<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div></p>

	<p>Note that we have defined the abstract factory (MyAbstractGameFactory) as a module: it defines the abstract method that must be implemented by the class that includes it.<br />
RpgFactory and ArcadeFactory represent the two concrete factories responsible to build, respectively, Arcade and <span class="caps">RPG</span> games.</p>

	<p>The code of a GameStore, that can provide games basing on the needs of the customer, will be defined as follows:</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> GameStore<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>number_of_games, game_type<span style="color:#006600; font-weight:bold;">&#41;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> game_type == <span style="color:#ff3333; font-weight:bold;">:rpg</span><br />
&nbsp; &nbsp; &nbsp; title = <span style="color:#996600;">'Final Fantasy'</span><br />
&nbsp; &nbsp; &nbsp; game_factory = RpgFactory.<span style="color:#9900CC;">new</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">elsif</span> game_type== <span style="color:#ff3333; font-weight:bold;">:arcade</span><br />
&nbsp; &nbsp; &nbsp; title = <span style="color:#996600;">'Double Dragon'</span><br />
&nbsp; &nbsp; &nbsp; game_factory = ArcadeFactory.<span style="color:#9900CC;">new</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@games</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; number_of_games.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@games</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> game_factory.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{title} #{i+1}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> <br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> show_games<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@games</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>game<span style="color:#006600; font-weight:bold;">|</span> game.<span style="color:#9900CC;">description</span><span style="color:#006600; font-weight:bold;">&#125;</span> <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div></p>

	<p>At this point, launching the following file main.rb</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#main.rb</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'models.rb'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'factories.rb'</span><br />
game_store = GameStore.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span>, <span style="color:#ff3333; font-weight:bold;">:rpg</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
game_store.<span style="color:#9900CC;">show_games</span><br />
game_store = GameStore.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span>, <span style="color:#ff3333; font-weight:bold;">:arcade</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
game_store.<span style="color:#9900CC;">show_games</span></div></td></tr></tbody></table></div></p>

	<p>we&#8217;ll get the following output in console:</p>

	<p>I am a <span class="caps">RPG</span> named Final Fantasy 1<br />
I am a <span class="caps">RPG</span> named Final Fantasy 2<br />
I am an Arcade named Double Dragon 1<br />
I am an Arcade named Double Dragon 2<br />
I am an Arcade named Double Dragon 3<br />
I am an Arcade named Double Dragon 4<br />
I am an Arcade named Double Dragon 5</p>

	<p>At this point we can optimize our Factories in order to take advantage of the potential offered by Ruby:</p>

	<p><div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:450px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;">#factories2.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">class</span> GameFactory<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> MyAbstractGameFactory<br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>game_class<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@game_class</span> = game_class<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<span style="color:#006600; font-weight:bold;">&#40;</span>title<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@game_class</span>.<span style="color:#9900CC;">new</span> title<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span> <br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> GameStore<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>number_of_games, game_type<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; c = <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span>game_type.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">capitalize</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; game_factory = GameFactory.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> game_type == <span style="color:#ff3333; font-weight:bold;">:rpg</span><br />
&nbsp; &nbsp; &nbsp; title = <span style="color:#996600;">'Final Fantasy'</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">elsif</span> game_type == <span style="color:#ff3333; font-weight:bold;">:arcade</span><br />
&nbsp; &nbsp; &nbsp; title = <span style="color:#996600;">'Double Dragon'</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@games</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; number_of_games.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@games</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> game_factory.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{title} #{i}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> <br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> show_games<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@games</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>game<span style="color:#006600; font-weight:bold;">|</span> game.<span style="color:#9900CC;">description</span><span style="color:#006600; font-weight:bold;">&#125;</span> <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div></p>

	<p>As we can see, now a single concrete factory GameFactory continues to build Arcade and <span class="caps">RPG</span> basing on the need of the moment.<br />
This will allow a system to be independent from the implementation of concrete objects and that the client, through the interface, to use the different product families.<br />
Note that in both examples the definition of MyAbstractGameFactory was made only for educational purposes and was used to &#8220;simulate&#8221; in Ruby an abstract method of an abstract class.</p>

	<p>The output generated by invoking this new GameStore is the same as seen in the previous example.</p>

	<p>All code available in this repository on GitHub: <a href="http://github.com/devinterface/design_patterns_in_ruby" title="">design_patterns_in_ruby</a></p>


	<p>Source code <a href="http://github.com/devinterface/design_patterns_in_ruby" title="">here</a></p>

	<p><strong>Previous posts from this serie:</strong><br />
<a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="">Design Patterns in Ruby: Introduction</a><!-- Social Bookmarking Reloaded BEGIN --><div class="social_bookmark"><em>Share on:</em><br />
<a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/&#038;title=Design+Patterns+in+Ruby%3A+Abstract+Factory" title="Add 'Design Patterns in Ruby: Abstract Factory' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to Del.icio.us" alt="Add 'Design Patterns in Ruby: Abstract Factory' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/&#038;title=Design+Patterns+in+Ruby%3A+Abstract+Factory" title="Add 'Design Patterns in Ruby: Abstract Factory' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to digg" alt="Add 'Design Patterns in Ruby: Abstract Factory' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/&#038;title=Design+Patterns+in+Ruby%3A+Abstract+Factory" title="Add 'Design Patterns in Ruby: Abstract Factory' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to reddit" alt="Add 'Design Patterns in Ruby: Abstract Factory' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="Add 'Design Patterns in Ruby: Abstract Factory' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to Technorati" alt="Add 'Design Patterns in Ruby: Abstract Factory' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/&#038;title=Design+Patterns+in+Ruby%3A+Abstract+Factory" title="Add 'Design Patterns in Ruby: Abstract Factory' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to Stumble Upon" alt="Add 'Design Patterns in Ruby: Abstract Factory' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/&#038;title=Design+Patterns+in+Ruby%3A+Abstract+Factory" title="Add 'Design Patterns in Ruby: Abstract Factory' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to Google Bookmarks" alt="Add 'Design Patterns in Ruby: Abstract Factory' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="Add 'Design Patterns in Ruby: Abstract Factory' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to Rojo" alt="Add 'Design Patterns in Ruby: Abstract Factory' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/&#038;title=Design+Patterns+in+Ruby%3A+Abstract+Factory" title="Add 'Design Patterns in Ruby: Abstract Factory' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'Design Patterns in Ruby: Abstract Factory' to OKnotizie" alt="Add 'Design Patterns in Ruby: Abstract Factory' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=Design+Patterns+in+Ruby%3A+Abstract+Factory&#038;url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="Add 'Design Patterns in Ruby: Abstract Factory' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to Diggita" alt="Add 'Design Patterns in Ruby: Abstract Factory' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/&#038;t=Design+Patterns+in+Ruby%3A+Abstract+Factory" title="Add 'Design Patterns in Ruby: Abstract Factory' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to FaceBook" alt="Add 'Design Patterns in Ruby: Abstract Factory' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/&#038;title=Design+Patterns+in+Ruby%3A+Abstract+Factory" title="Add 'Design Patterns in Ruby: Abstract Factory' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to Upnews" alt="Add 'Design Patterns in Ruby: Abstract Factory' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="Add 'Design Patterns in Ruby: Abstract Factory' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to Twitter" alt="Add 'Design Patterns in Ruby: Abstract Factory' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&#038;title=Design+Patterns+in+Ruby%3A+Abstract+Factory&#038;url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="Add 'Design Patterns in Ruby: Abstract Factory' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to Technotizie" alt="Add 'Design Patterns in Ruby: Abstract Factory' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=Design+Patterns+in+Ruby%3A+Abstract+Factory&#038;url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="Add 'Design Patterns in Ruby: Abstract Factory' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'Design Patterns in Ruby: Abstract Factory' to FriendFeed" alt="Add 'Design Patterns in Ruby: Abstract Factory' to FriendFeed" /></a></div><br />
<!-- Social Bookmarking Reloaded END --></p>
 <h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/" title="<!--:en-->Design Patterns in Ruby: Chain of Responsibility<!--:--><!--:it-->Design Patterns in Ruby: Chain of Responsibility<!--:-->"><!--:en-->Design Patterns in Ruby: Chain of Responsibility<!--:--><!--:it-->Design Patterns in Ruby: Chain of Responsibility<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="<!--:en-->Design Patterns in Ruby: Adapter<!--:--><!--:it-->Design Patterns in Ruby: Adapter<!--:-->"><!--:en-->Design Patterns in Ruby: Adapter<!--:--><!--:it-->Design Patterns in Ruby: Adapter<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="<!--:en-->Design Patterns in Ruby: Introduction <!--:--><!--:it-->Design Patterns in Ruby: Introduzione<!--:-->"><!--:en-->Design Patterns in Ruby: Introduction <!--:--><!--:it-->Design Patterns in Ruby: Introduzione<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/05/how-to-model-a-custom-search-form-in-rails/" title="<!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:-->"><!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/02/create-zip-files-on-the-fly/" title="<!--:en-->Create zip files on the fly with ruby<!--:--><!--:it-->Creare un file zip al volo con ruby<!--:-->"><!--:en-->Create zip files on the fly with ruby<!--:--><!--:it-->Creare un file zip al volo con ruby<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/6BY3wNsNAu4UH5xLrqQmnq6a9JE/0/da"><img src="http://feedads.g.doubleclick.net/~a/6BY3wNsNAu4UH5xLrqQmnq6a9JE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/6BY3wNsNAu4UH5xLrqQmnq6a9JE/1/da"><img src="http://feedads.g.doubleclick.net/~a/6BY3wNsNAu4UH5xLrqQmnq6a9JE/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=L2HQorVA-j4:SyPW1jU8LfE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=L2HQorVA-j4:SyPW1jU8LfE:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=L2HQorVA-j4:SyPW1jU8LfE:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=L2HQorVA-j4:SyPW1jU8LfE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=L2HQorVA-j4:SyPW1jU8LfE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/L2HQorVA-j4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/</feedburner:origLink></item>
		<item>
		<title>Design Patterns in Ruby: Introduction</title>
		<link>http://feedproxy.google.com/~r/devinterfaceblog/~3/gX1XVLLXeyI/</link>
		<comments>http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 09:30:41 +0000</pubDate>
		<dc:creator>Stefano</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Metodologies]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.devinterface.com/?p=700</guid>
		<description><![CDATA[	Hello everyone.

	With this post I would like to start a serie of articles concerning the description of the most common design patterns and how these can be applied with the Ruby language.

	In the world of software engineering you don&#8217;t need to reinvent the wheel to face daily problems: these problems are often very common and [...]]]></description>
			<content:encoded><![CDATA[	<p>Hello everyone.</p>

	<p>With this post I would like to start a serie of articles concerning the description of the most common design patterns and how these can be applied with the Ruby language.</p>

	<p>In the world of software engineering you don&#8217;t need to reinvent the wheel to face daily problems: these problems are often very common and in most cases they require a similar resolution.</p>

	<p>Christopher Alexander says that &#8220;<em>each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such way that you can use this solution a million times over, without ever doing it the same way twice</em>&#8220;.</p>

	<p>In 1995, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, starting from the work of Christopher Alexander and Kent Beck, have published &#8220;Design Patterns: Elements of Reusable Object-Oriented Software.&#8221; With this book, the four authors (from then knowned as <a href="http://en.wikipedia.org/wiki/Design_Patterns" title="">GoF</a>, Gang of Four) have proposed solutions to the 23 most common and recurring problems in software engineering.</p>

	<p>The GoF have analyzed and divided the 23 patterns into 3 categories:</p>

	<p><strong>Creational patterns</strong></p>

	<ul>
		<li>Abstract Factory</li>
		<li>Builder</li>
		<li>Factory Method</li>
		<li>Prototype</li>
		<li>Singleton</li>
	</ul>

	<p><strong>Structural patterns</strong></p>
	<ul>
		<li>Adapter</li>
		<li>Bridge</li>
		<li>Composite</li>
		<li>Decorator</li>
		<li>Facade</li>
		<li>Flyweight</li>
		<li>Proxy</li>
	</ul>

	<p><strong>Behavioral patterns</strong></p>
	<ul>
		<li>Chain</li>
		<li>Command</li>
		<li>Interpreter</li>
		<li>Iterator</li>
		<li>Mediator</li>
		<li>Memento</li>
		<li>Observer</li>
		<li>State</li>
		<li>Strategy</li>
		<li>Template</li>
		<li>Visitor</li>
	</ul>

	<p>We will see during this series all the most common patterns: we&#8217;ll analyze the problem and define a solution if possible using the potential provided by the Ruby language.</p>

	<p>All showed code will be freely accessible in an out repository on GitHub at this address: <a href="http://github.com/devinterface/design_patterns_in_ruby" title="">design_patterns_in_ruby</a>. <!-- Social Bookmarking Reloaded BEGIN --><div class="social_bookmark"><em>Share on:</em><br />
<a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/&#038;title=Design+Patterns+in+Ruby%3A+Introduction" title="Add 'Design Patterns in Ruby: Introduction' to Del.icio.us"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'Design Patterns in Ruby: Introduction' to Del.icio.us" alt="Add 'Design Patterns in Ruby: Introduction' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/&#038;title=Design+Patterns+in+Ruby%3A+Introduction" title="Add 'Design Patterns in Ruby: Introduction' to digg"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Design Patterns in Ruby: Introduction' to digg" alt="Add 'Design Patterns in Ruby: Introduction' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/&#038;title=Design+Patterns+in+Ruby%3A+Introduction" title="Add 'Design Patterns in Ruby: Introduction' to reddit"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Design Patterns in Ruby: Introduction' to reddit" alt="Add 'Design Patterns in Ruby: Introduction' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="Add 'Design Patterns in Ruby: Introduction' to Technorati"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Design Patterns in Ruby: Introduction' to Technorati" alt="Add 'Design Patterns in Ruby: Introduction' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/&#038;title=Design+Patterns+in+Ruby%3A+Introduction" title="Add 'Design Patterns in Ruby: Introduction' to Stumble Upon"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Design Patterns in Ruby: Introduction' to Stumble Upon" alt="Add 'Design Patterns in Ruby: Introduction' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/&#038;title=Design+Patterns+in+Ruby%3A+Introduction" title="Add 'Design Patterns in Ruby: Introduction' to Google Bookmarks"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'Design Patterns in Ruby: Introduction' to Google Bookmarks" alt="Add 'Design Patterns in Ruby: Introduction' to Google Bookmarks" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.rojo.com/add-subscription/?resource=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="Add 'Design Patterns in Ruby: Introduction' to Rojo"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/rojo.png" title="Add 'Design Patterns in Ruby: Introduction' to Rojo" alt="Add 'Design Patterns in Ruby: Introduction' to Rojo" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://oknotizie.alice.it/post?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/&#038;title=Design+Patterns+in+Ruby%3A+Introduction" title="Add 'Design Patterns in Ruby: Introduction' to OKnotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/oknotizie.gif" title="Add 'Design Patterns in Ruby: Introduction' to OKnotizie" alt="Add 'Design Patterns in Ruby: Introduction' to OKnotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.diggita.it/submit.php?title=Design+Patterns+in+Ruby%3A+Introduction&#038;url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="Add 'Design Patterns in Ruby: Introduction' to Diggita"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/diggita.png" title="Add 'Design Patterns in Ruby: Introduction' to Diggita" alt="Add 'Design Patterns in Ruby: Introduction' to Diggita" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/&#038;t=Design+Patterns+in+Ruby%3A+Introduction" title="Add 'Design Patterns in Ruby: Introduction' to FaceBook"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Design Patterns in Ruby: Introduction' to FaceBook" alt="Add 'Design Patterns in Ruby: Introduction' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.upnews.it/submit?url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/&#038;title=Design+Patterns+in+Ruby%3A+Introduction" title="Add 'Design Patterns in Ruby: Introduction' to Upnews"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/upnews.png" title="Add 'Design Patterns in Ruby: Introduction' to Upnews" alt="Add 'Design Patterns in Ruby: Introduction' to Upnews" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="Add 'Design Patterns in Ruby: Introduction' to Twitter"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'Design Patterns in Ruby: Introduction' to Twitter" alt="Add 'Design Patterns in Ruby: Introduction' to Twitter" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technotizie.it/posta_ok?action=f2&#038;title=Design+Patterns+in+Ruby%3A+Introduction&#038;url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="Add 'Design Patterns in Ruby: Introduction' to Technotizie"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/technotizie.png" title="Add 'Design Patterns in Ruby: Introduction' to Technotizie" alt="Add 'Design Patterns in Ruby: Introduction' to Technotizie" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://friendfeed.com/share/bookmarklet/frame#title=Design+Patterns+in+Ruby%3A+Introduction&#038;url=http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/" title="Add 'Design Patterns in Ruby: Introduction' to FriendFeed"><img src="http://blog.devinterface.com/wp-content/plugins/social-bookmarking-reloaded/friendfeed.png" title="Add 'Design Patterns in Ruby: Introduction' to FriendFeed" alt="Add 'Design Patterns in Ruby: Introduction' to FriendFeed" /></a></div><br />
<!-- Social Bookmarking Reloaded END --></p>
 <h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://blog.devinterface.com/2010/07/design-patterns-in-ruby-chain-of-responsibility/" title="<!--:en-->Design Patterns in Ruby: Chain of Responsibility<!--:--><!--:it-->Design Patterns in Ruby: Chain of Responsibility<!--:-->"><!--:en-->Design Patterns in Ruby: Chain of Responsibility<!--:--><!--:it-->Design Patterns in Ruby: Chain of Responsibility<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/07/design-patterns-in-ruby-adapter/" title="<!--:en-->Design Patterns in Ruby: Adapter<!--:--><!--:it-->Design Patterns in Ruby: Adapter<!--:-->"><!--:en-->Design Patterns in Ruby: Adapter<!--:--><!--:it-->Design Patterns in Ruby: Adapter<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/06/design-patterns-in-ruby-abstract-factory/" title="<!--:en-->Design Patterns in Ruby: Abstract Factory<!--:--><!--:it-->Design Patterns in Ruby: Abstract Factory<!--:-->"><!--:en-->Design Patterns in Ruby: Abstract Factory<!--:--><!--:it-->Design Patterns in Ruby: Abstract Factory<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/05/how-to-model-a-custom-search-form-in-rails/" title="<!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:-->"><!--:en-->How to model a custom search form in Rails<!--:--><!--:it-->Come modellare una form di ricerca usando Rails<!--:--></a></li><li><a href="http://blog.devinterface.com/2010/02/create-zip-files-on-the-fly/" title="<!--:en-->Create zip files on the fly with ruby<!--:--><!--:it-->Creare un file zip al volo con ruby<!--:-->"><!--:en-->Create zip files on the fly with ruby<!--:--><!--:it-->Creare un file zip al volo con ruby<!--:--></a></li></ul>
<p><a href="http://feedads.g.doubleclick.net/~a/x6PcGOQ_MG278wXuqmCjWdgnjYE/0/da"><img src="http://feedads.g.doubleclick.net/~a/x6PcGOQ_MG278wXuqmCjWdgnjYE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/x6PcGOQ_MG278wXuqmCjWdgnjYE/1/da"><img src="http://feedads.g.doubleclick.net/~a/x6PcGOQ_MG278wXuqmCjWdgnjYE/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=gX1XVLLXeyI:EbNbqi7PIwI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=gX1XVLLXeyI:EbNbqi7PIwI:-BTjWOF_DHI"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=gX1XVLLXeyI:EbNbqi7PIwI:-BTjWOF_DHI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/devinterfaceblog?a=gX1XVLLXeyI:EbNbqi7PIwI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/devinterfaceblog?i=gX1XVLLXeyI:EbNbqi7PIwI:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/devinterfaceblog/~4/gX1XVLLXeyI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://blog.devinterface.com/2010/06/design-patterns-in-ruby-introduction/</feedburner:origLink></item>
	</channel>
</rss>
