Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.
Squirrel - Once More, with Feeling!
I admit, the first iteration of Squirrel wasn’t really as super fabulous as I thought it would be. Because of how I was using the blocks, you couldn’t do a simple thing like access params. And since half the fun of it was being able to make good looking, flexible queries, not being able to use params was a giant pain.
Now, however, I’ve managed to mix the clean, functional look of instance_eval with the husky utilitarianism of params to come up with a happier, shinier Squirrel:
1 2 3 4 |
users = User.find(:all) do blog.title == params[:blog_name] end |
And what’s that in the trees, gracefully swinging from vine to vine? It’s our friends any and all here to give us grouping blocks!
1 2 3 4 5 6 7 |
site = Site.find(:first) do any { domains.hostname == params[:hostname] domains.hostname == "www.#{params[:hostname]}" } end |
All the functions you’d expect to be able to use in your controller are available (well, as long as you actually are in your controller, anyway; squirrels aren’t miracle workers)—params, session, etc.
You can use the unary minus to negate any condition or block, and you can also use it to do a descending order_by
1 2 3 4 5 6 7 8 |
Post.find(:all) do -any { title =~ /^OMG/ id == 4 } order_by -created_on end |
This is a major rewrite from the original code and cleaned up things in a very good way. And what’s more, we’re using it in real code now, so I have a fire under my ass to keep it in fighting shape.
The SVN repo is https://svn.thoughtbot.com/plugins/squirrel/trunk for all of you itching to try it out.
About this entry
You're reading an entry on GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS, the company weblog of thoughtbot, inc.
- Author:
- Jon Yurek
- Published:
- January 12th 02:28 PM
- Updated:
- March 25th 08:48 PM
- Sections:
- Development
thoughtbot is hiring
We are hiring web developers and web designers in both Boston and New York, NY.
What are we up to?
We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.

Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.
About thoughtbot, inc.
We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.
9 comments
Jump to comment form