<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codeulate. &#187; ruby</title>
	<atom:link href="http://codeulate.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeulate.com</link>
	<description></description>
	<lastBuildDate>Mon, 03 Oct 2011 04:12:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>A handy vim macro for RSpec users</title>
		<link>http://codeulate.com/2011/02/a-handy-vim-macro-for-rspec-users/</link>
		<comments>http://codeulate.com/2011/02/a-handy-vim-macro-for-rspec-users/#comments</comments>
		<pubDate>Fri, 18 Feb 2011 15:55:49 +0000</pubDate>
		<dc:creator>Ben Orenstein</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://codeulate.com/?p=277</guid>
		<description><![CDATA[In this screencast, I teach how to record a useful vim macro that does a simple RSpec &#8220;refactoring&#8221;. (You&#8217;ll want to open it on Youtube and watch in high-resolution.) Commands used: 7G &#8211; jump to line 7 &#8220;td &#8211; delete into the t register &#8220;tp &#8211; put from the t register qq &#8211; start recording [...]]]></description>
			<content:encoded><![CDATA[<p>In this screencast, I teach how to record a useful vim macro that does a simple RSpec &#8220;refactoring&#8221;.</p>
<p>(You&#8217;ll want to open it on Youtube and watch in high-resolution.)</p>
<p><iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/_3L0d8wAm_8" frameborder="0" allowfullscreen></iframe></p>
<p>Commands used:<br />
7G &#8211; jump to line 7<br />
&#8220;td &#8211; delete into the t register<br />
&#8220;tp &#8211; put from the t register<br />
qq &#8211; start recording a macro (just a series of keystrokes) into register q<br />
@q &#8211; execute the keystrokes stored in register q<br />
vmap &#8211; set up a mapping active only when in visual mode</p>
]]></content:encoded>
			<wfw:commentRss>http://codeulate.com/2011/02/a-handy-vim-macro-for-rspec-users/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Land Your First Rails Patch (Screencast)</title>
		<link>http://codeulate.com/2011/02/land-your-first-rails-patch-screencast/</link>
		<comments>http://codeulate.com/2011/02/land-your-first-rails-patch-screencast/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 02:24:31 +0000</pubDate>
		<dc:creator>Ben Orenstein</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://codeulate.com/?p=261</guid>
		<description><![CDATA[Last week I posted How to land your first patch in Rails. The post was well-received, but one commenter asked if I might put together more-detailed steps on how to make your first docpatch. Well, I&#8217;ve done just that, and the result is the screencast below. It&#8217;s only 4 minutes, but contains everything you need [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I posted <a href="http://codeulate.com/2011/02/how-to-land-your-first-patch-in-rails/">How to land your first patch in Rails</a>. </p>
<p>The post was well-received, but one commenter asked if I might put together more-detailed steps on how to make your first docpatch. Well, I&#8217;ve done just that, and the result is the screencast below.</p>
<p>It&#8217;s only 4 minutes, but contains everything you need to know to get your name on the <a href="http://contributors.rubyonrails.org/">Rails Contributors</a> list. Check it out!</p>
<p><iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/Z9DoZgO1RFo" frameborder="0" allowfullscreen></iframe></p>
<p><strong>Update</strong>: in the screencast, I mention that you need to message lifo for commit access to docrails. However, as Xavier noted below, docrails now has public write access for everyone, so need to perform that step.</p>
<p>By the way, if you liked the video, you should check out my other <a href="http://www.codeulatescreencasts.com">screencasts for Rails programmers</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeulate.com/2011/02/land-your-first-rails-patch-screencast/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A handy ruby trick: Class.new</title>
		<link>http://codeulate.com/2011/02/a-handy-ruby-trick-class-new/</link>
		<comments>http://codeulate.com/2011/02/a-handy-ruby-trick-class-new/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 04:22:35 +0000</pubDate>
		<dc:creator>Ben Orenstein</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://codeulate.com/?p=234</guid>
		<description><![CDATA[Today I learned that you can pass a block to Class.new, like so: klass = Class.new do &#160; def self.speak &#160; &#160; &#34;hi!&#34; &#160; end end &#62;&#62; klass.speak =&#62; &#34;hi! This is a handy for creating one-off classes; the kind you&#8217;ll throw away quickly. It&#8217;s likely that you&#8217;d do this during tests. Here&#8217;s a test [...]]]></description>
			<content:encoded><![CDATA[<p>Today I learned that you can pass a block to Class.new, like so:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:635px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">klass = <span style="color:#9966CC; font-weight:bold;">Class</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">speak</span><br />
&nbsp; &nbsp; <span style="color:#996600;">&quot;hi!&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> klass.<span style="color:#9900CC;">speak</span><br />
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;hi!</span></div></div>
<p>This is a handy for creating one-off classes; the kind you&#8217;ll throw away quickly. </p>
<p>It&#8217;s likely that you&#8217;d do this during tests. Here&#8217;s a test in active_support that uses this technique:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:635px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> ClassAttributeAccessorTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span></span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> setup<br />
&nbsp; &nbsp; @<span style="color:#9966CC; font-weight:bold;">class</span> = <span style="color:#9966CC; font-weight:bold;">Class</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; cattr_accessor <span style="color:#ff3333; font-weight:bold;">:foo</span><br />
&nbsp; &nbsp; &nbsp; cattr_accessor <span style="color:#ff3333; font-weight:bold;">:bar</span>, &nbsp;<span style="color:#ff3333; font-weight:bold;">:instance_writer</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span><br />
&nbsp; &nbsp; &nbsp; cattr_reader &nbsp; <span style="color:#ff3333; font-weight:bold;">:shaq</span>, <span style="color:#ff3333; font-weight:bold;">:instance_reader</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Now grab an instance of our new class</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@object</span> = @<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">new</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#008000; font-style:italic;"># Elided: tests that make sure the cattr_accessor method behaves</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Another thing I picked up at the same time: you can pass a constant to Class.new and it will set that as the superclass:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:635px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> klass = <span style="color:#9966CC; font-weight:bold;">Class</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">String</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;Class:0x105604658&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> klass.<span style="color:#9900CC;">superclass</span><br />
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">String</span></div></div>
<p>Here&#8217;s another test from Rails. This one makes use of both tricks I just showed you:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:635px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; test <span style="color:#996600;">&quot;configuration is crystalizeable&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; parent = <span style="color:#9966CC; font-weight:bold;">Class</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::Configurable</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; child &nbsp;= <span style="color:#9966CC; font-weight:bold;">Class</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>parent<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># ...</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://codeulate.com/2011/02/a-handy-ruby-trick-class-new/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Use Rails&#8217; Scaffolded Controllers</title>
		<link>http://codeulate.com/2010/11/dont-use-rails-scaffolded-controllers/</link>
		<comments>http://codeulate.com/2010/11/dont-use-rails-scaffolded-controllers/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 05:04:28 +0000</pubDate>
		<dc:creator>Ben Orenstein</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://codeulate.com/?p=199</guid>
		<description><![CDATA[Fellow Rails Developers, the state of the art has moved on, and you should no longer be using the controllers generated by Rails&#8217; scaffold command (or &#8216;rails g resource foo&#8217;). These controllers are all the same. Repetition like that can and should be eliminated. Instead, you should use something like Inherited Resources. Here&#8217;s what a [...]]]></description>
			<content:encoded><![CDATA[<p>Fellow Rails Developers, the state of the art has moved on, and you should no longer be using the controllers generated by Rails&#8217; scaffold command (or &#8216;rails g resource foo&#8217;).</p>
<p>These controllers are all the same. Repetition like that can and should be eliminated.</p>
<p>Instead, you should use something like <a href="https://github.com/josevalim/inherited_resources">Inherited Resources</a>. </p>
<p>Here&#8217;s what a controller generated by Rails looks like:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:635px;height:300px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> UsersController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
&nbsp; <span style="color:#008000; font-style:italic;"># GET /users</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># GET /users.xml</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> index<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@users</span> = User.<span style="color:#9900CC;">all</span><br />
<br />
&nbsp; &nbsp; respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span><span style="color:#CC0066; font-weight:bold;">format</span><span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#008000; font-style:italic;"># index.html.erb</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@users</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#008000; font-style:italic;"># GET /users/1</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># GET /users/1.xml</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> show<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span><span style="color:#CC0066; font-weight:bold;">format</span><span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#008000; font-style:italic;"># show.html.erb</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#008000; font-style:italic;"># GET /users/new</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># GET /users/new.xml</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> new<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">new</span><br />
<br />
&nbsp; &nbsp; respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span><span style="color:#CC0066; font-weight:bold;">format</span><span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#008000; font-style:italic;"># new.html.erb</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#008000; font-style:italic;"># GET /users/1/edit</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> edit<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#008000; font-style:italic;"># POST /users</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># POST /users.xml</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> create<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span><span style="color:#CC0066; font-weight:bold;">format</span><span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">save</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> redirect_to<span style="color:#006600; font-weight:bold;">&#40;</span>@user, <span style="color:#ff3333; font-weight:bold;">:notice</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'User was successfully created.'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:created</span>, <span style="color:#ff3333; font-weight:bold;">:location</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;new&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">errors</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#008000; font-style:italic;"># PUT /users/1</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># PUT /users/1.xml</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> update<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span><span style="color:#CC0066; font-weight:bold;">format</span><span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">update_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> redirect_to<span style="color:#006600; font-weight:bold;">&#40;</span>@user, <span style="color:#ff3333; font-weight:bold;">:notice</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'User was successfully updated.'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span> head <span style="color:#ff3333; font-weight:bold;">:ok</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;edit&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">errors</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#008000; font-style:italic;"># DELETE /users/1</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># DELETE /users/1.xml</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> destroy<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">destroy</span><br />
<br />
&nbsp; &nbsp; respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span><span style="color:#CC0066; font-weight:bold;">format</span><span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> redirect_to<span style="color:#006600; font-weight:bold;">&#40;</span>users_url<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span> head <span style="color:#ff3333; font-weight:bold;">:ok</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Here&#8217;s that SAME controller using Inherited Resources:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:635px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> UsersController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">InheritedResources::Base</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>That&#8217;s right, just by inheriting from InheritedResources::Base you all the standard REST actions for free. </p>
<p>If you need to, you can override any of the default actions:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:635px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> UsersController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">InheritedResources::Base</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> new<br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># do different stuff for new</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>It even has a nice DSL for customizations you&#8217;re likely to need. Here&#8217;s how to change just the redirect for the destroy action:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:635px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> UsersController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">InheritedResources::Base</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> destroy<br />
&nbsp; &nbsp; destroy!<span style="color:#006600; font-weight:bold;">&#123;</span> root_url <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>The best thing is, once you&#8217;ve got that duplicated code out of there, you don&#8217;t need to test it! Inherited Resources already has its own tests for the standard actions, so you only need test the things you explicitly change.</p>
<p>A few months ago, I refactored an app to use Inherited Resources instead of the generated controllers. And by &#8216;refactored&#8217;, I mean &#8216;deleted pages and pages of controller and test code&#8217;. I &#8216;git rm&#8217;d almost all our functional tests and damn that felt <em>good</em>.</p>
<p>You gotta get in on this! The <a href="https://github.com/josevalim/inherited_resources/blob/master/README.rdoc">README</a> is solid, so you should have little trouble getting started.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeulate.com/2010/11/dont-use-rails-scaffolded-controllers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby Best Practices: A Review</title>
		<link>http://codeulate.com/2009/07/ruby-best-practices-a-review/</link>
		<comments>http://codeulate.com/2009/07/ruby-best-practices-a-review/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 19:19:03 +0000</pubDate>
		<dc:creator>Ben Orenstein</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://codeulate.com/?p=26</guid>
		<description><![CDATA[One of my favorite simple pleasures is reading a technical or instructional book where the level of complexity perfectly matches my expertise.  I&#8217;ve been enjoying this exact experience while reading Greg Brown&#8216;s new book, Ruby Best Practices. I have been programming professionally for over three years, with the last five months doing full time Ruby [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favorite simple pleasures is reading a technical or instructional book where the level of complexity perfectly matches my expertise.  I&#8217;ve been enjoying this exact experience while reading <a href="http://rubylearning.com/blog/2009/06/21/interview-author-gregory-brown/">Greg Brown</a>&#8216;s new book, <a href="http://www.amazon.com/gp/product/0596523009?ie=UTF8&amp;tag=codeulate-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596523009">Ruby Best Practices</a>.</p>
<p>I have been programming professionally for over three years, with the last five months doing full time <a href="http://rubyonrails.org/">Ruby on Rails</a> development.  I would label myself an intermediate Ruby user: comfortable with the basics, but with limited exposure to its more advanced topics.  Lately, I have been reading more and more open source Ruby projects, and have been looking for ways to contribute back to our teriffic community.</p>
<p>Given my experience and goals, <a href="http://www.amazon.com/gp/product/0596523009?ie=UTF8&amp;tag=codeulate-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596523009">Ruby Best Practices</a> was a perfect read.  The book is a collection of general strategies for solving problems in Ruby, with a focus on real-world code examples. Its author is an experienced Ruby developer who also happens to be an excellent writer.</p>
<p>Ruby Best Practices has a number of notable strengths. First, Brown is highly pragmatic. When discussing closures, he writes &#8220;I could show some abstract examples or academically exciting functionality such as Proc#curry, but instead, I decided that I wanted to show you something I use fairly frequently.&#8221;  This attitude has lead to a book that is full of ideas you can actually use.  It feels like the experienced guy down the hall showing you all his best stuff.</p>
<p>Secondly, RBP&#8217;s examples are almost uniformly excellent.  As contributor or creator of several popular Ruby projects (<a href="http://github.com/sandal/prawn/tree/master">Prawn </a>and <a href="http://github.com/ruport/ruport/tree/master">Ruport</a>) Brown has no lack of real-word code examples to choose from, and he does so with skill.  In addition, he&#8217;s not shy about trimming down the examples to leave behind just the most relevant code elements.  These two factors combine to produce code listings which are indicative of real open-source programs while remaining quite comprehensible.  Not only are Brown&#8217;s examples excellent, but they are plentiful.  Nearly every new idea he introduces is demonstrated through a case study of real (or plausibly real) code.  Working through this book will definitely sharpen your code-reading skills.</p>
<p>Beyond simply being clear, Brown&#8217;s writing is also fun to read.  His enthusiasm for the subject matter is obvious: &#8220;I could stop here and move on to the next topic, but similar to when we looked into the belly of lazy.rb earlier in this chapter, I can&#8217;t resist walking through and explaining some cool code.&#8221;  This sort enthusiasm continues throughout, and leads to a technical book that I&#8217;m tempted to call a page-turner. Making my way through the book really excited me to read and work on open-source projects.</p>
<p>If you&#8217;re still on the fence, take a read through the sample chapter, <a href="http://cdn.oreilly.com/books/9780596523008/Mastering_the_Dynamic_Toolkit.xml.pdf">Mastering the Dynamic Toolkit</a>.  Or, if you&#8217;d prefer a quicker read, check out <a href="http://blog.rubybestpractices.com/posts/gregory/005-code-reading-stdlib.html">this post</a> on <a href="http://rubybestpractices.com/">the book&#8217;s blog</a> to get a feel for Brown&#8217;s writing style.  If you like what you see, don&#8217;t hesitate to <a href="http://www.amazon.com/gp/product/0596523009?ie=UTF8&amp;tag=codeulate-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596523009">pick up a copy</a> of this excellent book.</p>
<p><strong>Chapter Titles:</strong><br />
1.  Driving Code Through Tests<br />
2.  Designing Beautiful APIs<br />
3.  <a href="http://cdn.oreilly.com/books/9780596523008/Mastering_the_Dynamic_Toolkit.xml.pdf">Mastering the Dynamic Toolkit</a><br />
4.  Text Processing and File Management<br />
5.  Functional Programming Techniques<br />
6.  When Things Go Wrong<br />
7.  Reducing Cultural Barriers<br />
8.  Skillful Project Maintenance<br />
A.  Writing Backwards Compatible Code<br />
B.  Leveraging Ruby&#8217;s Standard Library<br />
C.  Ruby Worst Practices</p>
]]></content:encoded>
			<wfw:commentRss>http://codeulate.com/2009/07/ruby-best-practices-a-review/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

