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:

users = User.find(:all) do
  blog.title == params[:blog_name]

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!

site = Site.find(:first) do
  any {
    domains.hostname == params[:hostname]
    domains.hostname == "www.#{params[:hostname]}"

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.

Post.find(:all) do
  -any {
    title =~ /^OMG/
    id == 4
  order_by -created_on

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 repo is here for all of you itching to try it out.

Pair with one of our expert developers to level up your skills with Coaching by thoughtbot. Save time learning best practices and techniques for reducing technical debt in Ember, Ruby, Haskell, and Go in 1-on-1 sessions tailored to your goals.