<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><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" version="2.0">

<channel>
	<title>code tunes</title>
	
	<link>http://codetunes.com</link>
	<description>Web applications, software engineering, Ruby on Rails, Cake PHP, JavaScript, etc.</description>
	<pubDate>Tue, 04 Nov 2008 19:13:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/CodeTunes" type="application/rss+xml" /><item>
		<title>UploadPack - easy and flexible way to upload files with CakePHP</title>
		<link>http://codetunes.com/2008/11/04/uploadpack-easy-and-flexible-way-to-upload-files-with-cakephp/</link>
		<comments>http://codetunes.com/2008/11/04/uploadpack-easy-and-flexible-way-to-upload-files-with-cakephp/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 19:13:02 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[Plugins]]></category>

		<category><![CDATA[behavior]]></category>

		<category><![CDATA[helper]]></category>

		<category><![CDATA[plugin]]></category>

		<category><![CDATA[thumbnailing]]></category>

		<category><![CDATA[upload]]></category>

		<category><![CDATA[UploadPack]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=86</guid>
		<description><![CDATA[I have seen and worked with many scripts and plugins (not only written in PHP) that tries to deal with file-upload functionality on server side, thanks to this I think I have some view on how good file-upload plugin should work. The thing was to put it in CakePHP clothes and code it.
Here are the [...]]]></description>
			<content:encoded><![CDATA[<p>I have seen and worked with many scripts and plugins (not only written in PHP) that tries to deal with file-upload functionality on server side, thanks to this I think I have some view on how good file-upload plugin should work. The thing was to put it in CakePHP clothes and code it.</p>
<p>Here are the requirements I set:</p>
<ul>
<li>save procedure of record with attached file should be no different to usual record</li>
<li>it should be possible to do some additional processing of uploaded file at the time of saving it (for example thumbnail generation)</li>
<li>easy access to uploded file, it&#8217;s URL and alternatives (different thumnails) from view level</li>
<li>everything should require no or minimum configuration to work, but still remain flexible if the whole application needs it</li>
<li>natural integration with CakePHP framework</li>
</ul>
<p>Here&#8217;s the effect of some planning and coding - <a title="UploadPack" href="http://github.com/netguru/uploadpack">UploadPack</a>. Right now it contains:</p>
<ul>
<li>UploadBehavior - deals with saving files to disk and post-save processing</li>
<li>UploadHelper - provides nice access to files (URLs, thumbnails) from view level</li>
</ul>
<p>Everything works quite gracefully, I think. You only need to add one field to model&#8217;s database table (which will hold file name) and attach behavior to model. The rest is done automatically. Everything is documented on <a title="UploadPack" href="http://github.com/netguru/uploadpack">repository&#8217;s page</a>.</p>
<p>It&#8217;s still an early version - 0.1, but there is much it can do right now. The work is underway to provide new features. Take a look at it.</p>
<p>If you have any views or ideas, please leave a comment. I&#8217;d appreciate it.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip/" title="Custom thumbnail generation with Paperclip">Custom thumbnail generation with Paperclip</a></li>
<li><a href="http://codetunes.com/2008/09/15/namedscopebehavior-upgrade/" title="NamedScopeBehavior upgrade">NamedScopeBehavior upgrade</a></li>
<li><a href="http://codetunes.com/2008/09/05/named-scope-in-cakephp/" title="Named scope in CakePHP">Named scope in CakePHP</a></li>
<li><a href="http://codetunes.com/2008/10/26/uploading-to-multiple-s3-buckets-with-paperclip-and-rails/" title="Uploading to multiple S3 buckets with Paperclip and Rails">Uploading to multiple S3 buckets with Paperclip and Rails</a></li>
<li><a href="http://codetunes.com/2008/08/21/convenient-controllers-callback-methods-with-cakephp/" title="Convenient controller&#8217;s callback methods with CakePHP">Convenient controller&#8217;s callback methods with CakePHP</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F11%2F04%2Fuploadpack-easy-and-flexible-way-to-upload-files-with-cakephp%2F';
  addthis_title  = 'UploadPack+-+easy+and+flexible+way+to+upload+files+with+CakePHP';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=afbVtp"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=afbVtp" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/11/04/uploadpack-easy-and-flexible-way-to-upload-files-with-cakephp/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Uploading to multiple S3 buckets with Paperclip and Rails</title>
		<link>http://codetunes.com/2008/10/26/uploading-to-multiple-s3-buckets-with-paperclip-and-rails/</link>
		<comments>http://codetunes.com/2008/10/26/uploading-to-multiple-s3-buckets-with-paperclip-and-rails/#comments</comments>
		<pubDate>Sun, 26 Oct 2008 14:55:25 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Plugins]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[paperclip]]></category>

		<category><![CDATA[plugin]]></category>

		<category><![CDATA[S3]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=84</guid>
		<description><![CDATA[When your application uses many static files (photos for example), you should consider placing these files on different hosts to improve the speed at which they are downloaded by web browsers.
Most web browsers have a limit on how many simultaneous connections can be made to a single named host. And this limit is usually two. [...]]]></description>
			<content:encoded><![CDATA[<p>When your application uses many static files (photos for example), you should consider placing these files on different hosts to improve the speed at which they are downloaded by web browsers.</p>
<p>Most web browsers have a limit on how many simultaneous connections can be made to a single named host. And this limit is usually <em>two</em>. It means that if you let&#8217;s say display many photos on a single page, user&#8217;s browser can only download two at a time. The broadband internet connection will be no help here. The browser will not fully use it, because it will keep opening and closing connections.</p>
<p>With little server and Rails configuration you pretend to be serving your files from different hosts and trick the browser (<a title="Distributed asset hosts" href="http://chadfowler.com/2007/2/18/edge-rails-goody-distributed-asset-hosts">read more</a>). This is easy if you keep all the files on your own server, but when you use <a title="Paperclip" href="http://thoughtbot.com/projects/paperclip">Paperclip</a> to upload files to Amazon&#8217;s S3 servers it gets more trickier. In fact Paperclip doesn&#8217;t support uploading to different hosts (buckets).</p>
<p>First, notice that these addresses means the same for Amazon&#8217;s S3:</p>
<ul>
<li>http://s3.amazonaws.com/bucket_name/filename.ext</li>
<li>http://bucket_name.s3.amazonaws.com/filename.ext</li>
</ul>
<p>So all we need to do is to make Paperclip upload to different buckets and return attachment&#8217;s url of the latter type. I wrote an extension that accomplishes this and included it in <a title="PaperclipExtended" href="http://github.com/netguru/paperclip-extended">PaperclipExtended</a>. All you need to do is download both Paperclip and PaperclipExtended and change your model definition.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
    has_attached_file <span style="color:#ff3333; font-weight:bold;">:avatar</span>,
                      <span style="color:#ff3333; font-weight:bold;">:storage</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:s3</span>,
                      <span style="color:#ff3333; font-weight:bold;">:s3_credentials</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{RAILS_ROOT}/config/s3.yml&quot;</span>,
                      <span style="color:#ff3333; font-weight:bold;">:path</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;avatars/:id/:style_:extension&quot;</span>,
                      <span style="color:#ff3333; font-weight:bold;">:bucket</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> |attachment|
                        i = attachment.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">id</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">4</span>
                        <span style="color:#996600;">&quot;bucket_#{i}&quot;</span>
                      <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This will place each avatar in one of four buckets: bucket_0, bucket_1, bucket_2 or bucket_3. The exact bucket is chosen at runtime and in this case it’s based on models id.</p>
<p>Getting attachment’s url:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">avatar</span>.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:original</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; http://bucket_1.s3.amazonaws.com/avatars/1/original.jpg</span></pre></div></div>

<p>If you have a page on your website that displays many uploaded photos, placing them in different buckets should make noticeable difference to user - they will load faster.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip/" title="Custom thumbnail generation with Paperclip">Custom thumbnail generation with Paperclip</a></li>
<li><a href="http://codetunes.com/2008/11/04/uploadpack-easy-and-flexible-way-to-upload-files-with-cakephp/" title="UploadPack - easy and flexible way to upload files with CakePHP">UploadPack - easy and flexible way to upload files with CakePHP</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F10%2F26%2Fuploading-to-multiple-s3-buckets-with-paperclip-and-rails%2F';
  addthis_title  = 'Uploading+to+multiple+S3+buckets+with+Paperclip+and+Rails';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=jn8ToN"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=jn8ToN" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/10/26/uploading-to-multiple-s3-buckets-with-paperclip-and-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SwfUpload and Flash 10</title>
		<link>http://codetunes.com/2008/10/21/swfupload-and-flash-10/</link>
		<comments>http://codetunes.com/2008/10/21/swfupload-and-flash-10/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 16:23:34 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Libraries]]></category>

		<category><![CDATA[flash]]></category>

		<category><![CDATA[SwfUpload]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=71</guid>
		<description><![CDATA[
SwfUpload is a graceful combination of flash and javascript that provides a mechanism for uploading files which is far more user-friendly than standard &#60;input type=&#8221;file” /&#62;. It allows to upload many files at once and displays nice-looking progress bar.
Not everything works fine in every environment, though. Some problems are caused by the differences between some [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-72" title="flashplayer_100x100" src="http://codetunes.com/wp-content/uploads/2008/10/flashplayer_100x100.jpg" alt="" width="100" height="100" /><br />
<a title="SwfUpload" href="http://swfupload.org/">SwfUpload</a> is a graceful combination of flash and javascript that provides a mechanism for uploading files which is far more user-friendly than standard &lt;input type=&#8221;file” /&gt;. It allows to upload many files at once and displays nice-looking progress bar.</p>
<p>Not everything works fine in every environment, though. Some problems are caused by the differences between some versions of flash player or even by bugs in flash players. Many of them can be solved, <a title="Problematic SwfUpload" href="http://codetunes.com/2008/06/27/problematic-swfupload/">I wrote a post about it some time ago</a>. But now when the Flash 10 came out, we have new obstacles to overcome.</p>
<p>Flash 10 is more restrictive about security issues that it&#8217;s predecessors. Now you cannot trigger opening file-select window from outside of the flash and it affects SwfUpload which used to do it from Javascript.</p>
<p>Thankfully, developers reacted quickly and are now working on new version (2.2) of the library. Alpha version is already out there and it deals with the mentioned problem. Since it is only alpha I recommend upgrading to stable version as soon as it comes out.</p>
<p>Here are inctructions how to reconfigure your app to work with SwfUpload 2.2.</p>
<p>First, download the most recent version of the library from SVN repository:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">svn co http://swfupload.googlecode.com/svn/swfupload/trunk/core swfupload</pre></div></div>

<p>You&#8217;ll only need two files from this: swfupload/swfupload.js and swfupload/Flash/swfupload.swf. These are the new version of files you should already have in your application. Replace old ones with these.</p>
<p>The new SwfUpload creates &#8220;Select files&#8221; button on it&#8217;s own, you only need to provide a placeholder for it.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;swfuploadButtonPlaceHolder&quot;&gt;&lt;/div&gt;</pre></div></div>

<p>The last thing is to update some Javascript.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">swfu <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SWFUpload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
...
<span style="color: #660066;">button_placeholder_id</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;swfuploadButtonPlaceHolder&quot;</span><span style="color: #339933;">,</span>
button_image_url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;../images/swfupload/button.jpg&quot;</span><span style="color: #339933;">,</span>
button_width<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;216&quot;</span><span style="color: #339933;">,</span>
button_height<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;25&quot;</span>
...<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>&#8220;button_image_url&#8221; accepts a path relative to the swf file. The image it points to, should look somewhat like this:</p>
<p><img class="alignnone size-full wp-image-73" title="button" src="http://codetunes.com/wp-content/uploads/2008/10/button.jpg" alt="" width="216" height="75" /></p>
<p>One image containing three states of a button (normal, on mouse over, on mouse click). You can have text instead of image if you want:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">swfu <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SWFUpload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
...
<span style="color: #660066;">button_text</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'&lt;span class=&quot;theFont&quot;&gt;Upload&lt;/span&gt;'</span><span style="color: #339933;">,</span>
button_text_style<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;.theFont { font-size: 16; }&quot;</span>
...<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>That&#8217;s it. You can see working example I based on <a title="SwfUpload demo" href="http://g.benoit.free.fr/swfupload/simpledemo/">here</a>.</p>
<p>If you find it useful, please share.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/06/27/problematic-swfupload/" title="Problematic SwfUpload">Problematic SwfUpload</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F10%2F21%2Fswfupload-and-flash-10%2F';
  addthis_title  = 'SwfUpload+and+Flash+10';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=uGVl0H"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=uGVl0H" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/10/21/swfupload-and-flash-10/feed/</wfw:commentRss>
		</item>
		<item>
		<title>NamedScopeBehavior upgrade</title>
		<link>http://codetunes.com/2008/09/15/namedscopebehavior-upgrade/</link>
		<comments>http://codetunes.com/2008/09/15/namedscopebehavior-upgrade/#comments</comments>
		<pubDate>Sun, 14 Sep 2008 22:39:53 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[behavior]]></category>

		<category><![CDATA[named scope]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=64</guid>
		<description><![CDATA[I have just added a small functionality to my NamedScopeBehavior.
Now you can use named scopes from multiple models at once in a single find query. Assuming that given models are associated through belongsTo or hasOne association.
Quick example:

// model definitions
class User extends AppModel &#123;
  var $actsAs = array&#40;
    'NamedScope' =&#62; array&#40;
  [...]]]></description>
			<content:encoded><![CDATA[<p>I have just added a small functionality to my <a title="NamedScopeBehavior" href="http://github.com/netguru/namedscopebehavior/tree/master">NamedScopeBehavior</a>.</p>
<p>Now you can use named scopes from multiple models at once in a single find query. Assuming that given models are associated through belongsTo or hasOne association.</p>
<p>Quick example:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// model definitions</span>
<span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$actsAs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="">'NamedScope'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="">'activated'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'User.activated'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$belongsTo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="">'Group'</span>
  <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Group <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$actsAs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="">'NamedScope'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="">'admins'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'Group.name'</span> <span style="color: #339933;">=&gt;</span> <span style="">'admins'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// in controller</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="">'all'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'scope'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'User.activated'</span><span style="color: #339933;">,</span> <span style="">'Group.admins'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>New version can be found in <a title="NamedScopeBehavior" href="http://github.com/netguru/namedscopebehavior/tree/master">repository</a>.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/09/05/named-scope-in-cakephp/" title="Named scope in CakePHP">Named scope in CakePHP</a></li>
<li><a href="http://codetunes.com/2008/11/04/uploadpack-easy-and-flexible-way-to-upload-files-with-cakephp/" title="UploadPack - easy and flexible way to upload files with CakePHP">UploadPack - easy and flexible way to upload files with CakePHP</a></li>
<li><a href="http://codetunes.com/2008/08/21/convenient-controllers-callback-methods-with-cakephp/" title="Convenient controller&#8217;s callback methods with CakePHP">Convenient controller&#8217;s callback methods with CakePHP</a></li>
<li><a href="http://codetunes.com/2008/07/11/migration-to-cakephp-rc2/" title="Migration to CakePHP RC2">Migration to CakePHP RC2</a></li>
<li><a href="http://codetunes.com/2008/07/07/not-so-obvious-behavior-of-uses-in-cakephp/" title="Not so obvious behavior of $uses in CakePHP">Not so obvious behavior of $uses in CakePHP</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F09%2F15%2Fnamedscopebehavior-upgrade%2F';
  addthis_title  = 'NamedScopeBehavior+upgrade';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=NP3eYJ"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=NP3eYJ" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/09/15/namedscopebehavior-upgrade/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Named scope in CakePHP</title>
		<link>http://codetunes.com/2008/09/05/named-scope-in-cakephp/</link>
		<comments>http://codetunes.com/2008/09/05/named-scope-in-cakephp/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 12:35:11 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[behavior]]></category>

		<category><![CDATA[named scope]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=55</guid>
		<description><![CDATA[Some time ago I wrote how to improve Cake controllers&#8217; callback system basing on a solution from Ruby on Rails. Today I am going to port another cool feature of Rails to CakePHP - named scope.
Check this Rails example first if you don&#8217;t know what the named scope is:

# model definition
class User &#60; ActiveRecord::Base
  [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I wrote <a title="Convenient controller’s callback methods with CakePHP" href="http://codetunes.com/2008/08/21/convenient-controllers-callback-methods-with-cakephp/">how to improve Cake controllers&#8217; callback system</a> basing on a solution from Ruby on Rails. Today I am going to port another cool feature of Rails to CakePHP - named scope.</p>
<p>Check this Rails example first if you don&#8217;t know what the named scope is:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># model definition</span>
<span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#008000; font-style:italic;"># only activated users</span>
  named_scope <span style="color:#ff3333; font-weight:bold;">:activated</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;activated_at is not null&quot;</span> 
&nbsp;
  <span style="color:#008000; font-style:italic;"># only currently online users</span>
  named_scope <span style="color:#ff3333; font-weight:bold;">:online</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;date_add(last_activity, interval 5 minute) &lt; now()&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This is very simple example. In fact, <a title="Named scope - it's not just for conditions ya know" href="http://www.ryandaigle.com/articles/2008/8/20/named-scope-it-s-not-just-for-conditions-ya-know">named scope functionality can be used for much more than this</a>. However my Cake version I present below is limited to defining conditions.</p>
<p>So we defined some scopes and gave them names. Now we can apply them to searching.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># let's find all activated users</span>
users = User.<span style="color:#9900CC;">activated</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><span style="color:#006600; font-weight:bold;">&#41;</span> 
&nbsp;
<span style="color:#008000; font-style:italic;"># let's find all activated users who are currently online and has more than 10 points</span>
users = User.<span style="color:#9900CC;">activated</span>.<span style="color:#9900CC;">online</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>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;points &gt; 10&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>I bet you can already see advantages of such notation. Often used conditions (for example: often you want to limit search results to activated users only) are placed in named_scope definition, the others (probably more situation-specific ones) go to usual :conditions option. Improved readability, less writing, fewer chances to make mistakes.</p>
<p>Let me present you <a title="NamedScopeBehavior" href="http://github.com/netguru/namedscopebehavior/tree/master">NamedScopeBehavior</a> which will give you similat functionality in CakePHP. What it does is converting scope definitions to conditions parameter of find() method and that is done transparently in beforeFind callback method.</p>
<p>Download named_scope.php file from <a title="NamedScopeBehavior" href="http://github.com/netguru/namedscopebehavior/tree/master">repository</a> and place it it app/models/behaviors folder of your Cake app. Then attach this behavior to a model and define named scopes.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$actsAs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="">'NamedScope'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="">'activated'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'User.activated in not null'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="">'online'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'date_add(User.last_activity, interval 5 minute) &gt; now()'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then you can use it.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// in your controller</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="">'all'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'scope'</span> <span style="color: #339933;">=&gt;</span> <span style="">'activated'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="">'all'</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="">'points &gt; 10'</span><span style="color: #339933;">,</span> <span style="">'scope'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'activated'</span><span style="color: #339933;">,</span> <span style="">'online'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Pagination also works fine with scopes.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// in your controller</span>
<span style="color: #000088;">$paginate</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="">'User'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="">'order'</span> <span style="color: #339933;">=&gt;</span> <span style="">'created ASC'</span><span style="color: #339933;">,</span>
    <span style="">'limit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span>
    <span style="">'scope'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'online'</span><span style="color: #339933;">,</span> <span style="">'activated'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Happy developing!<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/09/15/namedscopebehavior-upgrade/" title="NamedScopeBehavior upgrade">NamedScopeBehavior upgrade</a></li>
<li><a href="http://codetunes.com/2008/11/04/uploadpack-easy-and-flexible-way-to-upload-files-with-cakephp/" title="UploadPack - easy and flexible way to upload files with CakePHP">UploadPack - easy and flexible way to upload files with CakePHP</a></li>
<li><a href="http://codetunes.com/2008/08/21/convenient-controllers-callback-methods-with-cakephp/" title="Convenient controller&#8217;s callback methods with CakePHP">Convenient controller&#8217;s callback methods with CakePHP</a></li>
<li><a href="http://codetunes.com/2008/07/11/migration-to-cakephp-rc2/" title="Migration to CakePHP RC2">Migration to CakePHP RC2</a></li>
<li><a href="http://codetunes.com/2008/07/07/not-so-obvious-behavior-of-uses-in-cakephp/" title="Not so obvious behavior of $uses in CakePHP">Not so obvious behavior of $uses in CakePHP</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F09%2F05%2Fnamed-scope-in-cakephp%2F';
  addthis_title  = 'Named+scope+in+CakePHP';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=l4Dfod"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=l4Dfod" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/09/05/named-scope-in-cakephp/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Convenient controller’s callback methods with CakePHP</title>
		<link>http://codetunes.com/2008/08/21/convenient-controllers-callback-methods-with-cakephp/</link>
		<comments>http://codetunes.com/2008/08/21/convenient-controllers-callback-methods-with-cakephp/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 22:50:46 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[callbacks]]></category>

		<category><![CDATA[controller]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=48</guid>
		<description><![CDATA[Controller&#8217;s callback methods (beforeFilter, beforeRender and afterFilter) are very useful, they save a lot of time, makes code more readable and DRY. You probably use them a lot. They are great. Unless they&#8217;re too big.
The process is simple.

You want some code executed before every action in your controller, so you put the code in beforeFilter [...]]]></description>
			<content:encoded><![CDATA[<p>Controller&#8217;s callback methods (beforeFilter, beforeRender and afterFilter) are very useful, they save a lot of time, makes code more readable and <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>. You probably use them a lot. They are great. Unless they&#8217;re too big.</p>
<p>The process is simple.</p>
<ul>
<li>You want some code executed before every action in your controller, so you put the code in beforeFilter callback.</li>
<li>Then you want some code executed only before certain actions, so you add the if-then-else block to your before filter that deciced whether to execute the code or not.</li>
<li>Some code needs to be executed only if the user is logged in, so another if-then-else block is created.</li>
<li>More conditions creates more mess in your callback&#8230;</li>
</ul>
<p>Could it be changed? Well, look at following snippet which comes from Ruby on Rails.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> UsersController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  before_filter <span style="color:#ff3333; font-weight:bold;">:do_something</span>, <span style="color:#ff3333; font-weight:bold;">:do_always</span>
  before_filter <span style="color:#ff3333; font-weight:bold;">:do_something_else</span>, <span style="color:#ff3333; font-weight:bold;">:only</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;">:new</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  before_filter <span style="color:#ff3333; font-weight:bold;">:do_something_different</span>, <span style="color:#ff3333; font-weight:bold;">:except</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:edit</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># method definitions commented out</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>What will happen:</p>
<ul>
<li><em>do_something</em> and <em>do_always</em> methods will be called during every request</li>
<li><em>do_something_else</em> method will be called only if <em>show</em> or <em>new</em> actions are requested</li>
<li><em>do_something_different</em> method will be called during every request except those to <em>edit</em> action</li>
</ul>
<p>Clean and simple. Every piece of code that needs to be called in before_filter callback (under whatever conditions) is placed in it&#8217;s own method. The controller decides what methods to invoke and when.</p>
<p>I prefer this approach to Cake&#8217;s built-in one, so I decided to port it.</p>
<p>You need to put this code into your AppController.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AppController <span style="color: #000000; font-weight: bold;">extends</span> Controller <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> _callbacks<span style="color: #009900;">&#40;</span><span style="color: #000088;">$callbacks</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$defaults</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="">'methods'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="">'only'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="">'except'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="">'if'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="">'unless'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span>;
    <span style="color: #000088;">$ifs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$unlesses</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$methods</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$callbacks</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> am<span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span><span style="color: #339933;">,</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000088;">$ok</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span>;
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="">'if'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$if</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$if</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ifs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$ifs</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$if</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatchMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_$if&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$ok</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ok</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$ifs</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$if</span><span style="color: #009900;">&#93;</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$ok</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">break</span>;
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="">'unless'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$unless</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unless</span><span style="color: #339933;">,</span> <span style="color: #000088;">$unlesses</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$unlesses</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$unless</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatchMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_$unless&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$ok</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ok</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$unlesses</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$unless</span><span style="color: #009900;">&#93;</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$ok</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">break</span>;
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ok</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="">'only'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #339933;">,</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="">'only'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$ok</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span>;
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="">'except'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #339933;">,</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="">'except'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$ok</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span>;
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ok</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$methods</span> <span style="color: #339933;">=</span> am<span style="color: #009900;">&#40;</span><span style="color: #000088;">$methods</span><span style="color: #339933;">,</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="">'methods'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$methods</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatchMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_$method&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> __mergeVars<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$pluginName</span> <span style="color: #339933;">=</span> Inflector<span style="color: #339933;">::</span><span style="color: #004000;">camelize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">plugin</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000088;">$pluginController</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pluginName</span> <span style="color: #339933;">.</span> <span style="">'AppController'</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_subclass_of</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="">'AppController'</span><span style="color: #009900;">&#41;</span> || <span style="color: #990000;">is_subclass_of</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pluginController</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$appVars</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_class_vars</span><span style="color: #009900;">&#40;</span><span style="">'AppController'</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #000088;">$uses</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$appVars</span><span style="color: #009900;">&#91;</span><span style="">'uses'</span><span style="color: #009900;">&#93;</span>;
      <span style="color: #000088;">$merge</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'beforeFilter'</span><span style="color: #339933;">,</span> <span style="">'afterFilter'</span><span style="color: #339933;">,</span> <span style="">'beforeRender'</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #000088;">$plugin</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">plugin</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$plugin</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pluginName</span> <span style="color: #339933;">.</span> <span style="">'.'</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_subclass_of</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pluginController</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$pluginController</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span>;
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$pluginController</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span>;
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pluginController</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$pluginVars</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_class_vars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pluginController</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$merge</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$appVar</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$appVars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$appVars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$appVars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$appVars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000088;">$pluginVar</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pluginVars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pluginVars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pluginVars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$pluginVars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000088;">$thisVar</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> am<span style="color: #009900;">&#40;</span><span style="color: #000088;">$appVar</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pluginVar</span><span style="color: #339933;">,</span> <span style="color: #000088;">$thisVar</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    parent<span style="color: #339933;">::</span>__mergeVars<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> beforeFilter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beforeFilter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_callbacks<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beforeFilter</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> afterFilter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">afterFilter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_callbacks<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">afterFilter</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> beforeRender<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beforeRender</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_callbacks<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beforeRender</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Pretty long but worth it. Now you can define in controllers (or AppController, or PluginAppController):</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$beforeFilter</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="">'methods'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'do_something'</span><span style="color: #339933;">,</span> <span style="">'do_always'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="">'methods'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'do_something_else'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="">'only'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'show'</span><span style="color: #339933;">,</span> <span style="">'new'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="">'methods'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'do_something_different'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="">'except'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'edit'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> _do_something<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">function</span> _do_always<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">function</span> _do_something_else<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">function</span> _do_something_different <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is equivalent to Rails definition above. Note the underscores before function names. They help distinguish those methods from usual controller actions.</p>
<p>You can do even more:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$beforeFilter</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="">'methods'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'do_something'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="">'if'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'is_admin'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="">'methods'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'do_something_else'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="">'unless'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'is_logged_in'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> _logged_in<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">function</span> _is_admin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now <em>do_something</em> method will be called if <em>_is_admin</em> returns true, and <em>do_something_else</em> if <em>_is_logged_in</em> returns false. It&#8217;s self-explanatory I think. </p>
<p>Of course you can use all options (only, except, if, unless) together or mix them.</p>
<p>The definitions are processed in order they appear in $beforeFilter (or $beforeRender, or $afterFilter) array. </p>
<p>The definitions from your controller are merged with those defined in PluginAppController and AppController (thanks to redefined __mergeVars() method which is called during controller construction). Firstly AppController&#8217;s definitions are processed, then PluginAppController&#8217;s, and your controller&#8217;s at last.</p>
<p>Enjoy and comment please.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/11/04/uploadpack-easy-and-flexible-way-to-upload-files-with-cakephp/" title="UploadPack - easy and flexible way to upload files with CakePHP">UploadPack - easy and flexible way to upload files with CakePHP</a></li>
<li><a href="http://codetunes.com/2008/09/15/namedscopebehavior-upgrade/" title="NamedScopeBehavior upgrade">NamedScopeBehavior upgrade</a></li>
<li><a href="http://codetunes.com/2008/09/05/named-scope-in-cakephp/" title="Named scope in CakePHP">Named scope in CakePHP</a></li>
<li><a href="http://codetunes.com/2008/07/26/send-this-page-to-friend-with-polymorphic-contoller-in-rails/" title="&#8220;Send this page to friend&#8221; with polymorphic controller in Rails">&#8220;Send this page to friend&#8221; with polymorphic controller in Rails</a></li>
<li><a href="http://codetunes.com/2008/07/11/migration-to-cakephp-rc2/" title="Migration to CakePHP RC2">Migration to CakePHP RC2</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F08%2F21%2Fconvenient-controllers-callback-methods-with-cakephp%2F';
  addthis_title  = 'Convenient+controller%26%238217%3Bs+callback+methods+with+CakePHP';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=rWK2ym"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=rWK2ym" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/08/21/convenient-controllers-callback-methods-with-cakephp/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Custom thumbnail generation with Paperclip</title>
		<link>http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip/</link>
		<comments>http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 10:34:15 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Plugins]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[paperclip]]></category>

		<category><![CDATA[plugin]]></category>

		<category><![CDATA[thumbnailing]]></category>

		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=37</guid>
		<description><![CDATA[Paperclip is a great plugin for Ruby on Rails which eases the pain of image upload and resize process. The usage is very simple, results are what you want in most cases. Not in all cases though.
Your role is only to define the sizes of thumbnails that will be generated from original image. Thumnailing in [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://thoughtbot.com/projects/paperclip" title="Paperclip">Paperclip</a> is a great plugin for Ruby on Rails which eases the pain of image upload and resize process. The usage is very simple, results are what you want in most cases. Not in all cases though.</p>
<p>Your role is only to define the sizes of thumbnails that will be generated from original image. Thumnailing in is fact done by calling ImageMagick&#8217;s <b>convert</b> command with <b>-scale</b> argument, for example <b>convert -scale &#8216;640&#215;480&gt;&#8217;</b>.</p>
<p>You can alter how exactly the image is scaled by appending modifiers to desired thumbnail size, like &#8216;>&#8217; in &#8216;640&#215;480<b>&gt;</b>, however it won&#8217;t give you unlimited power over thumbnailing process.</p>
<p>Consider following example. I have a small image (120&#215;120).</p>
<p><img src="http://codetunes.com/wp-content/uploads/2008/07/av.gif" alt="" /></p>
<p>Scaling it to smaller size gives me the result I want. But when the result image needs to be bigger than original, the original image is enlarged in a way that you&#8217;ll see single pixels.</p>
<p><img src="http://codetunes.com/wp-content/uploads/2008/07/av300x300pixels.gif" alt="" /></p>
<p>What I want to achieve is the enlargment done not by scaling the original image, but by adding a border around it.</p>
<p><img src="http://codetunes.com/wp-content/uploads/2008/07/av300x300.gif" style="border: 1px solid black" alt="" /></p>
<p>This cannot be achieved by adding any of modifiers to desired thumbnail size, so why not to pass additional arguments to <b>convert</b> command? Yes, that would be a solution.</p>
<p>And here&#8217;s my solution - <a href="http://github.com/netguru/paperclip-extended" title="PaperclipExtended">PaperclipExtended</a>. It&#8217;s a plugin that modifies original Paperclip, so that now it accepts additional (optional) parameter <b>:commands</b> when defining the thumbnails sizes.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_attached_file <span style="color:#ff3333; font-weight:bold;">:avatar</span>, <span style="color:#ff3333; font-weight:bold;">:styles</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;">:medium</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;300x300&gt;&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:small</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;100x100&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#ff3333; font-weight:bold;">:commands</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;">:medium</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;-background white -gravity center -extent 300x300 +repage&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>During thumbnail generation Paperclip will now append given commands to <b>convert</b> command. Convert command for <i>medium</i> style will be now:<br />
<b>convert -scale &#8216;300&#215;300&gt;&#8217; -background white -gravity center -extent 300&#215;300 +repage</b><br />
Convert command for <i>small</i> style will remain unchanged. </p>
<p>Passing such <b>commands</b> will help me with the enlargment I mentioned above. For what else could be done check the documentation of <a href="http://www.imagemagick.org/script/command-line-options.php" title="ImageMagick command line options">ImageMagick&#8217;s command line options</a>.</p>
<p><a href="http://github.com/netguru/paperclip-extended" title="PaperclipExtended">PaperclipExtended</a> is not a replacement for Paperclip, just an extension. It works with version 2.1.2 of Paperclip plugin (the current one and the only one I tested). Compatibility with future version is not guaranteed.</p>
<p>I put a plugin on GitHub.<br />
<a href="http://github.com/netguru/paperclip-extended">http://github.com/netguru/paperclip-extended</a></p>
<p>How to install:</p>
<pre>
script/plugin install git://github.com/netguru/paperclip-extended.git
</pre>
<p>Configuration is the same as of original Paperclip plugin, but you can now add an optional <b>:commands</b> parameter.</p>
<p>Hope it&#8217;ll be helpful!</p>
<p>If you have any questions, post them in comments to this post.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/11/04/uploadpack-easy-and-flexible-way-to-upload-files-with-cakephp/" title="UploadPack - easy and flexible way to upload files with CakePHP">UploadPack - easy and flexible way to upload files with CakePHP</a></li>
<li><a href="http://codetunes.com/2008/10/26/uploading-to-multiple-s3-buckets-with-paperclip-and-rails/" title="Uploading to multiple S3 buckets with Paperclip and Rails">Uploading to multiple S3 buckets with Paperclip and Rails</a></li>
<li><a href="http://codetunes.com/2008/07/26/send-this-page-to-friend-with-polymorphic-contoller-in-rails/" title="&#8220;Send this page to friend&#8221; with polymorphic controller in Rails">&#8220;Send this page to friend&#8221; with polymorphic controller in Rails</a></li>
<li><a href="http://codetunes.com/2008/07/20/tableless-models-in-rails/" title="Tableless models in Rails">Tableless models in Rails</a></li>
<li><a href="http://codetunes.com/2008/07/01/formatting-dates-for-sql-queries-in-rails/" title="Formatting dates for SQL queries in Rails">Formatting dates for SQL queries in Rails</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F07%2F30%2Fcustom-thumbnail-generation-with-paperclip%2F';
  addthis_title  = 'Custom+thumbnail+generation+with+Paperclip';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=cp0GoV"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=cp0GoV" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip/feed/</wfw:commentRss>
		</item>
		<item>
		<title>“Send this page to friend” with polymorphic controller in Rails</title>
		<link>http://codetunes.com/2008/07/26/send-this-page-to-friend-with-polymorphic-contoller-in-rails/</link>
		<comments>http://codetunes.com/2008/07/26/send-this-page-to-friend-with-polymorphic-contoller-in-rails/#comments</comments>
		<pubDate>Sat, 26 Jul 2008 14:06:19 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[controller]]></category>

		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=33</guid>
		<description><![CDATA[Last week I wrote about Tableless models in Ruby on Rails, giving an example of Recommendation model which could be used to validate users&#8217; recommendations. Today I will extend that example so that it will be more complete.
What the mentioned solution lacks is a controller handling recommendation actions. Let&#8217;s first make Article and Photo models [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I wrote about <a title="Tableless models in Ruby on Rails" href="http://codetunes.com/2008/07/20/tableless-models-in-rails/">Tableless models in Ruby on Rails</a>, giving an example of Recommendation model which could be used to validate users&#8217; recommendations. Today I will extend that example so that it will be more complete.</p>
<p>What the mentioned solution lacks is a controller handling recommendation actions. Let&#8217;s first make Article and Photo models recoommendable ones by defining their associations:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">  has_many <span style="color:#ff3333; font-weight:bold;">:recommendations</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:recommendable</span></pre></div></div>

<p>The models are ready, so let&#8217;s create a controller.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> RecommendationsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> new
    <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;">:photo_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
      <span style="color:#0066ff; font-weight:bold;">@recommedable</span> = Photo.<span style="color:#9900CC;">find</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:photo_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> !params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:article_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
      <span style="color:#0066ff; font-weight:bold;">@recommedable</span> = Article.<span style="color:#9900CC;">find</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:article_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@recommendable</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@recommendable</span> = Recommendable.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:params</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:recommendation</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@recommendable</span>.<span style="color:#9900CC;">valid</span>?
      <span style="color:#008000; font-style:italic;"># send it via e-mail</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'Your recommendations has been processed'</span>
      redirect_to <span style="color:#0066ff; font-weight:bold;">@recommendation</span>.<span style="color:#9900CC;">recommendable</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:new</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We&#8217;re almost there, but there&#8217;s more Rails can do for us here. Let&#8217;s define some new routes.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># config/routes.rb</span>
  map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:recommendations</span>
&nbsp;
  map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:articles</span> <span style="color:#9966CC; font-weight:bold;">do</span> |a|
    a.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:recommendations</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:photos</span> <span style="color:#9966CC; font-weight:bold;">do</span> |p|
    <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:recommendations</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now we&#8217;re able to use those pretty helpers in our views that will generate nice-looking URLs (like: article/1/recommendation/new).</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'recommend this article'</span>, new_article_recommendation_path<span style="color:#006600; font-weight:bold;">&#40;</span>@article<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>The link above will take us to the &#8216;new&#8217; action of RecommendationsController. The last thing we need to do is to render the form, so that user can comment his recommendation.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@recommendable</span>.<span style="color:#9900CC;">recommendations</span>.<span style="color:#9900CC;">build</span> <span style="color:#9966CC; font-weight:bold;">do</span> |f| <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">hidden_field</span> <span style="color:#ff3333; font-weight:bold;">:recommendable_type</span>, <span style="color:#ff3333; font-weight:bold;">:value</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@recommendable</span>.<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">hidden_field</span> <span style="color:#ff3333; font-weight:bold;">:recommendable_id</span>, <span style="color:#ff3333; font-weight:bold;">:value</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@recommendable</span>.<span style="color:#9900CC;">id</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_area</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">'Do it!'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>There are many more cases of use polymorphic controllers (with normal or tableless models) than sending recommendations. Commenting, ranking, abuse-reporting, tagging&#8230; The rule is that if you have a model that is associated to other models via polymorphic association, you should consider using polymorpic controller to manage it. Not only Rails will do much work for you behind the scenes, but the structure of your application will stay clear both inside (reusable code) and outside (nice links).<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/08/21/convenient-controllers-callback-methods-with-cakephp/" title="Convenient controller&#8217;s callback methods with CakePHP">Convenient controller&#8217;s callback methods with CakePHP</a></li>
<li><a href="http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip/" title="Custom thumbnail generation with Paperclip">Custom thumbnail generation with Paperclip</a></li>
<li><a href="http://codetunes.com/2008/07/20/tableless-models-in-rails/" title="Tableless models in Rails">Tableless models in Rails</a></li>
<li><a href="http://codetunes.com/2008/07/01/formatting-dates-for-sql-queries-in-rails/" title="Formatting dates for SQL queries in Rails">Formatting dates for SQL queries in Rails</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F07%2F26%2Fsend-this-page-to-friend-with-polymorphic-contoller-in-rails%2F';
  addthis_title  = '%26%238220%3BSend+this+page+to+friend%26%238221%3B+with+polymorphic+controller+in+Rails';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=vRdOYH"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=vRdOYH" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/07/26/send-this-page-to-friend-with-polymorphic-contoller-in-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Welcome to Code Tunes</title>
		<link>http://codetunes.com/2008/07/23/welcome-to-code-tunes/</link>
		<comments>http://codetunes.com/2008/07/23/welcome-to-code-tunes/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 22:33:16 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.codetunes.com/?p=30</guid>
		<description><![CDATA[Developing for web is fun! blog has been renamed to Code Tunes and moved to a new domain codetunes.com. Please update your RSS subscriptions to follow the new feed.
Thanks for reading and stay tuned for more. New stuff is coming.
Related posts

No Related Post


  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F07%2F23%2Fwelcome-to-code-tunes%2F';
  addthis_title  = 'Welcome+to+Code+Tunes';
 [...]]]></description>
			<content:encoded><![CDATA[<p>Developing for web is fun! blog has been renamed to Code Tunes and moved to a new domain <a title="Code Tunes" href="http://codetunes.com">codetunes.com</a>. Please update your RSS subscriptions to follow the <a title="Code Tunes RSS" href="feed:http://www.codetunes.com/feed/">new feed</a>.</p>
<p>Thanks for reading and stay tuned for more. New stuff is coming.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li>No Related Post</li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F07%2F23%2Fwelcome-to-code-tunes%2F';
  addthis_title  = 'Welcome+to+Code+Tunes';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=k58XFn"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=k58XFn" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/07/23/welcome-to-code-tunes/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Tableless models in Rails</title>
		<link>http://codetunes.com/2008/07/20/tableless-models-in-rails/</link>
		<comments>http://codetunes.com/2008/07/20/tableless-models-in-rails/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 14:43:55 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[Model]]></category>

		<category><![CDATA[REST]]></category>

		<category><![CDATA[tableless]]></category>

		<guid isPermaLink="false">http://blog.szajbe.pl/?p=25</guid>
		<description><![CDATA[If you&#8217;re developing an application in Ruby on Rails framework, there are many situations when you should consider using tableless models. The advantages you&#8217;d get are:

consistent application structure, because you&#8217;re using models to represent objects in your app
routes available for free if you also define controllers dedicated for those models, resulting in RESTful application
easy validation [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re developing an application in Ruby on Rails framework, there are many situations when you should consider using tableless models. The advantages you&#8217;d get are:</p>
<ul>
<li>consistent application structure, because you&#8217;re using models to represent objects in your app</li>
<li>routes available for free if you also define controllers dedicated for those models, resulting in <a title="RESTful example: the World Wide Web" href="http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_example:_the_World_Wide_Web">RESTful</a> application</li>
<li>easy validation (just like with normal models) and other goodies shipped with Active Record</li>
<li>easier testing with unit tests</li>
<li>form building as easy as with normal models</li>
</ul>
<p>If you&#8217;re familiar with <a title="Skinny Controller, Fat Model" href="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model">&#8220;fat model, skinny controller&#8221;</a> concept you&#8217;ll find more reasons.</p>
<p>An example situation: you give the users an ability to recommends object they find on the site to their friends. They can recommend photos and articles. You do not want to track those recommendations in database. Your model file would look like this.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Recommendation <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">columns</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#0066ff; font-weight:bold;">@columns</span> ||= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">column</span><span style="color:#006600; font-weight:bold;">&#40;</span>name, sql_type = <span style="color:#0000FF; font-weight:bold;">nil</span>, default = <span style="color:#0000FF; font-weight:bold;">nil</span>, null = <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    columns <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::ConnectionAdapters::Column</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>name.<span style="color:#9900CC;">to_s</span>, default, sql_type.<span style="color:#9900CC;">to_s</span>, null<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  column <span style="color:#ff3333; font-weight:bold;">:recommendable_type</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:recommendable_id</span>, <span style="color:#ff3333; font-weight:bold;">:integer</span>
  column <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:body</span>, <span style="color:#ff3333; font-weight:bold;">:text</span>
&nbsp;
  belongs_to <span style="color:#ff3333; font-weight:bold;">:recommendable</span>, <span style="color:#ff3333; font-weight:bold;">:polymorphic</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:recommendable</span>
  validates_associated <span style="color:#ff3333; font-weight:bold;">:recommendable</span>
  validates_format_of <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">/</span>^$|^\S<span style="color:#006600; font-weight:bold;">+</span>\@<span style="color:#006600; font-weight:bold;">&#40;</span>\<span style="color:#006600; font-weight:bold;">&#91;</span>?<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">-</span>zA<span style="color:#006600; font-weight:bold;">-</span>Z0<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9</span>\<span style="color:#006600; font-weight:bold;">-</span>\.<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span>\.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">-</span>zA<span style="color:#006600; font-weight:bold;">-</span>Z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#125;</span>|<span style="color:#006600; font-weight:bold;">&#91;</span>0<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>\<span style="color:#006600; font-weight:bold;">&#93;</span>?<span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">/</span>ix
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:body</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>As you can see the only difference in model definition is that you need to provide the columns yourself.</p>
<p>The idea was borrowed from <a href="http://snipplr.com/view/1849/tableless-model/" title="Tableless model">this snippet</a>.</p>
<p>In the next article I&#8217;ll cover polymorphic controllers which will help to convert above example into complete one.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip/" title="Custom thumbnail generation with Paperclip">Custom thumbnail generation with Paperclip</a></li>
<li><a href="http://codetunes.com/2008/07/26/send-this-page-to-friend-with-polymorphic-contoller-in-rails/" title="&#8220;Send this page to friend&#8221; with polymorphic controller in Rails">&#8220;Send this page to friend&#8221; with polymorphic controller in Rails</a></li>
<li><a href="http://codetunes.com/2008/07/07/not-so-obvious-behavior-of-uses-in-cakephp/" title="Not so obvious behavior of $uses in CakePHP">Not so obvious behavior of $uses in CakePHP</a></li>
<li><a href="http://codetunes.com/2008/07/01/formatting-dates-for-sql-queries-in-rails/" title="Formatting dates for SQL queries in Rails">Formatting dates for SQL queries in Rails</a></li>
<li><a href="http://codetunes.com/2007/11/30/read-only-model-fields/" title="Read-only model fields">Read-only model fields</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F07%2F20%2Ftableless-models-in-rails%2F';
  addthis_title  = 'Tableless+models+in+Rails';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feeds.feedburner.com/~a/CodeTunes?a=Luwana"><img src="http://feeds.feedburner.com/~a/CodeTunes?i=Luwana" border="0"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/07/20/tableless-models-in-rails/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
