September 2009
15 posts
4 tags
testing named scopes
Everyone was psyched when Nick Kallen’s has_finder plugin was added to Rails as named_scope. They’re powerful, particularly when chaining. One disadvantage is that they are so easy to create, people want their tests to be equally concise, which is often impossible. Two related “testing named scopes” questions have come up recently on the thoughtbot training mailing list...
Sep 29th
2 notes
1 tag
Success story: Big Room consulting
We like to spotlight our thoughtbot training students’ work. Big Room In this installment, we’re checking in with Mike Breen, a local Bostonian. Mike took one of our very first Boston classes. Since last year, he has been doing Rails consulting as an owner of Big Room Technology. Since last October I launched a family of products for SEAK.com: Directory of Expert Witnesses,...
Sep 22nd
3 tags
Role Suggesting Name
There’s an excellent discussion on Josh’s “self” post last week. In it, I argued: in my opinion, naming classes, methods, and variables expressively should be the #1 goal of a programmer when writing or refactoring code. How to write great names The path to great naming involves a few simple rules. You can often deduce the scope and type of a variable. The one...
Sep 18th
swallow nil
I’ve been using this for about a year and a half. Abomination or legitimate tool for the box? Usage two dots or more. nil anywhere in the chain is an acceptable response. campaign = swallow_nil { supporter.politician.campaign } Definition def swallow_nil yield rescue NoMethodError nil end try() It’s similar to try from ActiveSupport except for chains instead of...
Sep 18th
6 notes
How to Ruin a jQuery Plug-in: Markup
jQuery is one of my favorite libraries, in any language. And one of the greatest advantages of using jQuery is the abundance of third-party plug-ins available. Just about anything you need to do has probably already been done and extracted into a plug-in. That being said there are ways I feel you can completely ruin a jQuery plug-in. Generating or expecting specific markup. Recently I needed a...
Sep 17th
tiniest of tips
Every time you find yourself typing something long and repetitive, automate it. Every time. I often hop into script/console, so aliasing that is a no-brainer. That is not the point of this post, though; I also often look up my current user when I’m in script/console, and I get tired of typing my email address every time. Add a method into your ~/.irbrc to make that painless,...
Sep 16th
Five Ridiculously Awesome Cucumber (and Webrat)...
Cucumber, if you haven’t heard, is the Next Big Thing™. It affords developers the ability to write human-readable integration tests. Even though it’s only at version 0.3.101, it’s full-featured with a handful of hidden gems. Here are five of my favorites. Running Targeted Features with Cucumber Profiles In a Rails application, if a RAILS_ROOT/cucumber.yml file is present,...
Sep 16th
10 notes
To self. or not to self.
Off with his head! Ruby developers have gone back and forth for ages as to whether to use self. on their method calls. Why is everyone bickering? Can’t we all just get along? My guess is that we can’t. This argument is similar to that of the use of whitespace. Some people are picky while others are not; however, almost all have a very strong opinion. This above all: to...
Sep 11th
3 notes
Zsh completion for your tests and specs
Got zsh? Loving your TATFT but tired of typing out all those spec commands? spec -cfs spec/controllers/application_controller_spec.rb or test commands? ruby -Itest test/functionals/admin/users_controller_test.rb Well, put away that keyboard oil, because you’ll be saving keystrokes left and right! [~/dev/railsapp] $ spc a<Tab> admin/base_controller application_controller ...
Sep 11th
Quacking like an ActiveRecord in Rails 2.3.4
You know everything about pretending to be an ActiveRecord::Base object, and especially the bit about ActiveRecord::Errors. That part’s really cool. What we are doing here is making an object that quacks like an ActiveRecord::Base object but has no database table backing it. As examples: a search object, a credit card object, or a remote user object. To do this, you need id, new_record?,...
Sep 10th
Catch a tiger by the tail
Rails’ logs contain valuable information. You’ve probably seen output like the following: Processing PagesController#show (for 127.0.0.1 at 2009-09-09 19:07:29) [GET] Parameters: {"id"=>"about"} Rendering template within layouts/narrow Rendering pages/about Rendered shared/_navigation (2.7ms) Rendered shared/_flashes (0.8ms) Rendered shared/_javascript (1.2ms) Completed in 28ms...
Sep 9th
You've Got Mail
There are just some days when you want to interact with real data from a Rails application that you wrote. Doing a database dump, saving the output locally, and loading that data in is the ideal method, but once you do that, you’ve got live data locally. This can be a Bad Thing™ if you’re sending email. Why? Mailers can be triggered in any number of places. A callback, a...
Sep 9th
1 note
gsub with a block
Did you know gsub can take a block? I didn’t until today. "10 comments".gsub(/ (.+)/) do |words| " <div class='superstyle'>#{words.lstrip}</div>" end # "10 <div class='superstyle'>comments</div>" It seems limited. I would have expected $1 to be the first block parameter, $2 to be the second block parameter, etc., but I have only been able to get one block...
Sep 8th
2 notes
7 tags
Testing failure
A common pattern in our apps is handling failure by notifying Hoptoad. A good example is hitting a third party web service, Twitter, from Thunder Thimble. def fetch_tweets(search) search.fetch rescue *(HTTP_ERRORS + Twitter::Search::ERRORS) => error HoptoadNotifier.notify(error) [] end Hoptoad We send rescued errors to Hoptoad, shielding our users from 500s with relevant copy. ...
Sep 6th
2 notes
5 tags
Always remember me
Clearance now uses only cookies with a long expiration as its default. The effect is always remembering the user unless they ask to be signed out. “I’ll never let go, Jack! I’ll never let go!” The only place to go after the Care Bears is the Titanic. A better “remember” default A couple of weeks ago, I asked how Clearance should handle “remember...
Sep 1st
3 notes