<?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>Pervasive Code</title>
	<atom:link href="http://www.pervasivecode.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pervasivecode.com/blog</link>
	<description>Jamie Flournoy's Software Development Blog</description>
	<lastBuildDate>Fri, 19 Mar 2010 05:37:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rails Migration Antipatterns and How To Fix Them</title>
		<link>http://www.pervasivecode.com/blog/2010/03/18/rails-migration-antipatterns-and-how-to-fix-them/</link>
		<comments>http://www.pervasivecode.com/blog/2010/03/18/rails-migration-antipatterns-and-how-to-fix-them/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 11:29:40 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=171</guid>
		<description><![CDATA[Migrations are one of the best features of Rails. Although some folks prefer pure SQL rather than Rails migration DSL, I don&#8217;t know of anyone who dislikes the idea of a versioned schema that can evolve in a controlled and repeatable fashion.
But because the concept of database migrations is such a powerful one, it&#8217;s tempting [...]]]></description>
			<content:encoded><![CDATA[<p>Migrations are one of the best features of Rails. Although some folks prefer pure SQL rather than Rails migration DSL, I don&#8217;t know of anyone who dislikes the idea of a versioned schema that can evolve in a controlled and repeatable fashion.</p>
<p>But because the concept of database migrations is such a powerful one, it&#8217;s tempting to jam any old change that affects the database into a new migration and run <code>rake db:migrate</code> to make it happen. I&#8217;ve been guilty of a bit of this in the past, and I&#8217;ve joined some projects that did other ugly things in migrations. In the process I&#8217;ve learned the hard way that there are some things you <b>must never do</b> in a migration or they will come back to haunt you later. Here they are.<br />
<span id="more-171"></span></p>
<h3>Antipattern: Require the Database to Exist Already</h3>
<p>In other words, the antipattern is for the first migration to depend on some tables and maybe even some data already being in the database.</p>
<p>I know that the original Rails blog video shows DHH using a MySQL admin tool to create the blog database interactively, but really you should be using migrations to create the schema programmatically from scratch.</p>
<p>If you&#8217;re already working on a project that didn&#8217;t do that, you can run <code>rake db:schema:dump</code> and look at db/schema.rb; it contains code that you can insert into a new migration to create the same schema in your development environment. If you&#8217;re using DB features that the design philosophy of ActiveRecord doesn&#8217;t agree with, such as triggers, and the schema.rb dump doesn&#8217;t include them (or if you just think the migration DSL is ugly and you like SQL DDL better), you can do a mysqldump / pg_dump / whateverdump and wrap a migration around the loading of that SQL file.</p>
<p>If you have a hybrid (you have to start with an old db dump and then migrate it so it becomes current), that&#8217;s gross, and you have a couple of options which are both pretty ugly. But they will work, and when you&#8217;re done the ugliness will be gone.</p>
<p>You could fight your way back to the oldest schema version by debugging the <code>self.down</code> methods and running <code>rake db:rollback</code> repeatedly until you can create a <code>00001_starting_db_schema.rb</code> migration, or you could just blow away all the migrations and use the highest schema version for a new migration that contains the output of a current <code>rake db:schema:dump</code>. It depends on how many copies of the database are out there with old schemas that would need to be brought up to date. Clearing out db/migrate and replacing it all with a single migration is cleaner, but if your production database is 5 migrations out of date you obviously can&#8217;t do that. But you could collapse it down to the one big-bang migration (as the oldest), plus the 5 pending schema changes. If you do it right, you can just deploy the new code and run <code>rake db:migrate</code> and everything will be fine. If not, well, you were testing it on a backup of the production database, right? :)</p>
<h3>Antipattern: Only Work Correctly With the Production Data</h3>
<p>What&#8217;s wrong with developers just making dumps of the production database and loading them locally?</p>
<p>First of all, it means that all schema changes have to start at the production database and work backwards to developers&#8217; sandboxed development environments. Hopefully this strikes you as a very stupid workflow.</p>
<p>Secondly, maybe your users don&#8217;t all want to get a message that says &#8220;test message foo bar sdfasdfasd bloopity bloop&#8221; when you&#8217;re testing your new alert system. Should you really be putting their data (passwords, contact info, etc.) at the mercy of your crummy new code?</p>
<p>You should be able to immediately generate an empty, clean database for development. <code>rake db:drop; rake db:create; rake db:migrate</code> should do this; <code>rake db:reset</code> should have the same result but should be faster since it doesn&#8217;t bother with each migration in sequence.</p>
<p>You should also be able to immediately generate any essential base data such as the initial admin user. The <a href="http://github.com/mbleigh/seed-fu">SeedFu</a> plugin does a good job here.</p>
<p>If you need some additional fake data to fiddle around with in your development environment, the <a href="http://github.com/ryanb/populator">Populator</a> gem is handy for mass-inserting a bunch of faux data, especially in conjunction with <a href="http://faker.rubyforge.org/">Faker</a>.</p>
<p>Note that the migrations should neither depend on nor contain actual data. They should just change the data model.</p>
<h3>Antipattern: Clean Up That Only Works on Production Data</h3>
<p>This is really a subset of the previous item but it&#8217;s worth considering as a special case.</p>
<p>If you want to fix some data that got slightly corrupted by some bad code that has been replaced, migrations aren&#8217;t a terrible way to accomplish that.</p>
<p>It&#8217;s not really what migrations are for, and a one-off rake task can do it just as well, but if you really want to, you can get away with it under one condition: you have to make your cleanup migration code succeed even if the database is empty (such as when a developer has just run <code>rake db:reset; rake db:migrate</code>).</p>
<h3>Antipattern: Load Data</h3>
<p>The <a href="http://github.com/ryanb/populator">populator</a> gem is good for initial, mandatory data. The <a href="http://github.com/notahat/machinist">machinist</a> gem is good for synthetic test data. Delete db/fixtures and everything in it. Fixtures are evil.</p>
<p>Wrap a rake task around the &#8220;get my development database ready&#8221; concept. This task should start with the &#8220;get my empty production database ready&#8221; task (or some subset of that which is appropriate for developer use).</p>
<p>If you need to load arbitrary data now and then, write an importer. Do this as a rake task, or a web UI to a bulk data importer feature. Better yet, make a web UI in your admin area which is just a wrapper around the rake task that bulk imports data. Then delegate the bulk importing to your customers so your admins can do real admin work. But don&#8217;t load data in a migration.</p>
<h3>Antipattern: Use Rails Models in the Migration</h3>
<p>Models evolve, but old migrations don&#8217;t change (nor should they). So when you wrote a migration that used a model, it used the old version of the model code. Then a year later the model has evolved, and the new validations on first_name and last_name fail because it used to be full_name, and that old migration that hasn&#8217;t changed has stopped working. It depended on something that did change, incompatibly.</p>
<p>For rockstar points, in your continuous integration environment you should run <code>rake db:drop; rake db:create; rake db:migrate</code> to make sure that this can never happen.</p>
<p>But if it has already happened, rip out the model code and replace it with Rails DSL code, with execute statements containing raw SQL code, or (if you feel like a Ruby rockstar) declare new, stripped down model classes inside your migration class that will act as stand-ins for the limited needs of the migration. See <a href="http://toolmantim.com/thoughts/migrating_with_models">Migrating with Models</a> for more on how to do this last trick.</p>
<h3>Conclusion</h3>
<p>You should always be able to do this in every Rails environment that your application has: <code>rake db:drop; rake db:create; rake db:migrate; rake db:reset</code></p>
<p>At this point you should then be able to run <code>rake db:test:prepare</code> and then <code>rake spec</code> or <code>rake test</code> or whatever and have it work.</p>
<p>If any part of that process fails, you are missing out on the benefits of using Rails migrations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2010/03/18/rails-migration-antipatterns-and-how-to-fix-them/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making Bundler 0.8.5 install Nokogiri on Leopard with a newish libxml</title>
		<link>http://www.pervasivecode.com/blog/2010/03/17/making-bundler-0-8-5-install-nokogiri-on-leopard-with-a-newish-libxml/</link>
		<comments>http://www.pervasivecode.com/blog/2010/03/17/making-bundler-0-8-5-install-nokogiri-on-leopard-with-a-newish-libxml/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 01:03:34 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=166</guid>
		<description><![CDATA[Nokogiri on a standard installation of Leopard is complain-y about a couple of old libraries:
&#8220;HI.  You&#8217;re using libxml2 version 2.6.16 which is over 4 years old and has plenty of bugs.  We suggest that for maximum HTML/XML parsing pleasure, you upgrade your version of libxml2 and re-install nokogiri.  If you like using [...]]]></description>
			<content:encoded><![CDATA[<p>Nokogiri on a standard installation of Leopard is complain-y about a couple of old libraries:</p>
<p>&#8220;HI.  You&#8217;re using libxml2 version 2.6.16 which is over 4 years old and has plenty of bugs.  We suggest that for maximum HTML/XML parsing pleasure, you upgrade your version of libxml2 and re-install nokogiri.  If you like using libxml2 version 2.6.16, but don&#8217;t like this warning, please define the constant I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 before requring nokogiri.&#8221;</p>
<p>Aaron Kalin <a href="http://martinisoftware.com/2009/07/31/nokogiri-on-leopard.html">figured out how to fix this if you&#8217;re installing nokogiri as a system gem</a>, but I want to use Bundler and keep my system gems down to the bare minimum. I figured out how to do this under Bundler 0.8.5.<br />
<span id="more-166"></span></p>
<p>The gems I have installed via plain old &#8220;gem install xxx&#8221; are bundler08 -v0.8.5, rake -v0.8.7, and ZenTest -v4.2.1. That&#8217;s it. Everything else is under vendor/bundler. I&#8217;m using <a href="http://rvm.beginrescueend.com/">RVM</a> and REE 1.8.7 2009.10 as described in <a href="http://www.pervasivecode.com/blog/2010/03/16/using-rvm-to-install-ree-1-8-7-2009-10/">my previous post</a>.</p>
<p>Here is a quick summary of Aaron&#8217;s blog post; this will update your libxml and libxslt libraries to reasonably recent versions. (Be my guest if you want to use libxml 2.7.7 and libxslt 1.1.26, which are current as of today, but I didn&#8217;t test those myself.)</p>
<pre>
mkdir -p ~/Downloads/XML
cd ~/Downloads/XML
curl -O ftp://xmlsoft.org/libxml2/libxml2-2.7.3.tar.gz
curl -O ftp://xmlsoft.org/libxml2/libxslt-1.1.24.tar.gz
tar xzf libxml2-2.7.3.tar.gz
tar xzf libxslt-1.1.24.tar.gz
cd ~/Downloads/XML/libxml2-2.7.3
./configure
nice make
sudo make install
cd ~/Downloads/XML/libxslt-1.1.24
./configure --with-libxml-prefix=/usr/local --with-libxml-include-prefix=/usr/local/include --with-libxml-libs-prefix=/usr/local/lib
nice make
sudo make install
</pre>
<p>Now make this YAML file and call it bundler_build_options_macosx_leopard.yml and put it in your RAILS_ROOT:</p>
<pre>
nokogiri:
  xml2-include: /usr/local/include/libxml2
  xml2-lib: /usr/local/lib
</pre>
<p>Then, to build nokogiri so that it uses the new libs you just installed:</p>
<pre>
gem bundle --build-options bundler_build_options_macosx_leopard.yml
</pre>
<p>You should now have nokogiri installed under vendor/bundler/ruby/1.8/gems, and when you run your app it should not complain about the version of libxml2 anymore.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2010/03/17/making-bundler-0-8-5-install-nokogiri-on-leopard-with-a-newish-libxml/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using RVM to install REE 1.8.7-2009.10</title>
		<link>http://www.pervasivecode.com/blog/2010/03/16/using-rvm-to-install-ree-1-8-7-2009-10/</link>
		<comments>http://www.pervasivecode.com/blog/2010/03/16/using-rvm-to-install-ree-1-8-7-2009-10/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 01:48:53 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=160</guid>
		<description><![CDATA[I&#8217;m trying out Ruby Version Manager this week, and my first impression is that this is some cool technology. But I wasn&#8217;t able to figure out how to get it to install an older version of REE to get around this bug (the &#8220;Marshal.load reentered at marshal_load&#8221; issue).
Igor P&#8217;s solution is correct (just install REE [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m trying out <a href="http://rvm.beginrescueend.com/">Ruby Version Manager</a> this week, and my first impression is that this is some cool technology. But I wasn&#8217;t able to figure out how to get it to install an older version of REE to get around <a href="http://groups.google.com/group/emm-ruby/browse_thread/thread/ba3ef0a931618052">this bug</a> (the &#8220;Marshal.load reentered at marshal_load&#8221; issue).</p>
<p>Igor P&#8217;s solution is correct (just install REE 1.8.7-2009.10), but it took a little fiddling to figure out how to get RVM to use the older version of REE. Here&#8217;s how to do it:</p>
<pre>
cd ~/.rvm/archives
wget -q http://rubyforge.org/frs/download.php/66162/ruby-enterprise-1.8.7-2009.10.tar.gz
rvm install ree-1.8.7-2009.10
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2010/03/16/using-rvm-to-install-ree-1-8-7-2009-10/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Karmic on Xen with Bad /etc/fstab = PAIN</title>
		<link>http://www.pervasivecode.com/blog/2010/02/07/karmic-on-xen-with-bad-etcfstab-pain/</link>
		<comments>http://www.pervasivecode.com/blog/2010/02/07/karmic-on-xen-with-bad-etcfstab-pain/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 00:32:20 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[servers]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=156</guid>
		<description><![CDATA[Argh! I spent about 5 hours yesterday troubleshooting a failed Ubuntu Jaunty -> Karmic (9.04->9.10) upgrade. It worked fine until I rebooted and then failed to boot. Here&#8217;s how I fixed it.

It failed to boot, saying this:

One or more mounts listed in /etc/fstab cannot yet be mounted
/ : waiting for /dev/xvda1
/tmp : waiting for (null)
/swap [...]]]></description>
			<content:encoded><![CDATA[<p>Argh! I spent about 5 hours yesterday troubleshooting a failed Ubuntu Jaunty -> Karmic (9.04->9.10) upgrade. It worked fine until I rebooted and then failed to boot. Here&#8217;s how I fixed it.<br />
<span id="more-156"></span><br />
It failed to boot, saying this:<br />
<code><br />
One or more mounts listed in /etc/fstab cannot yet be mounted<br />
/ : waiting for /dev/xvda1<br />
/tmp : waiting for (null)<br />
/swap : waiting for /dev/xvda9<br />
</code></p>
<p>I tried a lot of stuff and finally solved it. My solution is on the Ubuntu Forum, here: <a href="http://ubuntuforums.org/showpost.php?p=8789500&#038;postcount=37">One or more of the mounts listed in /etc/fstab/ cannot yet be mounted (Karmic)</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2010/02/07/karmic-on-xen-with-bad-etcfstab-pain/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ubuntu 9.10 (Jaunty Jackalope) upgrade notes</title>
		<link>http://www.pervasivecode.com/blog/2010/02/06/ubuntu-910-jaunty-jackalope-upgrade-notes/</link>
		<comments>http://www.pervasivecode.com/blog/2010/02/06/ubuntu-910-jaunty-jackalope-upgrade-notes/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 04:24:57 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[servers]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=154</guid>
		<description><![CDATA[Once again Ubuntu Linux proves itself to be easy to upgrade. Going from 9.04 to 9.10 (one release newer, since their numbering is bsaed on dates) was easy, but included the standard sprinkling of manual re-customization that I&#8217;ve come to expect from Debian based systems.

I did the Network Upgrade for Servers.
I had to re-customize these [...]]]></description>
			<content:encoded><![CDATA[<p>Once again Ubuntu Linux proves itself to be easy to upgrade. Going from 9.04 to 9.10 (one release newer, since their numbering is bsaed on dates) was easy, but included the standard sprinkling of manual re-customization that I&#8217;ve come to expect from Debian based systems.<br />
<span id="more-154"></span><br />
I did the <a href="https://help.ubuntu.com/community/KarmicUpgrades#Network%20Upgrade%20for%20Ubuntu%20Servers%20%28Recommended%29">Network Upgrade for Servers</a>.</p>
<p>I had to re-customize these files since I&#8217;m not running with 100% default configuration:</p>
<p>/etc/monit/monit<br />
/etc/monit/monitrc<br />
/etc/dovecot/dovecot.conf<br />
/etc/apache2/apache2.conf<br />
/etc/php/apache2/php.ini</p>
<p>I basically did a manual diff side by side in Emacs and copied my changes over into the new config files. Reboot, no problems. Nice.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2010/02/06/ubuntu-910-jaunty-jackalope-upgrade-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tidier HTTP error responses in Rails controllers</title>
		<link>http://www.pervasivecode.com/blog/2009/07/27/tidier-http-error-responses-in-rails-controllers/</link>
		<comments>http://www.pervasivecode.com/blog/2009/07/27/tidier-http-error-responses-in-rails-controllers/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 04:58:10 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=122</guid>
		<description><![CDATA[Rails provides some flexible and fairly short controller methods for responding with an HTTP error code. Given that controllers are complicated enough by nature, I&#8217;m always looking for ways to DRY them up and make the code easy to understand. So here are some controller methods that make it really easy to provide correct HTTP [...]]]></description>
			<content:encoded><![CDATA[<p>Rails provides some flexible and fairly short controller methods for responding with an HTTP error code. Given that controllers are complicated enough by nature, I&#8217;m always looking for ways to DRY them up and make the code easy to understand. So here are some controller methods that make it really easy to provide correct HTTP error responses to clients.<br />
<span id="more-122"></span></p>
<p>Put this in config/initializers/respond_with_http_errors.rb :</p>
<pre><code>
class ActionController::Base

  protected

  # Display a "400 Bad Request" response based on a determination made by application logic.
  def respond_bad_request(message = "Your request was invalid.", options = {})
    respond_with_error(400, 'Bad Request', message, options)
  end

  # Display a "403 Forbidden" response based on a determination made by application logic.
  def respond_forbidden(message = "You are not authorized to access that document.", options = {})
    respond_with_error(403, 'Forbidden', message, options)
  end

  # Display a "404 Not Found" response based on a determination made by application logic.
  def respond_not_found(message = "The document you requested doesn't exist.", options = {})
    respond_with_error(404, 'Not Found', message, options)
  end

  def respond_with_error(status_code, status_name, message, options)
    flash[:error] = message
    defaults = { :status =&gt; status_code }
    respond_to do |format|
      format.html { defaults.merge!(:inline =&gt; "&lt;h1&gt;#{status_name}&lt;/h1&gt;&lt;p&gt;&lt;%=h flash[:error] %&gt;&lt;/p&gt;", :layout =&gt; true) }
      format.json { defaults.merge!(:json =&gt; "#{status_name}: #{message}".to_json) }
    end
    render defaults.merge(options)
  end

end
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2009/07/27/tidier-http-error-responses-in-rails-controllers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Proper Error Handling in Rails Controllers</title>
		<link>http://www.pervasivecode.com/blog/2009/07/27/proper-error-handling-in-rails-controllers/</link>
		<comments>http://www.pervasivecode.com/blog/2009/07/27/proper-error-handling-in-rails-controllers/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 04:56:24 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=131</guid>
		<description><![CDATA[Rails controllers can get out of hand if you&#8217;re not very careful. Skinny Controller Fat Model is a great start. But what about handling errors? Isn&#8217;t it enough to just let Rails catch your exception and show a 500 Server Error page?
No, it&#8217;s not. Falling back on 500 Server Error for everything outside of the [...]]]></description>
			<content:encoded><![CDATA[<p>Rails controllers can get out of hand if you&#8217;re not very careful. <a href="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model">Skinny Controller Fat Model</a> is a great start. But what about handling errors? Isn&#8217;t it enough to just let Rails catch your exception and show a 500 Server Error page?</p>
<p>No, it&#8217;s not. Falling back on 500 Server Error for everything outside of the &#8220;happy path&#8221; through your code is sloppy coding.<br />
<span id="more-131"></span><br />
I say this for three reasons:</p>
<p><b>The 5xx response codes are for server-side errors.</b></p>
<p>If the code is working properly but the request is wrong (no such document, an invalid request, or insufficient user privileges for the requested operation), the 4xx response codes are more appropriate. You should only return a 5xx series error message when your application is broken.</p>
<p><b>A generic &#8220;oops&#8221; page hides the cause of the error from the user.</b></p>
<p>Maybe the user followed a bad link, that goes to a document that has been deleted. They should see a 404 Not Found response, not a 500 Server Error. This is especially important if the user agent is a web service client actively under development. And remember that you can still customize the response body, so a 404 can show a search or site map to help them find the content that was Not Found, and a 403 might suggest that the user upgrade their subscription so they can see the Forbidden content.</p>
<p><b>Failing to trap bogus input is a recipe for a security vulnerability.</b></p>
<p>Rails will detect ridiculous input in many cases, but it doesn&#8217;t know about your business rules.</p>
<p>For example, what if someone fires up Firebug and figures out your API, and deletes her ex-boyfriend&#8217;s account because you forgot to check that the ID being deleted matches the currently logged in user? Oops.</p>
<p>You know who else got pwn3d because they put their security rules in the client? AOL. Don&#8217;t be like AOL. They got <a href="http://en.wikipedia.org/wiki/AOHell">pwn3d big time</a>. Make sure your controller handles the &#8220;unhappy path&#8221; too, and responds appropriately.</p>
<p>You make sure your controller handles these cases by writing tests, and checking that the response is correct. To do this, you need unambiguous errors that clearly explain what the controller didn&#8217;t like about the request. </p>
<p>Your controller tests should <i>never</i> expect an exception. Exceptions belong inside the application (such as between a model and a controller), not at the boundary.</p>
<p>Instead, you should be using something like <code>assert_response :bad_request</code>, and providing a useful textual message in the page that will help the user understand what they did wrong.</p>
<p>A nice bonus of using the right HTTP error responses is that your tests are more readable. And since controller tests tend to be really verbose already (mine tend to be ~10x as many LOC as the controller itself), the extra readability is really nice.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2009/07/27/proper-error-handling-in-rails-controllers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Cucumber step to test for a YM4R Google Map</title>
		<link>http://www.pervasivecode.com/blog/2009/07/27/a-cucumber-step-to-test-for-a-ym4r-google-map/</link>
		<comments>http://www.pervasivecode.com/blog/2009/07/27/a-cucumber-step-to-test-for-a-ym4r-google-map/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 03:34:46 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=116</guid>
		<description><![CDATA[I had some problems with a view in a Rails app that was conditionally hiding a Google Map that was generated using the YM4R plugin. I don&#8217;t usually test views in unit tests, and this logic depended on a particular situation with the data behind the view, so I decided that this would be a [...]]]></description>
			<content:encoded><![CDATA[<p>I had some problems with a view in a Rails app that was conditionally hiding a Google Map that was generated using the <a href="http://ym4r.rubyforge.org/">YM4R</a> plugin. I don&#8217;t usually test views in unit tests, and this logic depended on a particular situation with the data behind the view, so I decided that this would be a good candidate for a Cucumber feature.</p>
<p>Here&#8217;s the Cucumber step implementation I wrote.<br />
<span id="more-116"></span><br />
Put this in a new file called features/step_definitions/ym4r_gm_steps.rb :</p>
<pre>
# Steps for use with the ym4r_gm plugin

Then /^I should see a Google Map$/i do
  within 'head' do
    ['http://maps.google.com/maps', '/javascripts/ym4r-gm.js'].each do |js_url|
      assert_have_xpath "//script[starts-with(@src, '#{js_url}')]"
    end
  end
  assert_select '#map_div'
end
</pre>
<p>This assumes that you let the plugin installer put the .js file in the default location, and that you have your Google Map inside #map_div.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2009/07/27/a-cucumber-step-to-test-for-a-ym4r-google-map/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fancier Stubbing of GeoKit for Rails unit tests</title>
		<link>http://www.pervasivecode.com/blog/2009/07/23/fancier-stubbing-of-geokit-for-rails-unit-tests/</link>
		<comments>http://www.pervasivecode.com/blog/2009/07/23/fancier-stubbing-of-geokit-for-rails-unit-tests/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 00:00:44 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[process]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[servers]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=108</guid>
		<description><![CDATA[I&#8217;m working on a Rails app that uses the ym4r_gm plugin, getting Google to do the geocoding for Thentic. I liked the idea of stubbing the web service call, because all those calls to an external service add up to over 20 seconds of test suite run time(!). That&#8217;s almost half of the 50 second [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a Rails app that uses the ym4r_gm plugin, getting Google to do the geocoding for <a href="http://www.thentic.com/">Thentic</a>. I liked the idea of stubbing the web service call, because all those calls to an external service add up to over 20 seconds of test suite run time(!). That&#8217;s almost half of the 50 second run time of my unit tests (and 50 seconds is much too long for a unit test suite).</p>
<p>I found a good starting point at <a href="http://beardendesigns.com/blogs/permalink/55">geokit stubbing for faster tests</a>. I also wanted a way to stub a geocoding failure, and a way to prevent any unit tests from using the real geocoding web service.</p>
<p>Here&#8217;s how I did it.<br />
<span id="more-108"></span><br />
In test/test_helper.rb, add this:</p>
<pre>
class ActiveSupport::TestCase
  def setup
    GeoKit::Geocoders::MultiGeocoder.stubs(:geocode).raises(RuntimeError,
      'Use mock_geocoding_success! or mock_geocoding_failure! in your test')
  end

  def mock_geocoding_success!
    geocode_payload = GeoKit::GeoLoc.new(:lat => 123.456, :lng => 123.456)
    geocode_payload.success = true
    GeoKit::Geocoders::MultiGeocoder.expects(:geocode).returns(geocode_payload)
  end

  def mock_geocoding_failure!
    geocode_payload = GeoKit::GeoLoc.new
    geocode_payload.success = false
    GeoKit::Geocoders::MultiGeocoder.expects(:geocode).returns(geocode_payload)
  end
end
</pre>
<p>What this does is to force you to choose either one of those mock_geocoding methods before you call the geocode method. To me this seems like a good idea since the integration tests that exercise the full application stack should probably be written using Cucumber and Webrat (which is what I&#8217;m using).</p>
<p>You will probably want to merge my one-line setup method into your existing setup code in test_helper, if any. Also note that this uses <a href="http://mocha.rubyforge.org/">Mocha</a> for mocking.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2009/07/23/fancier-stubbing-of-geokit-for-rails-unit-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ubuntu 8.10 and 9.04 (Intrepid Ibex and Jaunty Jackalope) upgrade notes</title>
		<link>http://www.pervasivecode.com/blog/2009/05/30/ubuntu-jaunty-jackalope-upgrade-notes/</link>
		<comments>http://www.pervasivecode.com/blog/2009/05/30/ubuntu-jaunty-jackalope-upgrade-notes/#comments</comments>
		<pubDate>Sat, 30 May 2009 22:54:18 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[servers]]></category>
		<category><![CDATA[xen]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=96</guid>
		<description><![CDATA[I&#8217;m at WordCamp San Francisco today and decided that running a year old version of WordPress (on a year old version of Ubuntu Linux) was undesirable. So, with the confidence that comes from many relatively easy Ubuntu OS upgrades, I charged ahead. For (I think) the second time ever, things went badly. Here&#8217;s what I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m at <a href="http://2009.sf.wordcamp.org/">WordCamp San Francisco</a> today and decided that running a year old version of WordPress (on a year old version of <a href="http://www.ubuntu.com/">Ubuntu Linux</a>) was undesirable. So, with the confidence that comes from many relatively easy Ubuntu OS upgrades, I charged ahead. For (I think) the second time ever, things went badly. Here&#8217;s what I did and how I fixed it.<br />
<span id="more-96"></span><br />
First, I had to figure out what release of Ubuntu was currently installed:<br />
<code>lsb_release -a</code></p>
<p>I was on &#8220;hardy&#8221;, a.k.a. the <a href="https://wiki.ubuntu.com/HardyHeron/">Hardy Heron</a> release, a.k.a. Ubuntu 8.04 LTS.</p>
<p>I had not bothered to install <a href="https://wiki.ubuntu.com/IntrepidIbex/">Ubuntu 8.10 / &#8220;Intrepid Ibex&#8221;</a> because I didn&#8217;t have a reason to when it was release. I now wanted to upgrade to <a href="https://wiki.ubuntu.com/JauntyJackalope">Ubuntu 9.04 &#8220;Jaunty Jackalope&#8221;</a> which has <a href="http://wordpress.org/">WordPress</a> 2.7.1, the current release (as of today).</p>
<p>The way to upgrade from 8.04 to 9.04 is to upgrade to 8.10 first. So I did that:</p>
<p><a href="https://help.ubuntu.com/community/IntrepidUpgrades#Network%20Upgrade%20for%20Ubuntu%20Servers%20(Recommended)">Intrepid Upgrades: Network Upgrade for Ubuntu Servers</a> worked really well. I had to do a little bit of manual file merging as usual (I still don&#8217;t understand why dpkg can&#8217;t merge changes from the old file into a new file) but that was it. Easy!</p>
<p>When I rebooted the VPS, it kernel panicked: can&#8217;t mount the root filesystem. Oh crap. /dev/xvda1 is missing? Really? I told the VPS to hard reboot and it came up fine. But that&#8217;s a little scary. (I think this is something more related to my VPS hosting provider than Ubuntu, but I haven&#8217;t yet upgraded my laptop VMWare Ubuntu VPS&#8217;s yet so I&#8217;m not sure.)</p>
<p>The second stage didn&#8217;t go so well. I did the same sort of simple upgrade: the Jaunty <a href="http://www.ubuntu.com/getubuntu/upgrading#Network%20Upgrade%20for%20Ubuntu%20Servers%20%28Recommended%29">Network Upgrade for Ubuntu Servers</a> instructions are the same as the ones for Intrepid. Upgrade, edit a couple of config files, reboot. Kernel panic again, same reason, reboot. Should work, right?</p>
<p>It booted, but had no network access. I was able to log in via my VPS hosting provider&#8217;s SSH remote console feature, so I was able to see that /etc/init.d/networking was failing to start. It was the same problem that&#8217;s described in <a href="http://www.fs3.ph/article/ubuntu-904-in-an-openvz-ve">Ubuntu 9.04 in an OpenVZ VE</a>. Adding that one line to <code>/etc/init.d/networking</code> fixed the problem. Reboot, all better.</p>
<p>So if you&#8217;re doing this upgrade on a VPS, make sure you&#8217;ve added that little 1-line hack after you do the Jaunty upgrade and before you reboot.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2009/05/30/ubuntu-jaunty-jackalope-upgrade-notes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
