Testing named_scope
We’re huge fans of NamedScope here at Thoughtbot. It does wonders for finder reuse and clarity. Unfortunately, it also creates a large number of finders that all must be tested, where the old method may have only created one.
To help out with the testing of simple named_scope definitions, we added a helper to Shoulda, should_have_named_scope:
class User < ActiveRecord::Base
named_scope :old, :conditions => "age > 50"
named_scope :eighteen, :conditions => { :age => 18 }
named_scope :recent, lambda {|count| { :limit => count } }
end
class UserTest < Test::Unit::TestCase
should_have_named_scope :old, :conditions => "age > 50"
should_have_named_scope :eighteen, :conditions => { :age => 18 }
should_have_named_scope 'recent(5)', :limit => 5
should_have_named_scope 'recent(1)', :limit => 1
end
Now for complex methods, where the options returned by the has_finder are hairier than a simple limit or a single column condition, we would also write black box tests. But this helper is great for those simpler calls.
Quick vim svn blame snippet
Just to prove the theory that every incredibly incomprehensible piece of code you come across is probably your own, I penned a quick svn blame vim command.
vmap gl :<C-U>!svn blame <C-R>=expand("%:p") <CR> \| sed -n <C-R>=line("'<") <CR>,<C-R>=line("'>") <CR>p <CR>
Just highlight the disgusting code in question, and bask in your own shame!

Getting back into the swing of things.
The past three weeks have been a total whirlwind of excitement, glamour, drinking, more glamour, sickness, boredom, and a bit more glamour.
I gave a presentation on BDD with shoulda for Mountain West Ruby Conf in Salt Lake City (video above).

It must be nice to live in a city completely surrounded by gorgeous snow capped mountains. And the library where the conference was held was an incredible example of architecture. Lots of sweeping curves and generous amounts of glass.
After that, it was off to present at Scotland on Rails. I’ve never been to a city as thoroughly beautiful as Edinburgh. It seemed like every street was lined with buildings older than my own country.
I’ll be putting the shots I took for both of these trips up on my flickr account as soon as I have time to unload them.
Unfortunately, I fell under the grips of a serious cold at the end of that trip, and just finished recovering last weekend. So now it’s time to get back up to speed.
I’ll be in New York next weekend for GoRuCo, Brisbane on the 9th for some personal time, San Diego on the 23rd for more of the same, Portland on the 29th for RailsConf, and Portland again in July for OSCON, where I’ll be giving a 3 hour tutorial on advanced ruby metaprogramming.
In fact, I’ve got so many trips planned this season, that I’ve started using Dopplr to keep track of them. Check out my profile and itinerary in the sidebar, stage left.
And I promise to get back to more technical posts as soon as the dust settles.
Email your Backpack GTD items
Like most kids out there, I’m a follower of the GTD system. I’ve gone through a bunch of GTD software and have boiled it down to a combination of Backpack and a pad of paper in my hip pocket.
I thought I’d share a script I wrote that emails me my list of todo items every morning.
First off, my backpack GTD system is pretty straight forward:
- A page for each context, the names of which start with ’@’ – so @work, @phone, @online, @zipcar, @home, @blog, etc.
- Each context page includes a single list named “Tasks”
That’s about it. I’ve also got a someday/maybe page, and a page for each major project, but those don’t get emailed to me.
Download this script, change the constants at the top, and plop it on a server somewhere. You should test it on the server it will live on by running /usr/local/bin/ruby /home/tsaleh/bin/email_todos.rb
Then setup your crontab to run that sucker every morning. My crontab looks like this:
@daily /usr/local/bin/ruby /home/tsaleh/bin/email_todos.rb
That should be it. The next morning you’ll get a list of the tasks you have to complete for each context.
Politiquotes - Who said what, when, and where
I’d like to announce the launching of a small side project of mine:
Politiquotes
A political awareness app where you can post, vote on, and discuss quotes from your favorite candidates.
So join up and get your quote on!
Shoulda 4.0.1 - the lean and mean release
Crossposted from GiantRobots
Shoulda is a great tool for cleaning up your tests, but there was always some dirt under the hood that was keeping me up at night. I don’t want to just move complexity around – I want to remove it entirely.
A recent change in edge rails forced me to get off my butt and do that.
So everyone give a warm hello to the latest Shoulda gem. It now uses a Context class in the backend, prints out should_eventually tests in a clear way, names the bare shouldas nicely, and fixes a few small bugs.
I also took the opportunity to fix some of the outstanding issues in the shoulda rails plugin (with a lot of great help from the community).
So grab the latest plugin and have some fun:
piston install https://svn.thoughtbot.com/plugins/shoulda/tags/rel-4.0.1 vendor/plugins/shoulda
Also, please let me know if you find any bugs or issues.


