Ben Scofield

Archive for the ‘rails’ tag

Application templates in Rails 3

with 2 comments

Here’s a trick for creating modular templates in Rails 3: use the apply method

Say you like to use Rails templates, but you find them a little too inflexible – you want to use Cucumber, jQuery, and MongoDB, but not always together. You could create separate templates for each combination (and in fact, with Rails 2.3.x that’s what you have to do), but in Rails 3, things get a little easier. With 3, you can actually modularize your templates!

For instance, this gist removes the default Prototype JavaScript files from your app, and gives you a simple XHTML layout with Google’s hosted versions of jQuery all ready to go:

jQuery application template

This gist, on the other hand, requires the Cucumber and webrat gems and runs the Cucumber generator to set up the features directory, etc:

Cucumber application template

In current Rails, you’d either have to run these individually or put the code into a single file to get them to apply to a project; in Rails 3, however, you can create a separate template that applies both of them:

Aggregate template

In Rails 3, the directive to apply a template has been refactored into the apply command, which you can access inside a template, because they’re instance_eval‘d by the application generator. This opens up a whole new world of possibilities for remixing components, and should make starting new Rails applications even easier. One more reason to anticipate the arrival of 3!

Written by Ben

September 18th, 2009 at 7:00 am

Posted in General

Tagged with

My workflow – now Cucumber-enhanced!

with one comment

CucumberSo I’ve been using Cucumber somewhat heavily for a while now, on both personal and client projects, and I’m starting to settle into a comfortable rhythm with it. At first, though, I found integrating features into my workflow extremely disconcerting. It went something like this:

  • I’d write a feature, run it, and watch it flag half the steps as ‘pending’.
  • Then, I’d drop down into the step definitions and fill them out with the appropriate lower-level code (e.g., “Given a user named Sarah” became User.create(:name => 'Sarah')).
  • When the steps seemed correct, I’d rerun the feature and see it fail on (usually) the first step.
  • I’d then drop into the appropriate lower level – controllers or models, but usually the latter at this stage – and work through things in my normal, mostly TDDish way.
  • Once I got the appropriate lower-level code tested and working for that step, I’d rerun the feature and repeat the cycle for each step that failed.

This process worked, but I wouldn’t say it worked well. I ended up bouncing almost constantly between running the feature (to see which step was failing) and the functional and unit tests, and all in all never quite felt confident that things were working at any given point.

After talking to a number of people about their workflows with Cucumber, however, I started working slightly differently. Now, things go more like this:

  • I write a feature, run it, and watch it flag half the steps as ‘pending’.
  • I then leave Cucumber and drop into the lower-level code, referring to the original feature only if I forget the overall vision of what I’m working on. At this point, my work feels pretty much the same as it did before I started using Cucumber.
  • Once I’m satisfied with the tests and code at the lower level, I return to Cucumber, fill out the step definitions, and run the feature – at which point I’m either done (because the steps all pass), or realize I’ve forgotten something and drop down to the lower level again to fix it.

The key change here is that I stay out of Cucumber as much as possible; I only run features at the beginning (to see all the pendings) and at the end (to see the passes), and I really only touch the step definitions once. In effect, I’ve moved away from Cucumber as part of my test-driven development cycle, and instead use it as an overarching vision for the high-level functionality I’m working on… and yes, I know this sounds very close to the language of BDD, but it seems much closer to being story-driven than behavior-driven to me (behavior sounds more like the lower level, where I’m still clearly TDDing, thank you very much).

So that’s how I’m working with Cucumber now. What about you? Do you bounce back and forth frequently, and happily? Have you eliminated controller/functional testing in favor of Cucumber, thus reducing the levels to navigate? Done something completely different? Inquiring minds want to know!

Written by Ben

September 16th, 2009 at 7:00 am

Posted in General

Tagged with ,