<?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>One Man's Walk in work &#187; rails</title>
	<atom:link href="http://onemanswalk.com/work/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://onemanswalk.com/work</link>
	<description>jeremy lightsmith on agile, ruby, and consulting</description>
	<lastBuildDate>Wed, 04 May 2011 04:29:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Clickable Stack Traces on your Rails Error Page</title>
		<link>http://onemanswalk.com/work/2008/04/30/clickable-stack-traces-on-your-rails-error-page/</link>
		<comments>http://onemanswalk.com/work/2008/04/30/clickable-stack-traces-on-your-rails-error-page/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 18:29:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	Wouldn&#8217;t it be nice if when an error happened in your application, you could not only see the stack trace, but click on a line and jump to the offending code?  This is not groundbreaking stuff, I know, I had this like 10 years ago in C++ and later Java fat clients, and I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[	<p>Wouldn&#8217;t it be nice if when an error happened in your application, you could not only see the stack trace, but click on a line and jump to the offending code?  This is not groundbreaking stuff, I know, I had this like 10 years ago in C++ and later Java fat clients, and I&#8217;m sure other languages &#038; IDEs had it too &#8211; but somehow in moving to writing web apps in Ruby, I lost it.</p>

	<p>I want it back damnit!</p>

	<p>Turns out it&#8217;s pretty easy to get back (at least it is if you use textmate) &#8211; check it out.</p>

	<h2>Custom Error Page</h2>

	<p>First, to get a custom error page for your project, add something like this to your application.rb :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> rescue_action_locally<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  render <span style="color:#ff3333; font-weight:bold;">:template</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;application/public_error&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">alias</span> rescue_action_in_public render_action_locally</pre></div></div>
</p>



	<p>Note, if you&#8217;re using exception notifiable, you probably want to change the last line to something like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">alias</span> render_404 rescue_action_locally
<span style="color:#9966CC; font-weight:bold;">alias</span> render_500 rescue_action_locally</pre></div></div>
</p>



	<p>We use markaby, so our public_error template looks something like this; it&#8217;s probably a good idea to keep this simple and not use a layout, just in case the error came from the layout :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">html <span style="color:#9966CC; font-weight:bold;">do</span>
  head <span style="color:#9966CC; font-weight:bold;">do</span>
    title action_name
    stylesheet_link_tag <span style="color:#996600;">'error'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  body <span style="color:#9966CC; font-weight:bold;">do</span>
    div.<span style="color:#9900CC;">error</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      div.<span style="color:#9900CC;">message</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        h1 <span style="color:#996600;">&quot;Whoops&quot;</span>
&nbsp;
        p <span style="color:#996600;">&quot;We detected an error.  Don't worry, though, 
we've been notified and we're on it.&quot;</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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>




	<h2>Adding a Stack Trace w/ Links to the Error Page</h2>

	<p>So, it would be helpful to us for our error page to tell us more in our development and staging environments.  We do use exception notifiable, so we don&#8217;t actually need or want it to say anything else to a real user in production.  Adding this to our template, it now looks like this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">html <span style="color:#9966CC; font-weight:bold;">do</span>
  head <span style="color:#9966CC; font-weight:bold;">do</span>
    title action_name
    stylesheet_link_tag <span style="color:#996600;">'error'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  body <span style="color:#9966CC; font-weight:bold;">do</span>
    div.<span style="color:#9900CC;">error</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      div.<span style="color:#9900CC;">message</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        h1 <span style="color:#996600;">&quot;Whoops&quot;</span>
&nbsp;
        p <span style="color:#996600;">&quot;We detected an error.  Don't worry, though, 
we've been notified and we're on it.&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> RAILS_ENV != <span style="color:#996600;">'production'</span>
        div.<span style="color:#9900CC;">stack_trace</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          h2 <span style="color:#996600;">&quot;Stack Trace&quot;</span>
          div <span style="color:#006600; font-weight:bold;">&#123;</span> link_to_code $!.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>, <span style="color:#996600;">&quot;
&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
          hr
          div <span style="color:#006600; font-weight:bold;">&#123;</span> link_to_code $!.<span style="color:#9900CC;">backtrace</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;
&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</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>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>What&#8217;s that &#8220;link_to_code&#8221; method in there?</p>

	<p>It&#8217;s a method in application_helper that replaces any path with a textmate url to open up that file on your local system and jump to the offending line.  Check it out :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> link_to_code<span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>
  text.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</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>\w\.<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;">/</span><span style="color:#006600; font-weight:bold;">&#91;</span>\w\<span style="color:#006600; font-weight:bold;">/</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;">&#41;</span>\:<span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>match<span style="color:#006600; font-weight:bold;">|</span>
    file = $<span style="color:#006666;">1</span>.<span style="color:#9900CC;">starts_with</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? $<span style="color:#006666;">1</span> : <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, $<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    link_to match, <span style="color:#996600;">&quot;txmt://open?url=file://#{file}&amp;line=#{$2}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>That&#8217;s it.  Suddenly, stack traces are friendly again!</p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2008/04/30/clickable-stack-traces-on-your-rails-error-page/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Markaby w/ Rails 2.0.1</title>
		<link>http://onemanswalk.com/work/2007/12/13/using-markaby-w-rails-2-0-1/</link>
		<comments>http://onemanswalk.com/work/2007/12/13/using-markaby-w-rails-2-0-1/#comments</comments>
		<pubDate>Thu, 13 Dec 2007 19:07:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[markaby]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	Rails 2.0.1 is out, in general, it&#8217;s pretty nice.  Markaby doesn&#8217;t play too nice w/ it.  Markaby requires every named route to have a .to_s on the end of it.  Yuck.

	After migrating my 3rd project to rails 2.0.1, I got sick of adding .to_s to each named routes, and did some digging.

	Apparently, [...]]]></description>
			<content:encoded><![CDATA[	<p>Rails 2.0.1 is out, in general, it&#8217;s pretty nice.  Markaby doesn&#8217;t play too nice w/ it.  Markaby requires every named route to have a .to_s on the end of it.  Yuck.</p>

	<p>After migrating my 3rd project to rails 2.0.1, I got sick of adding .to_s to each named routes, and did some digging.</p>

	<p>Apparently, somehow a string coming from a named route is not <em>exactly</em> a string.  Sure</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  apples_path<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">class</span> == <span style="color:#CC0066; font-weight:bold;">String</span></pre></div></div>
</p>



	<p>but</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#CC0066; font-weight:bold;">String</span> === apples_path<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>
</p>



	<p>is <span class="caps">NOT</span> true.  I couldn&#8217;t quite track down why this was but it eventually leads to a</p>

<pre>
ActionView::TemplateError (undefined method `string_path' for #) on line #10 of messages/index.mab:

actionpack-2.0.1/lib/action_controller/polymorphic_routes.rb:27:in `send!'
actionpack-2.0.1/lib/action_controller/polymorphic_routes.rb:27:in `polymorphic_url'
actionpack-2.0.1/lib/action_controller/polymorphic_routes.rb:31:in `polymorphic_path'
actionpack-2.0.1/lib/action_view/helpers/url_helper.rb:79:in `url_for'
actionpack-2.0.1/lib/action_view/helpers/url_helper.rb:144:in `link_to'
...
</pre>

	<p>After an hour of trying in vain to find the source of the problem, I found a simple hack that fixes this.  Should have thought of this before, but add this code to your application_helper.rb</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> string_path<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>
    <span style="color:#CC0066; font-weight:bold;">string</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>If anyone else has a better solution, please pass it over!</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/12/13/using-markaby-w-rails-2-0-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Daemonizing a Ruby Script in Rails</title>
		<link>http://onemanswalk.com/work/2007/09/20/daemonizing-a-ruby-script-in-rails/</link>
		<comments>http://onemanswalk.com/work/2007/09/20/daemonizing-a-ruby-script-in-rails/#comments</comments>
		<pubDate>Thu, 20 Sep 2007 18:28:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	This took way longer than it should have, so I thought I&#8217;d jot down what I did so it might take less time next time.

	1. Install the daemons gem

	
sudo gem install daemons




	2. Presumably you have a script that looks something like this already

	
#!/usr/bin/env ruby
require File.dirname&#40;__FILE__&#41; + &#34;/. ./config/environment&#34;
&#160;
SchedulerDaemon.new.run




	This is a scheduler script that lives in [...]]]></description>
			<content:encoded><![CDATA[	<p>This took <em>way</em> longer than it should have, so I thought I&#8217;d jot down what I did so it might take less time next time.</p>

	<p>1. Install the <a href="http://daemons.rubyforge.org" title="">daemons</a> gem</p>

	<p>
<div class="wp_syntax"><div class="code"><pre>sudo gem install daemons</pre></div></div>
</p>



	<p>2. Presumably you have a script that looks something like this already</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;/. ./config/environment&quot;</span>
&nbsp;
SchedulerDaemon.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">run</span></pre></div></div>
</p>



	<p>This is a scheduler script that lives in the scripts directory of a rails project.</p>

	<p>3. You&#8217;re going to take this code and wrap it in daemon stuff, like so</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
RAILS_ROOT = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/. .'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
gem <span style="color:#996600;">'daemons'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'daemons'</span>
&nbsp;
Daemons.<span style="color:#9900CC;">run_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;scheduler&quot;</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:log_output</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:dir_mode</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:normal</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:dir</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{RAILS_ROOT}/log&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, <span style="color:#996600;">&quot;config/environment&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  SchedulerDaemon.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">run</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>The really hard part for me was debugging it.  Which meant figuring out how to get logging going.  With this code, you can just tail &#8220;log/scheduler.output&#8221; and see the contents of any puts in the code.  Once I started doing that, everything else was easy.</p>

	<p><span class="caps">RAILS</span>_ROOT has to be set first, because daemons changes the current working directory.  Also, I use the log dir, because it&#8217;s shared by capistrano, so I can deploy, then stop / start my scheduler and not worry about losing my first pid file &#8211; plus that&#8217;s where logs are supposed to go.</p>

	<p>4. To make my life just a little easier, I also added some debugging statements</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">Daemons.<span style="color:#9900CC;">run_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;scheduler&quot;</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:log_output</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:dir_mode</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:normal</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:dir</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{RAILS_ROOT}/log&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, <span style="color:#996600;">&quot;config/environment&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;starting scheduler at #{Time.now} for #{RAILS_ENV}&quot;</span>
    SchedulerDaemon.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">run</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">ensure</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;ending scheduler at #{Time.now}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>This was actually pretty easy, and next time it will take 5 minutes to create a daemon.  Nice gem.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/09/20/daemonizing-a-ruby-script-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object Mother&#8230;in rails?</title>
		<link>http://onemanswalk.com/work/2007/03/06/object-mother-in-rails/</link>
		<comments>http://onemanswalk.com/work/2007/03/06/object-mother-in-rails/#comments</comments>
		<pubDate>Tue, 06 Mar 2007 16:58:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	That&#8217;s right, the circa 2000 pattern still makes sense today.  Use an &#8220;object mother&#8221; as a test factory to conveniently create objects for your unit tests to bang against.  It will often default values, or have different states in which to create objects.  For example, you might have a new_user, as well [...]]]></description>
			<content:encoded><![CDATA[	<p>That&#8217;s right, the circa 2000 pattern still makes sense today.  Use an &#8220;object mother&#8221; as a test factory to conveniently create objects for your unit tests to bang against.  It will often default values, or have different states in which to create objects.  For example, you might have a new_user, as well as a new_superuser and new_guest method all of which return users.</p>

	<p>Read about the <a href="http://www.martinfowler.com/bliki/ObjectMother.html" title="">original pattern</a></p>

	<p>But why, you ask, not just use rails&#8217; fixtures?  Glad you asked.</p>

	<ol>
		<li>it&#8217;s more intuitive and maintainable to setup the data you need right next to the test</li>
			<li>it&#8217;s easier to create just exactly the objects you need when you have test factory methods</li>
	</ol>

	<p>But don&#8217;t take my word for it.  Let&#8217;s look at some code.</p>

	<p>First off, what does this look like in ruby?  I am creating an &#8220;ObjectMother&#8221; module, which I then just mixin to my tests.  A test might then look like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">include</span> ObjectMother
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> test_delete_project
    project = new_project<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'foo'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    post <span style="color:#ff3333; font-weight:bold;">:delete</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> project.<span style="color:#9900CC;">id</span>
    assert_raise<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> Tag.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>project.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>The magic is in &#8220;new_project&#8221;.  It&#8217;s an entirely pragmatic construct.  If you pass it a string, it will set everything else to acceptable defaults, and use that string as a name.  It looks something like this.</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">module</span> ObjectMother
  <span style="color:#9966CC; font-weight:bold;">def</span> new_project<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
    options = <span style="color:#006600; font-weight:bold;">&#123;</span>:name <span style="color:#006600; font-weight:bold;">=&gt;</span> options<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC0066; font-weight:bold;">String</span>
    options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:url_name</span><span style="color:#006600; font-weight:bold;">&#93;</span> = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\W<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> !options.<span style="color:#9900CC;">has_key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url_name</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    Project.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>Here in project, it defaults the url_name (which must be unique) from the name you&#8217;ve given it.  However, you could also create a more custom project by running this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  new_project<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'garage'</span>, <span style="color:#ff3333; font-weight:bold;">:url_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'the_garage'</span>, <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foo'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>
</p>



	<p>This is how it works for projects, but it&#8217;s only purpose in life is to make my life easier and reduce the amount of code one has to type or read.  So each type of thing it creates works a little bit differently according to our needs<br />
&#8212;&#8212;<br />
h2. A more complicated example</p>

	<p>I was playing with ferret last week, and wrote a test that looked like this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> test_across_types
    project = new_project<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'rabbit holes'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    post = new_post<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:subject</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'a rabbit has a big head'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    user = new_user<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:display_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'rabbit head'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@search</span>.<span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'rabbit'</span>
    assert_find <span style="color:#006600; font-weight:bold;">&#91;</span>project, post, user<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@search</span>.<span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'rabbit head'</span>
    assert_find <span style="color:#006600; font-weight:bold;">&#91;</span>post, user<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>My thought process was something along the lines of :</p>

	<ol>
		<li>I want to test that my searcher works across types</li>
			<li>I need to create a project, post, and user that all have a term in them (in different places)</li>
			<li>I want to search for the term, and make sure i get all of them</li>
			<li>I want to search for a term that maybe 2 of them have and make sure I only get those 2</li>
	</ol>

	<p>Writing the test for this part literally took 30 seconds, I didn&#8217;t have to go lookup the fixtures or add a new fixture for my new case.  I also didn&#8217;t have to remember all the things that it takes to make a valid project or post or user.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/03/06/object-mother-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;m Using Ruby on Rails</title>
		<link>http://onemanswalk.com/work/2005/01/26/i-m-using-ruby-on-rails/</link>
		<comments>http://onemanswalk.com/work/2005/01/26/i-m-using-ruby-on-rails/#comments</comments>
		<pubDate>Wed, 26 Jan 2005 00:00:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[hosting]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	So it may look like the old site, but you are looking at brand spanking rewrite.

	OneMansWalk now sits on top of RubyOnRails.  And it&#8217;s nice.  Very, very nice.  For the first time I could actually see Ruby as something that I would actually ask a client to consider for a web application.

	It [...]]]></description>
			<content:encoded><![CDATA[	<p>So it may look <em>like</em> the old site, but you are looking at brand spanking rewrite.</p>

	<p>OneMansWalk now sits on top of <a href="http://rubyonrails.com/" title="">RubyOnRails</a>.  And it&#8217;s nice.  Very, very nice.  For the first time I could actually see Ruby as something that I would actually ask a client to consider for a web application.</p>

	<p>It still is file based which lets me use cvs as a way to work in a disconnected fashion.  However, I&#8217;ve started separating content from logic, so the site is now a project on <a href="http://rubyforge.org/" title="">RubyForge</a> at http://rubyforge.org/projects/blinki/ and doesn&#8217;t include any content <img src='http://onemanswalk.com/work/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>

	<p>I&#8217;ve had a lot of requests to get a site like this up and running for my friends, so next up I&#8217;m going to try to install it on one of their machines and then work on packaging it for non programmers.  I want to create a windows installer that installs apache, ruby, imagemagick, and this source code.  We&#8217;ll see how that goes.  It will be successful when my mom can install it <img src='http://onemanswalk.com/work/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>

	<p>One note : if you have a link to my rss feed, you&#8217;ll need to update it, the new link is</p>

	<p><strong>http://www.onemanswalk.com/wiki/rss</strong></p>































 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2005/01/26/i-m-using-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

