<?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 &#187; ruby</title>
	<atom:link href="http://www.pervasivecode.com/blog/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pervasivecode.com/blog</link>
	<description>Jamie Flournoy's Software Development Blog</description>
	<lastBuildDate>Mon, 26 Jul 2010 05:29:53 +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>Making Bundler 1.0.0.beta.10 install Nokogiri on Leopard with a newish libxml</title>
		<link>http://www.pervasivecode.com/blog/2010/07/25/making-bundler-1-0-0-beta-10-install-nokogiri-on-leopard-with-a-newish-libxml/</link>
		<comments>http://www.pervasivecode.com/blog/2010/07/25/making-bundler-1-0-0-beta-10-install-nokogiri-on-leopard-with-a-newish-libxml/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 05:26:08 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=219</guid>
		<description><![CDATA[Okay, now that Bundler 1.0.0.beta.10 is out, you can once again pass build-time options to gems with native extensions, such as Nokogiri. So this supercedes my older instructions for making Bundler 0.8.5 install Nokogiri on Leopard.
So now instead of making a YAML file and referring to it, pass the options like this:

bundle config build.nokogiri \
--with-xml2-include=/usr/local/include/libxml2 [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, now that Bundler 1.0.0.beta.10 is out, you can once again pass build-time options to gems with native extensions, such as Nokogiri. So this supercedes my older <a href="http://www.pervasivecode.com/blog/2010/03/17/making-bundler-0-8-5-install-nokogiri-on-leopard-with-a-newish-libxml/">instructions for making Bundler 0.8.5 install Nokogiri on Leopard</a>.</p>
<p>So now instead of making a YAML file and referring to it, pass the options like this:</p>
<pre>
bundle config build.nokogiri \
--with-xml2-include=/usr/local/include/libxml2 \
--with-xml2-lib=/usr/local/lib
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2010/07/25/making-bundler-1-0-0-beta-10-install-nokogiri-on-leopard-with-a-newish-libxml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Princess and the Pea, as a Cucumber Feature</title>
		<link>http://www.pervasivecode.com/blog/2010/06/01/the-princess-and-the-pea-as-a-cucumber-feature/</link>
		<comments>http://www.pervasivecode.com/blog/2010/06/01/the-princess-and-the-pea-as-a-cucumber-feature/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 20:00:41 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=197</guid>
		<description><![CDATA[Kent Beck tweeted:

User story: &#8220;As a princess I want to confirm my royalty so I get bruised after sleeping on 40 mattresses over a pea&#8221;. Just tell real stories&#8221;

That sounded so much like a Cucumber feature that I decided to write it as one:

Feature: Physical Sensitivity
  In order to confirm my royalty
  As [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://twitter.com/kentbeck/">Kent Beck</a> tweeted:</p>
<blockquote><p>
User story: &#8220;As a princess I want to confirm my royalty so I get bruised after sleeping on 40 mattresses over a pea&#8221;. Just tell real stories&#8221;
</p></blockquote>
<p>That sounded so much like a <a href="http://cukes.info/">Cucumber</a> feature that I decided to write it as one:</p>
<pre>
Feature: Physical Sensitivity
  In order to confirm my royalty
  As a princess
  I want to be very delicate

  Scenario: 40 mattresses on a pea
  Given there is a pea on the bed
  And there is a stack of 20 mattresses on the pea
  And there is a stack of 20 featherbeds on the mattresses
  When I try to sleep on top of the stack of featherbeds
  Then I should not be able to sleep
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2010/06/01/the-princess-and-the-pea-as-a-cucumber-feature/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to make Machinist and Autotest coexist</title>
		<link>http://www.pervasivecode.com/blog/2010/03/23/how-to-make-machinist-and-autotest-coexis/</link>
		<comments>http://www.pervasivecode.com/blog/2010/03/23/how-to-make-machinist-and-autotest-coexis/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 03:47:22 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[agile development]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=189</guid>
		<description><![CDATA[If you&#8217;ve tried to use Machinist and autotest (part of ZenTest) you have probably seen this exception that prevented you from using it: 
`method_missing': No sham defined for name
It&#8217;s discussed in the machinist Google Group as well.
It&#8217;s because of a wacky hack that&#8217;s part of Machinist that overrides Module.name so you can do Sham.name, but [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve tried to use Machinist and autotest (part of ZenTest) you have probably seen this exception that prevented you from using it: </p>
<pre>`method_missing': No sham defined for name</pre>
<p>It&#8217;s discussed in <a href="http://groups.google.com/group/machinist-users/browse_thread/thread/9af4aa2243ae0368/4e9bba83113ace20">the machinist Google Group</a> as well.</p>
<p>It&#8217;s because of a wacky hack that&#8217;s part of Machinist that overrides Module.name so you can do <code>Sham.name</code>, but ZenTest expects Module.name to do what it does normally.</p>
<p>I have a fix for this.<br />
<span id="more-189"></span></p>
<p>I sent notahat a pull request via GitHub, so hopefully it&#8217;ll be merged by notahat into the official machinist gem, but until that happens you can try mine. I published the gem as JamieFlournoy-machinist to rubygems.org.</p>
<p>Try &#8216;gem install JamieFlournoy-machinist -v 1.0.6&#8242; and see if it works for you. It does for me with ZenTest 4.2.1, autotest-rails 4.1.0, and a Rails 2.3.5 project.</p>
<p><a href="http://github.com/JamieFlournoy/machinist">(my machinist fork on github)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2010/03/23/how-to-make-machinist-and-autotest-coexis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>2</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>
<p><b>Update:</b> Bundler 1.0.0.beta.10 allows this too. See my <a href="http://www.pervasivecode.com/blog/2010/07/25/making-bundler-1-0-0-beta-10-install-nokogiri-on-leopard-with-a-newish-libxml/">other post</a> that shows how to do it.</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>4</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>Things I had to fix for Rails 2.2.2</title>
		<link>http://www.pervasivecode.com/blog/2008/11/29/things-i-had-to-fix-for-rails-222/</link>
		<comments>http://www.pervasivecode.com/blog/2008/11/29/things-i-had-to-fix-for-rails-222/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 01:57:06 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=85</guid>
		<description><![CDATA[The new features in Ruby on Rails 2.2.2 have been well documented, and I&#8217;m looking forward to using several of them on WhatYouAte.com. If you&#8217;re reading this you probably are too.
However, if you&#8217;re upgrading an existing project and you&#8217;re sticking with official releases (as opposed to edge Rails) like I am, your code probably needs [...]]]></description>
			<content:encoded><![CDATA[<p>The new features in Ruby on Rails 2.2.2 have been <a href="http://www.rubyinside.com/rails-22-released-27-links-and-resources-to-get-you-going-1354.html">well documented</a>, and I&#8217;m looking forward to using several of them on <a href="http://www.whatyouate.com/">WhatYouAte.com</a>. If you&#8217;re reading this you probably are too.</p>
<p>However, if you&#8217;re upgrading an existing project and you&#8217;re sticking with official releases (as opposed to edge Rails) like I am, your code probably needs some tweaking to work with Rails 2.2.2. Mine certainly did. Although there were a lot of failed tests with ugly stacktraces, there were only a few API changes in Rails that needed to be accomodated to fix them all. Here&#8217;s a list of the changes that broke my app, and what I had to do to get it working again.<br />
<span id="more-85"></span></p>
<p><code>TextHelper.truncate</code> no longer allows you to supply the length parameter as a second argument. You have to use <code>truncate(somestring, :length => 20)</code> instead of <code>truncate(somestring, 20)</code>.</p>
<p>I was getting an error that looked like this:</p>
<pre>ActionView::TemplateError: wrong number of arguments (3 for 1)</pre>
<p>The line in question is:</p>
<pre>
&lt;%= render(:partial => 'layouts/header') %&gt;
</pre>
<p>The problem is that the simply_helpful plugin&#8217;s behavior is now built-in; just delete the whole vendor/plugins/simply_helpful directory if you were using it, and any existing code that depends on simply_helpful&#8217;s API should work again.</p>
<p>I was using the <a href="http://weblog.jamisbuck.org/2007/2/2/introducing-tztime">tztime</a> and <a href="http://agilewebdevelopment.com/plugins/tzinfo_timezone">tzinfo_timezone</a> plugins, which are no longer needed now that time zone support is available via Time.zone in Rails 2.2. Delete the plugins, upgrade the tzinfo gem to 0.3.12 or uninstall any older version of it, and search for TzTime instances in your code and replace any of the occurences of Time.at(some_tztime_variable) that you had been using. TzInfo is still useful in cases such as this:</p>
<pre>TZInfo::Timezone.get('UTC')</pre>
<p>But since you can now embed the time zone information in a Time instance, you don&#8217;t need TzTime objects anymore.</p>
<p>I was using a nice hack from Technoweenie, to allow me to validate forms using the familiar ActiveRecord model validations API even if those forms were not backed by a database table (sometimes the UI just doesn&#8217;t look anything like the data model, y&#8217;know?). It broke, and I was unable to find any solutions from anyone else, so I wrote my own: <a href="http://www.pervasivecode.com/blog/2008/11/29/ephemeralmodel-for-rails-222-form-validation-without-a-db-table/">EphemeralModel, for Rails 2.2.2 form validation without a DB table</a>.</p>
<p>I was using <code>ActiveRecord::Errors.default_error_messages[:invalid]</code> in a few model validators, which is now deprecated because it&#8217;s not internationalized. The new way to ask for the same string in the currently configured language is: <code>I18n.t(:invalid, :scope => 'activerecord.errors.messages')</code></p>
<p>Finally, the app wouldn&#8217;t start because it wants a <code>:secret</code> option to <code>config.action_controller.session</code> in <code>config/environment.rb</code>. I came up with something sufficiently random and now it&#8217;s fine.</p>
<p>I&#8217;d just like to make one last comment about this: having thorough automated tests is a huge time and sanity saver during this process. If you&#8217;re not close to 100% test coverage on your current Rails project, then your house is on fire and you need to address that immediately. Even as I write this in late 2008, I&#8217;m talking to prospective clients about contracting work, and some of them don&#8217;t do automated testing at all(!). Unless you&#8217;re in a mad dash for a demo that will save the company from financial collapse, having automated tests that verify that your code works should be priority #1. Upgrades can wait.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2008/11/29/things-i-had-to-fix-for-rails-222/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>EphemeralModel, for Rails 2.2.2 form validation without a DB table</title>
		<link>http://www.pervasivecode.com/blog/2008/11/29/ephemeralmodel-for-rails-222-form-validation-without-a-db-table/</link>
		<comments>http://www.pervasivecode.com/blog/2008/11/29/ephemeralmodel-for-rails-222-form-validation-without-a-db-table/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 23:13:05 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=84</guid>
		<description><![CDATA[I recently upgraded WhatYouAte.com to Rails 2.2.2. I had been using advice from the Rails Wiki&#8217;s HowToUseValidationsWithoutExtendingActiveRecord page. I was using a class based on the RailsWeenie code (that site is down now) and it stopped working. Here&#8217;s a new replacement hack that works almost identically.


# Usage: in the faux-model class, include EphemeralModel and ActiveRecord::Validations
# [...]]]></description>
			<content:encoded><![CDATA[<p>I recently upgraded <a href="http://www.whatyouate.com/">WhatYouAte.com</a> to Rails 2.2.2. I had been using advice from the Rails Wiki&#8217;s <a href="http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord">HowToUseValidationsWithoutExtendingActiveRecord</a> page. I was using a class based on the RailsWeenie code (that site is down now) and it stopped working. Here&#8217;s a new replacement hack that works almost identically.<br />
<span id="more-84"></span></p>
<pre>
# Usage: in the faux-model class, include EphemeralModel and ActiveRecord::Validations
# Then just say "column :foo" to set up a property called 'foo' for validation
module EphemeralModel
    # Stubbing methods from ActiveRecord::Base that ActiveRecord::Validations may call
    def EphemeralModel.included(mod)
        def mod.self_and_descendents_from_active_record; [self]; end
        def mod.human_attribute_name(arg); arg.to_s; end
        def mod.human_name; self.name; end
        def mod.column(attr_name)
            attr_name = attr_name.to_s
            column_names.push(attr_name) unless column_names.include?(attr_name)
            attr_accessor attr_name
        end
        mod.module_eval do
            def self.column_names
                cn = self.instance_variable_get(:@column_names)
                if cn.nil?
                    cn = self.instance_variable_set(:@column_names, [])
                end
                cn
            end
        end
    end
    def save; end
    def save!; end
    def update_attribute; end
    def new_record?; end

    def initialize(initial_attributes = nil)
        initial_attributes.each{|k,v| instance_variable_set("@#{k}".to_sym, v)} if initial_attributes
    end

    def attributes
        @attributes = {}
        self.class.column_names.each do |colname|
            @attributes[colname]= instance_variable_get("@#{colname}".to_sym)
        end
        @attributes
    end

    def attribute_names
        self.class.column_names
    end

    def [](name); attributes[name.to_s]; end

    def []=(name, value)
        instance_variable_set("@#{name}".to_sym, value)
        # print "set #{name} to " + instance_variable_get("@#{name}".to_sym) + ".\n" # DEBUG
    end
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2008/11/29/ephemeralmodel-for-rails-222-form-validation-without-a-db-table/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rakefile snippet to run Rails&#8217; rake:stats in a plain old gem</title>
		<link>http://www.pervasivecode.com/blog/2008/08/11/rakefile-snippet-to-run-rails-rakestats-in-a-plain-old-gem/</link>
		<comments>http://www.pervasivecode.com/blog/2008/08/11/rakefile-snippet-to-run-rails-rakestats-in-a-plain-old-gem/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 21:48:47 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/2008/08/11/rakefile-snippet-to-run-rails-rakestats-in-a-plain-old-gem/</guid>
		<description><![CDATA[I&#8217;ve been moving a bunch of utility code out of a medium sized project (>10KLOC)  to make it easier to test. I started by trying to make a set of plugins, but inter-plugin dependency management is basically nonexistent, and now that Rails supports explicit Gem dependencies, I decided to make them gems.
I&#8217;m happy I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been moving a bunch of utility code out of a medium sized project (>10KLOC)  to make it easier to test. I started by trying to make a set of plugins, but inter-plugin dependency management is basically nonexistent, and now that Rails supports explicit Gem dependencies, I decided to make them gems.</p>
<p>I&#8217;m happy I chose to make them gems, but I miss some of the stuff you get in a Rails project. In particular I wanted &#8216;rake:stats&#8217;, so I can update my estimation spreadsheet which is now almost 9 months old. I need the stats for each gem in addition to the main Rails project, in order to compare this to prior figures from the Rails project before I split it up.</p>
<p>So, here is the Rakefile snippet that I added, which adds the rake:stats task into a regular gem. If you don&#8217;t have the Rails gem installed, it will fail gracefully, without breaking your whole gem. So you need not make your teeny little gem depend on all of Rails being installed on every machine where your gem needs to go. Just install Rails whereever you want to run stats, which is probably already the case on your development machine.<br />
<span id="more-77"></span></p>
<p>Here is a Pastie link, since my blog theme truncates the too-wide text: <a href="http://pastie.org/251453">this same code on Pastie</a></p>
<pre>
# stats
begin
  gem 'rails'
  require 'code_statistics'
  namespace :spec do
    desc "Use Rails's rake:stats task for a gem"
    task :statsetup do
      class CodeStatistics
        def calculate_statistics
          @pairs.inject({}) do |stats, pair|
            if 3 == pair.size
              stats[pair.first] = calculate_directory_statistics(pair[1], pair[2]); stats
            else
              stats[pair.first] = calculate_directory_statistics(pair.last); stats
            end
          end
        end
      end
      ::STATS_DIRECTORIES = [['Libraries',   'lib',  /.(sql|rhtml|erb|rb|yml)$/],
                   ['Tests',     'test', /.(sql|rhtml|erb|rb|yml)$/]]
      ::CodeStatistics::TEST_TYPES << "Tests"
    end
  end
  desc "Report code statistics (KLOCs, etc) from the application"
  task :stats => “spec:statsetup” do
    CodeStatistics.new(*STATS_DIRECTORIES).to_s
  end
rescue Gem::LoadError => le
  task :stats do
    raise RuntimeError, “‘rails’ gem not found - you must install it in order to use this task.n”
  end
end</pre>
<p>BTW I intend to replace this with a <a href="http://www.dwheeler.com/sloccount/">sloccount</a>-based task later. If and when I do that I&#8217;ll publish it here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2008/08/11/rakefile-snippet-to-run-rails-rakestats-in-a-plain-old-gem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
