Sunday, June 21, 2009

Cucumber Don't Do Affordances

‹prev | My Chain | next›

Before moving onto the next Cucumber scenario, I take a moment to see if the previous scenario missed anything. I think it likely covered just about all of the necessary behavior needed. But the legacy Rails site (and the previous incarnation as an XML-based CMS) built up many affordances that I would prefer not to lose in translation.

The homepage in Webrat save_and_open_page form:

The homepage on the legacy Rails site:

Naked CSS and missing images aside, it looks as though I am missing:

  • The date of the meals

  • A "Read more..." link for the meal

  • Only linking to new recipes, not the entire menu

  • Meals on odd numbered days have right-aligned thumbnails (added "depth")

  • Text links to older meals at the bottom of the meals section

Working through that list one at a time, first up is this RSpec example:
  it "should include a pretty date for each meal" do
response.should have_selector(".meals", :content => "May 15")
This can be implemented with:
    %div= date.strftime("%B %e")
The next affordance to be added is:
  it "should suggest that the user read more..." do
response.should have_selector(".meals a",
:href => "/meals/2009/05/15",
:content => "
A simple link will make that example pass.

Only including new recipes rather than the whole meal menu is a bit trickier. The examples require building up the menu, which makes for longer examples.

One or more new recipes (the meal was prepared on 2009-05-15, new recipes would be from the same day) should be comma separated:
  it "should include a comma separated list of menu items" do
and_return(%Q|<a href="/recipes/2009/05/15/recipe1">chips</a>|,
%Q|<a href="/recipes/2009/05/15/recipe2">salsa</a>|)

assigns[:meals][0]["menu"] << "[recipe:2009/05/15/recipe1]"
assigns[:meals][0]["menu"] << "[recipe:2009/05/15/recipe2]"

response.should have_selector(".menu-items",
:content => "chips, salsa")
Old recipes and non-recipes should not be included on the homepage:
  it "should only include new recipe menu items" do
and_return(%Q|<a href="/recipes/2009/05/15/recipe1">chips</a>|)

assigns[:meals][0]["menu"] << "[recipe:2009/05/14/recipe1]"
assigns[:meals][0]["menu"] << "chili"
assigns[:meals][0]["menu"] << "[recipe:2009/05/15/recipe2]"

response.should have_selector(".menu-items",
:content => "chips")
No new recipes should produce nothing:
  it "should not include a delimiter between \"Read more\" and new recipes when no new recipes" do
response.should_not have_selector(".meals",
:content => "|")

I implement these three examples with this Haml code:
    - new_recipe_regexp ="%Y/%m/%d"))
- new_recipes = meal["menu"].select{ |r| r =~ new_recipe_regexp }
%a{:href => date.strftime("/meals/%Y/%m/%d")}
Read more&hellip;
- if new_recipes.length > 0
.menu-items= wiki(new_recipes.join(", "))
In that code snippet, I store a regular expression built from the meal's current date. I use that regular expression to select all recipes from the menu that match the current date. As long as there are new recipes, a "pipe" delimiter is inserted between the link to the current meal and to the new recipes. Finally, the wiki helper converts the wiki recipe text to HTML links.

I will stop there for today and pick up with the last two missing affordances tomorrow.

No comments:

Post a Comment