<?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>Blog - ShiftEleven &#187; rails</title>
	<atom:link href="http://shifteleven.com/articles/tag/rails/feed" rel="self" type="application/rss+xml" />
	<link>http://shifteleven.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 19 Sep 2009 11:19:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-alpha</generator>
		<item>
		<title>Blocks and Helpers, a Lovely Combination</title>
		<link>http://shifteleven.com/articles/2007/01/22/blocks-and-helpers-a-lovely-combination</link>
		<comments>http://shifteleven.com/articles/2007/01/22/blocks-and-helpers-a-lovely-combination#comments</comments>
		<pubDate>Tue, 23 Jan 2007 01:49:36 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/?p=25</guid>
		<description><![CDATA[Blocks rock and I couldn&#8217;t agree more.  The functional programming aspect of ruby has started to interest me more and more.  Blocks are used all over the place, with respond_to, collect, returning and ActionView::Helpers::FormHelper#form_for.
Sometimes, a design requires a little extra markup, perhaps something to get that rounded corner to work or what-have you. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://weblog.jamisbuck.org/2007/1/19/blocks-rock">Blocks rock</a> and I couldn&#8217;t agree more.  The <a href="http://tech.rufy.com/2006/11/functional-programming-in-ruby.html">functional programming aspect of ruby</a> has started to interest me more and more.  Blocks are used all over the place, with <a href="http://api.rubyonrails.com/classes/ActionController/MimeResponds/InstanceMethods.html#M000080,"><code>respond_to</code></a>, <a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003163,"><code>collect</code></a>, <a href="http://errtheblog.com/post/29,"><code>returning</code></a> and <a href="http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html#M000491."><code>ActionView::Helpers::FormHelper#form_for</code></a><code>.</code></p>
<p>Sometimes, a design requires a little extra markup, perhaps something to get that rounded corner to work or what-have you.  Now let&#8217;s say this piece of code requires certain classes, a certain kind of structure, including a tile, and has to be used in many places, something like a sidebar piece.  Instead of copying and pasting this structure over and over, you can use a helper to make you life easier, and if you combine it with a block, it will just feel even more painless.</p>
<p><span id="more-25"></span></p>
<h3>The Setup</h3>
<pre class="html" title="code">&lt;div class="sidebar"&gt;
  &lt;div class="bottom"&gt;
    &lt;h3&gt;Title&lt;/h3&gt;
    &lt;div class="body"&gt;
      &lt;p&gt;Some Content&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;</pre>
<p>There are two elements which must change; the title and the contents of the body.  The title should simply be just some text that goes in between the <code>h3</code> tag.  The contents of the body could be anything, in this case, it is the paragraph tag and everything inside.</p>
<h3>Creating a Helper without a Block</h3>
<p>Since we have these two varying things, we could create a helper method to encapsulate our structure.  The first parameter of the helper will be the title while the second parameter will be the contents of the body.</p>
<pre class="ruby" title="code">def sidebar(title, body)
  content_tag(
    'div',
    content_tag(
      'div',
      content_tag('h3', title) + content_tag('div', body, :class =&gt; 'body'),
      :class =&gt; 'bottom'
    ),
    :class =&gt; 'sidebar'
  )
end</pre>
<p>That looks pretty easy.  Now let&#8217;s look at the interface for this helper</p>
<pre class="html" title="code">&lt;%= sidebar('Hello World',
  '&lt;p&gt;It is one of these "Hello World" things again.&lt;/p&gt;') %&gt;</pre>
<p>Not too shabby, but what if we wanted to do an unordered list?</p>
<pre class="ruby" title="code">&lt;%= sidebar(
  'Hello World',
  '&lt;ul&gt;' +
    '&lt;li&gt;Item&lt;/li&gt;' +
    '&lt;li&gt;Item&lt;/li&gt;' +
    '&lt;li&gt;Item&lt;/li&gt;' +
  '&lt;/ul&gt;'
) %&gt;</pre>
<p>This feels pretty clunky and <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/">ERB</a> is completely ignored.  Instead of this procedural way of thinking, why not see what a block can do for us?</p>
<h3>Creating a Helper +with+ a Block</h3>
<p>For this to work, we will still need the first parameter to be the title, but the body will be passed in as a block to be processed from within our helper.  The block we create will be ERB code, this will feel more natural to the person who has to implement the code.  The magic to get this done comes from the <a href="http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000611"><code>concat</code></a> method of <code>ActionView::Helpers::TextHelper</code> module.</p>
<pre class="ruby" title="code">def sidebar(title, &amp;block)
  raise ArgumentError, "Missing block" unless block_given?

  start = &lt;&lt;-END
    &lt;div class="sidebar"&gt;
      &lt;div class="bottom"&gt;
        &lt;h3&gt;#{title}&lt;/h3&gt;
        &lt;div class="body"&gt;
  END
  concat(start, block.binding)

  block.call

  end = &lt;&lt;-END
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  END
  concat(end, block.binding)

end</pre>
<p>This may look slightly odd, but I assure you, the interface makes up for it.</p>
<pre class="html" title="code">&lt;% sidebar('Hello World') do %&gt;
  &lt;p&gt;And now back to the way I was doing things before&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;Item&lt;/li&gt;
    &lt;li&gt;Item&lt;/li&gt;
    &lt;li&gt;Item&lt;/li&gt;
  &lt;/ul&gt;
&lt;% end %&gt;</pre>
<h3>The Benefits</h3>
<ul>
<li>The implementation code is more readable</li>
<li>It gives you one place to make your common edits</li>
<li>It keeps your code <a href="http://www.artima.com/intv/dry.html">DRY(don&#8217;t repeat yourself)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2007/01/22/blocks-and-helpers-a-lovely-combination/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>DRYing Up respond_to</title>
		<link>http://shifteleven.com/articles/2006/11/26/drying-up-respond_to</link>
		<comments>http://shifteleven.com/articles/2006/11/26/drying-up-respond_to#comments</comments>
		<pubDate>Sun, 26 Nov 2006 23:34:58 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[respond_to]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/?p=19</guid>
		<description><![CDATA[Now that that release candidate for rails 1.2 is out, people are going to have even more reasons to use the respond_to method in their controller methods.  With that, there may come some unsettling feeling that you&#8217;re just repeating yourself over and over again.
def index
  @articles = Article.find(:all)
  respond_to do &#124;format&#124;
  [...]]]></description>
			<content:encoded><![CDATA[<p>Now that that <a href="http://blog.rubyonrails.org/2006/11/23/rails-1-2-release-candidate-1">release candidate for rails 1.2</a> is out, people are going to have even more reasons to use the <code>respond_to</code> method in their controller methods.  With that, there may come some unsettling feeling that you&#8217;re just repeating yourself over and over again.</p>
<pre class="ruby" title="code">def index
  @articles = Article.find(:all)
  respond_to do |format|
    format.html
    format.xml { render :xml =&gt; @articles.to_xml }
  end
end

def show
  @article = Article.find(params[:id])
  respond_to do |format|
    format.html
    format.xml { render :xml =&gt; @article.to_xml }
  end
end</pre>
<p>If we want to use the <code>respond_to</code>, we have to make sure it&#8217;s in all of our methods.  Wouldn&#8217;t it be easier to just do something like this?</p>
<pre class="ruby" title="code">def index
  @articles = Article.find(:all)
  format.html
  format.xml { render :xml =&gt; @articles.to_xml }
end</pre>
<p>Doing it this way, we don&#8217;t have to write 2 extra lines per method, and we don&#8217;t increase our indentation.  While that&#8217;s not a huge thing, I still just don&#8217;t like to constantly write out those lines when I don&#8217;t have to when I think we can build something a little trickier.  <span id="more-19"></span></p>
<h3>The Breakdown</h3>
<p>In the simplified example I gave above, we have several things that we need to accomplish.  One is to wrap our methods with a <code>respond_to</code> by using <a href="http://www.rubycentral.com/book/classes.html."><code>alias_method</code></a> Well, it&#8217;s actually not that simple because <code>respond_to</code> is a block and we will need to do some trickery there.  Another task is we need to create a <code>format</code> method.  Since we won&#8217;t be calling <code>format</code> with a block parameter, we need to make a method which will do the dirty work for us.  Finally, we need to make a tweak to <code>ActionController::MimeResponders::Respond#respond</code> for some of the other things that we are doing.</p>
<h4>Adding <code>wrap_respond_to_in</code> to our ApplicationController</h4>
<p>To start things off, we need to make a class method, which I have dubbed <code>wrap_respond_to_in</code> (I would <strong>love</strong> suggestions for names here).</p>
<pre class="ruby" title="code">class &lt;&lt; self
  def wrap_respond_to_in(*actions)
    if actions.include?(:all)
      # Also remove #rescue_action because this gets set in tests. Also include some of my friendly #find methods.
      actions = actions + self.public_instance_methods - ApplicationController.public_instance_methods - ['rescue_action'] - [:all]
      [:find_collection, :find_new, :find_member].each do |finder|
        actions &lt;&lt; finder if method_defined?(finder)
      end
    end

    # Returns the appropriate Responder format based on the action.
    define_method(:format) do
      @format.last rescue nil
    end

    for action in actions
      action = action.to_sym

      module_eval &lt;&lt;-END
        alias_method(:__#{action.to_i}__, :#{action.to_s})
        private :__#{action.to_i}__

        def #{action.to_s}(*args)
          result = nil      # This is the result of whatever method this wraps
          respond_to do |format|
            @format ||= []
            @format.push(format)
            result = __#{action.to_i}__(*args)
            @format.pop
          end
          return result
        end
      END
    end
  end
end</pre>
<p>Let&#8217;s break this down by section.  First things first, the method&#8217;s arguments should be a list of symbols for the names of the methods we would like to use our <code>respond_to</code> shorthand.  Because I would like to use this shorthand for all of my action methods, I have included the notion of sending <code>:all</code> as a parameter.  This will make sure that all of the public methods and some protected methods that I use (<code>find_member</code>, <code>find_collection</code>, and <code>find_new</code>).  Again, I like to keep things DRY(Don&#8217;t Repeat Yourself).  Next we define a method called <code>format</code>.  This will be an accessor to the last element of our <code>format</code> instance variable array.  More on this later.  After we have defined that, we need to loop through all of our methods that we want to use <code>respond_to</code>.  In each loop, we need to make an alias for our methods.  This is so that we can re-write the method, yet still keep a copy of the original code.  Next we overwrite the method.  As you can see here, this is where we put the <code>respond_to</code> block.  Now in this block, we have access to the <code>format</code> variable.  Next we push this value into our <code>format</code> instance variable.  The reasoning for using an array is to accommodate for a nested set of calls.  After we push <code>format</code> onto <code>format</code>, we then call our original method, which uses the shorthand and save that to to a variable.  Once that is done, we pop off the last value we put into the format@ variable, since we are done with it.  Finally, return the result of our original method.  This is the backbone for what we need to do.  Now we need to implement it.</p>
<h4>Tweaking ActionController::MimeResponds::Responder</h4>
<p>Because we now have assumed that all of our methods are going to use <code>respond_to</code>, we need to make a way out if for some reason we don&#8217;t need to use said method.  This would mean that our method exists without calling the <code>format</code> method.  In order to do that, we can use the following code:</p>
<pre class="ruby" title="code">module ActionController #:nodoc:
  module MimeResponds #:nodoc:
    class Responder
      alias_method :__respond_without_check_for_empty_order__, :respond
      private :__respond_without_check_for_empty_order__

      def respond
        return if @order.empty?
        __respond_and_check_for_empty_order__
      end
    end
  end
end</pre>
<p>We simply do a check to see if order is empty.  If so, that means our <code>format</code> method was never called, so we just simply need to exit out of the <code>respond</code> method.  I put this in my <code>lib</code> directory and make sure that my app requires this after rails has been loaded.</p>
<h4>Using <code>wrap_respond_to_in</code></h4>
<p>Quite simply all we need to do to use it is to call it, <strong>but we must do so after we have defined all of our methods for which we want to use this technique</strong></p>
<p>.</p>
<pre class="ruby" title="code">class Articles &lt; ApplicationController
  before_filter :find_member, :only =&gt; [:show]

  def show
    format.html
    format.xml { render :xml =&gt; @article.to_xml }
  end

  protected
    def find_member
      @article = Article.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      format.html do
        flash[:error] = "Article was not found"
        redirect_to articles_url
      end
      format.xml { head :status =&gt; 404 }
    end

    wrap_respond_to_in :all
end</pre>
<p>While this is not a complete controller, it does show you how to use it.</p>
<h3>Final Words</h3>
<p>There&#8217;s no reason to put a <code>respond_to</code> call in all of the methods when there is a way around it.  Once I figure out how to get SVN(subversion) working with <a href="http://www.dreamhost.com/,">Dreamhost</a> I am going to put this into a plugin for all to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2006/11/26/drying-up-respond_to/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Loading Your Rails Environment Into a Script</title>
		<link>http://shifteleven.com/articles/2006/10/08/loading-your-rails-environment-into-a-script</link>
		<comments>http://shifteleven.com/articles/2006/10/08/loading-your-rails-environment-into-a-script#comments</comments>
		<pubDate>Mon, 09 Oct 2006 03:08:52 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/10/08/loading-your-rails-environment-into-a-script</guid>
		<description><![CDATA[This is a fairly simple little tidbit, but still useful.  Sometimes I want to have a script for my rails application, like running some reports on the data.  There are two basic ways to do this: write a task(rake), or create a new ruby script file, like in the scripts directory.
If you want [...]]]></description>
			<content:encoded><![CDATA[<p>This is a fairly simple little tidbit, but still useful.  Sometimes I want to have a script for my rails application, like running some reports on the data.  There are two basic ways to do this: write a task(rake), or create a new ruby script file, like in the scripts directory.</p>
<p>If you want to do the latter, then there are two lines you my want to put at the top of your script.</p>
<pre class="ruby" title="code">ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")</pre>
<p>The first line allows us to supply which environment we would like to run the script in.  Say the script is called <code>process.rb</code>, we could call the script like <code>./process.rb production</code> and now the environment is set to production.</p>
<p>The second line loads the environment file.  Now all of rails, your models, libraries, and plugins are available to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2006/10/08/loading-your-rails-environment-into-a-script/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Loading Fixtures in a Migration</title>
		<link>http://shifteleven.com/articles/2006/09/28/loading-fixtures-in-a-migration</link>
		<comments>http://shifteleven.com/articles/2006/09/28/loading-fixtures-in-a-migration#comments</comments>
		<pubDate>Thu, 28 Sep 2006 20:57:23 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[fixtures]]></category>
		<category><![CDATA[migration]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/09/28/loading-fixtures-in-a-migration</guid>
		<description><![CDATA[I love migrations I think they are one of the best things about rails.  I love that I can count on any changes I&#8217;ve done in the database to be consistent across all other databases that I use.  Love it.
Sometimes I find that I want pre-load my database with some data, like categories [...]]]></description>
			<content:encoded><![CDATA[<p>I love <a href="http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations.">migrations</a> I think they are one of the best things about rails.  I love that I can count on any changes I&#8217;ve done in the database to be consistent across all other databases that I use.  Love it.</p>
<p>Sometimes I find that I want pre-load my database with some data, like categories that I have.  For testing purposes, I have already created this data in my fixture; now I just want to load that fixture into my database from a migration call.</p>
<p>I know that there is a <code>rake</code> task for this, so I looked into that code and came up with a little something.</p>
<p><span id="more-14"></span></p>
<pre class="ruby" title="code">require 'active_record/fixtures'

class CreateCategories &lt; ActiveRecord::Migration
  def self.up
    create_table :categories do |t|
      t.column :name, :string
    end

    Fixtures.create_fixtures('test/fixtures', File.basename("categories.yml", '.*'))
  end

  def self.down
    drop_table :categories
  end
end</pre>
<p>First I had to require <code>active_record/fixtures</code>.  This allows me to call the <code>Fixtures#create_fixtures</code> method, which load my data.  Now I can gaurantee that my fixture data will be loaded for any environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2006/09/28/loading-fixtures-in-a-migration/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Sleeping Easier with Migrations</title>
		<link>http://shifteleven.com/articles/2006/09/28/sleeping-easier-with-migrations</link>
		<comments>http://shifteleven.com/articles/2006/09/28/sleeping-easier-with-migrations#comments</comments>
		<pubDate>Thu, 28 Sep 2006 20:48:33 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[migration]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/09/28/sleeping-easier-with-migrations</guid>
		<description><![CDATA[As a rails user who loves migrations, I have noticed that all of the migrations are prefixed with three decimal places, like 001_create_sessions.rb or something of that nature.  That got me thinking, what would happen after I had 999 migrations?
That sets of the paranoia level up because who knows, I may need 1001 migrations! [...]]]></description>
			<content:encoded><![CDATA[<p>As a <a href="http://www.rubyonrails.org">rails</a> user who loves migrations, I have noticed that all of the migrations are prefixed with three decimal places, like <code>001_create_sessions.rb</code> or something of that nature.  That got me thinking, what would happen after I had 999 migrations?</p>
<p>That sets of the paranoia level up because who knows, I may need 1001 migrations!  So to settle this once and for all, I wrote a script that would generate 999 migrations for me.  I ran the 1000 migration and then it created <code>1001_test1001.rb</code>.</p>
<p>Good news! Rails doesn&#8217;t constrain its migrations to a 3 decimal place prefix.  So pointless&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2006/09/28/sleeping-easier-with-migrations/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Previewing Beast: the open source Rails forum</title>
		<link>http://shifteleven.com/articles/2006/09/02/previewing-beast-the-open-source-rails-forum</link>
		<comments>http://shifteleven.com/articles/2006/09/02/previewing-beast-the-open-source-rails-forum#comments</comments>
		<pubDate>Sat, 02 Sep 2006 14:23:58 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[beast]]></category>
		<category><![CDATA[forum]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/09/02/previewing-beast-the-open-source-rails-forum</guid>
		<description><![CDATA[Beast is a nice, light-weight forum written in Rails.  From what I can gather, the overall goal is to make something work with only 500 lines of code, and I think that could happen with this.  It is open source, and can easily be checked out
I did check it out and I have [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://beast.caboo.se/">Beast</a> is a nice, light-weight forum written in Rails.  From what I can gather, the overall goal is to make something work with only 500 lines of code, and I think that could happen with this.  It is open source, and can easily be <a href="http://svn.techno-weenie.net/projects/beast/.">checked out</a></p>
<p>I did check it out and I have to say that I&#8217;m impressed.  It is using the rails edge and is trying out some of the REST(representational state transfer)ful stuff all the while adhering to the philosophies of CRUD(create, read, update, delete).  I have wanted to look into a good example of a rails project using REST, and this code base is great for that.  What&#8217;s more is that it&#8217;s not very difficult to wrap your head around all of the code of the site, due to the small code base and nice abstraction of the code.</p>
<p><span id="more-7"></span></p>
<p>While reading up on this new forum, I came across a <a href="http://www.onekay.com/blog/archives/11/trackback/">post</a> in which the writer was interested in Beast because of the marketing tag-line, written in _under 500 lines_.  He goes on to raise some points on why that philosophy rubs him the wrong way, but I have different views on his points.</p>
<h3>Obscure Code</h3>
<p>This certainly can be the case in many instances.  I have seen a <a href="http://www.webmonkey.com/webmonkey/code/97/18/code2.txt">chat server written in 4 lines</a> of <a href="http://www.perl.org">perl</a> <a href="#PerlChat">code</a>.  That thing is a horrible mess.  That said, I think Beast suffers no where near the extent on that.  It does make use of writing out small methods to the whole line; and being that ruby can lend itself well to being understood when read aloud, it&#8217;s not that obscure.  I will say that in order to read some of these lines, you will have to be somewhat familiar with the <a href="http://wiki.rubygarden.org/Ruby/page/show/RubyIdioms.">idioms of ruby</a></p>
<h3>Included Libraries</h3>
<p><a href="http://www.onekay.com/blog/archives/author/marc/">Marc</a> makes the point of noting that just because you have written 500 lines of code, doesn&#8217;t mean that the project is 500 lines of code is you use frameworks and plugins and other libraries.  Mathematically, I agree; however, there is a difference in code that is apart of your project from that code abstracted to external uses.  You could also then argue that 50 lines in <a href="http://java.sun.com">Java</a> is actually more because you have to think about all of the code that was used to make all of the Java classes as well as that to make the engine but that shouldn&#8217;t matter.  The only thing that really matters is the 50 lines of code that are doing what you need to get the job done.</p>
<h3>Arbitrary Restriction</h3>
<p>It&#8217;s true, the number of lines of code really don&#8217;t matter to a machine; but to make claims that it isn&#8217;t more useful is untrue. Part of keeping code small is that it keeps it agile.  One way to keep the lines of code small in number is to not rewrite functionality.  Not repeating your code keeps your edits quick and you have the ability to change functionality faster. While setting the number at 500 may be weird, trying to adhere to keeping extraneous code out is a good thing.  Now if the project warrents lifting that 500 line cap and then code stays committed to the 500 limit, therefor kludging up things or doing this half-assed, then that is truly absurd.</p>
<h3>Feature loss</h3>
<p>I do not think that you have to lose features in order to make this work.  I think the restrictions can be liberating.  500 lines of code: that means there cannot be all of the bells and whistles; not until the core code is in place.  In order for the forum to work, the core functionality has to be in place, and since the 500 lines are the limit, then it means that the other pieces of scope have to wait.  I&#8217;m not going to say that Marc&#8217;s situation of poorly-implemented code can (and often does) happen, but I think with Beast, they have the right approach.</p>
<p>Beast is shaping up to be a really nice forum, something that you can use the ground work for and build higher if need be.  It&#8217;s simple and elegant and a fine example of a rails project.</p>
<h3>Notes</h3>
<p><a name="PerlChat">Perl Chat</a> written in 4 lines</p>
<pre class="perl" title="code">#!/usr/local/bin/perl -- minchat: run and telnet to port 5555 - bslesins
sub p{print@_}$SIG{CHLD}=sub{wait};socket S,2,2,6;bind S,pack(Snx12,2,5555);
listen S,5;while(accept C,S){if(!fork){open(STDOUT,"&gt;&amp;C");p"name:";$n=substr
,0,-2;$f=fork||exec"tail -f chatlog";open W,"&gt;&gt;chatlog";select(W);$|=1;p
"[$n here]\r\n";while(){p"$n&gt; $_";}p"[$n gone]\r\n";kill 15,$f;exit}}</pre>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2006/09/02/previewing-beast-the-open-source-rails-forum/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Looking towards Rails 1.2 with CRUD</title>
		<link>http://shifteleven.com/articles/2006/08/29/looking-towards-rails-12-with-crud</link>
		<comments>http://shifteleven.com/articles/2006/08/29/looking-towards-rails-12-with-crud#comments</comments>
		<pubDate>Wed, 30 Aug 2006 01:28:42 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[crud]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubyconf]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/08/29/4</guid>
		<description><![CDATA[A couple of months ago, I was shown the article that DHH(David Heinemeier Hannson) wrote after he gave his presentation at RailsConf 2006.  I looked at it then and kind of shrugged it off, mainly because I really didn&#8217;t know what the presentation was really about.
Tonight I found video of that presentation and I [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago, I was shown the <a href="http://www.loudthinking.com/arc/000593.html">article</a> that DHH(David Heinemeier Hannson) wrote after he gave his presentation at RailsConf 2006.  I looked at it then and kind of shrugged it off, mainly because I really didn&#8217;t know what the <a href="http://www.loudthinking.com/lt-files/worldofresources.pdf">presentation</a> was really about.</p>
<p>Tonight I found <a href="http://blog.scribestudio.com/articles/2006/07/09/david-heinemeier-hansson-railsconf-2006-keynote-address">video</a> of that presentation and I got excited about CRUD(Create, Read, Update, Delete).  It pointed out that I&#8217;ve been making some controllers a little bloated.  I have coupled much functionality into one controller; when looking back, I clearly could have separated out some functionality.</p>
<p><span id="more-4"></span></p>
<h3>Learning to love <code>has_many :through</code> even more with CRUD</h3>
<p>One of the points about this is that the coupling often happens when you have models that are joined by a table, like in a HABTM(has_many_and_belongs_to) relationship.   The table that joins the two other tables as an amorphous name, whose role changes depending on which side of the join table you are looking. <code>has_many :through</code> fixes this ambiguity as it gives a name to a new model, and thus a new domain to think about creating clean uses of CRUD.  An example will make this much clearer.</p>
<h4>The non-CRUD way</h4>
<pre class="ruby" title="code">class Author &lt; ActiveRecord::Base
  has_and_belongs_to_many :books
end

class Book &lt; ActiveRecord::Base
  has_and_belongs_to_many :authors
end</pre>
<p>Let&#8217;s say I have a AuthorController and we want to add books to an author, or remove a book from an author, we could write the following.</p>
<pre class="ruby" title="code">class AuthorController &lt; ApplicationController
  def add_book
  end

  def remove_book
  end
end</pre>
<p>This doesn&#8217;t seem that harmless, but what if we wanted to add authors to a book?</p>
<pre class="ruby" title="code">class BookController &lt; ApplicationController
  def add_author
  end

  def remove_author
  end
end</pre>
<p>This does not follow the ways of DRY(Don&#8217;t Repeat Yourself), because depending on which side of the coin you are looking at, you have two different ways to accomplish pretty much the same thing.</p>
<h4>Enter <code>has_many :through</code></h4>
<p>DHH points out that if we can identify the link between Authors and Books, there will be a new a less ambiguous and more elegant way to think about this issue.  The link to identify here is Authorship.  Now let&#8217;s change our models some.</p>
<pre class="ruby" title="code">class Author &lt; ActiveRecord::Base
  has_many :authorships
  has_many :books, :through =&gt; :authorships
end

class Authorship &lt; ActiveRecord::Base
  belongs_to :author
  belongs_to :book
end

class Book &lt; ActiveRecord::Base
  has_many :authorships
  has_many :authors, :through =&gt; :authorships
end</pre>
<p>Because the link has been identified, a controller can be created with the ideals of CRUD put right into place.</p>
<pre class="ruby" title="code">class AuthorshipController &lt; ApplicationController
  def create
    #Authorship.create(:author_id =&gt; params[:author_id], :book =&gt; params[:book_id])
  end

  def destroy
  end
end</pre>
<p>Everything is right with the world again</p>
<h3>POST, GET, PUT, DELETE</h3>
<p>The other big thing that was mentioned was mapping CRUD functionality to HTTP(hypertext transfer protocol) method definitions: POST, GET, PUT, and DELETE respectively.  The HTTP method acts as the verb for what the controller should do.  Here is the basic outline, the comments are how things fit into the lovely world the the HTTP methods as verbs:</p>
<pre class="ruby" title="code">class SomethingController &lt; ApplicationController

  # POST /something
  def create
  end

  # GET /something/1
  def show
  end

  # PUT /something/1
  def update
  end

  # DELETE /people/1
  def destroy
  end

end</pre>
<p>Since browsers don&#8217;t use PUT or DELETE, Rails has a hack for this; it&#8217;s going to send hidden fields with the actions so that the mappings know how to handle everything.  Those methods aren&#8217;t the only ones that are apart of the CRUD; there are other ones that are common to most controllers:</p>
<pre class="ruby" title="code">class SomethingController &lt; ApplicationController

  # GET /something
  def index
  end

  # GET /something;new
  def new
  end

  # GET /something/1;edit
  def edit
  end

end</pre>
<p><code>index</code> is responsible for listing all of the somethings, while <code>new</code> and <code>edit</code> are how users first start their journey to creating or updating something.</p>
<p>Clarity in this layout isn&#8217;t the other reason to use this.  The other reason Rails is going to be embracing <a href="http://www.xfront.com/REST-Web-Services.html">REST(Representational State Transfer)</a> ideals into the way Rails will route your URLs.  More on this later when I dive deeper into it.</p>
<p>Because of this presentation, I&#8217;m thinking about how I&#8217;m doing my controllers.  I usually don&#8217;t have this type of setup, but I am definitely going to start using it.  From now on, I will not be afraid do use the command <code>script/generate controller Something index new create show edit update destroy</code> and start to think about controllers from a CRUD perspective rather than creating more monolithic controllers, and to be ready for when Rails 1.2 comes in full effect.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2006/08/29/looking-towards-rails-12-with-crud/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
