Skip to content

Commit

Permalink
DRY up meal and recipe categories
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed Jun 19, 2009
1 parent 34c97a1 commit b4fafe8
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 25 deletions.
10 changes: 10 additions & 0 deletions features/step_definitions/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,13 @@
:class => "active",
:content => "Italian")
end

When /^I click on the recipe in the menu$/ do
click_link "Recipe for Meal 0"
end

Then /^I should see the recipe page$/ do
response.should have_selector("h1",
:content => "Recipe for Meal 0")
end

12 changes: 12 additions & 0 deletions helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ def amazon_url(asin)
"http://www.amazon.com/exec/obidos/ASIN/#{asin}/eeecooks-20"
end

def categories(context)
categories = %w{Italian Asian Latin Breakfast Chicken Fish Meat Salad Vegetarian}

links = categories.map do |category|
%Q|<li>#{recipe_category_link(context, category)}</li>|
end

links << "<a>Recipes</a>"

%Q|<ul id="eee-categories">#{links}</ul>|
end

def recipe_category_link(recipe, category)
recipes = recipe.is_a?(Array) ? recipe : [recipe]
if recipes.any? { |r|
Expand Down
23 changes: 23 additions & 0 deletions spec/eee_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper' )

describe "categories" do
it "should link to the italian category" do
categories({}).
should have_selector("#eee-categories a", :content => "Italian")
end
it "should be able to highlight the link to the italian category" do
categories({'tag_names' => %w{italian}}).
should have_selector("#eee-categories a", :content => "Italian")
end
it "should link to the fish category" do
categories({}).
should have_selector("#eee-categories a", :content => "Fish")
end
it "should link to the vegetarian category" do
categories({}).
should have_selector("#eee-categories a", :content => "Vegetarian")
end
it "should link to all recipes" do
categories({}).
should have_selector("#eee-categories a", :content => "Recipes")
end
end

describe "recipe_category_link" do
it "should create an active link if the recipe includes the category" do
recipe_category_link({'tag_names' => ['italian']}, 'Italian').
Expand Down
1 change: 1 addition & 0 deletions spec/eee_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
with(/2009-0/).
exactly(10).times.
and_return('{"title":"foo",' +
'"date":"2009-06-17",' +
'"summary":"foo summary",' +
'"menu":[]}')

Expand Down
2 changes: 2 additions & 0 deletions spec/views/meal.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"value" => ['2009-05-15', "Foo"] },
{ "key" => "2009-05-31",
"value" => ["2009-05-31", "Bar"] }]

assigns[:recipes] = []
end

it "should display a breadcrumb link to the other meals in this year" do
Expand Down
14 changes: 1 addition & 13 deletions views/meal.haml
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
%ul#eee-categories
%li= recipe_category_link(@recipes, 'Italian')
%li= recipe_category_link(@recipes, 'Asian')
%li= recipe_category_link(@recipes, 'Latin')
%li= recipe_category_link(@recipes, 'Breakfast')
%li= recipe_category_link(@recipes, 'Chicken')
%li= recipe_category_link(@recipes, 'Fish')
%li= recipe_category_link(@recipes, 'Meat')
%li= recipe_category_link(@recipes, 'Salad')
%li= recipe_category_link(@recipes, 'Vegetarian')
%li
%a Recipes

= categories(@recipes)

- date = Date.parse(@meal['date'])
.breadcrumbs
Expand Down
13 changes: 1 addition & 12 deletions views/recipe.haml
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
%ul#eee-categories
%li= recipe_category_link(@recipe, 'Italian')
%li= recipe_category_link(@recipe, 'Asian')
%li= recipe_category_link(@recipe, 'Latin')
%li= recipe_category_link(@recipe, 'Breakfast')
%li= recipe_category_link(@recipe, 'Chicken')
%li= recipe_category_link(@recipe, 'Fish')
%li= recipe_category_link(@recipe, 'Meat')
%li= recipe_category_link(@recipe, 'Salad')
%li= recipe_category_link(@recipe, 'Vegetarian')
%li
%a Recipes
= categories(@recipe)

.breadcrumbs
= breadcrumbs(Date.parse(@recipe['date']))
Expand Down

0 comments on commit b4fafe8

Please sign in to comment.