diff --git a/eee.rb b/eee.rb index ac301c4..341f153 100644 --- a/eee.rb +++ b/eee.rb @@ -38,6 +38,8 @@ data = RestClient.get url @meals_by_date = JSON.parse(data)['rows'] + @recipes = @meal['menu'].map { |m| wiki_recipe(m) }.compact + haml :meal end diff --git a/features/step_definitions/site.rb b/features/step_definitions/site.rb index 46aa2c4..0d95c87 100644 --- a/features/step_definitions/site.rb +++ b/features/step_definitions/site.rb @@ -101,3 +101,18 @@ response.should have_selector(".menu-items", :content => "Recipe for Meal 1") end + +When /^I click on the first meal$/ do + click_link "Meal 0" +end + +Then /^I should see the meal page$/ do + response.should have_selector("h1", + :content => "Meal 0") +end + +Then /^the Italian category should be highlighted$/ do + response.should have_selector("a", + :class => "active", + :content => "Italian") +end diff --git a/helpers.rb b/helpers.rb index 3d93e62..925e2ea 100644 --- a/helpers.rb +++ b/helpers.rb @@ -13,7 +13,11 @@ def amazon_url(asin) end def recipe_category_link(recipe, category) - if recipe['tag_names'] && recipe['tag_names'].include?(category.downcase) + recipes = recipe.is_a?(Array) ? recipe : [recipe] + if recipes.any? { |r| + r['tag_names'] && + r['tag_names'].include?(category.downcase) + } %Q|#{category}| else %Q|#{category}| @@ -37,6 +41,13 @@ def _db self.class.send(:class_variable_get, "@@db") end + def wiki_recipe(text) + if text =~ /\[recipe:([-\/\w]+)/ + permalink = $1.gsub(/\//, '-') + JSON.parse(RestClient.get("#{_db}/#{permalink}")) + end + end + def recipe_link(link, title=nil) permalink = link.gsub(/\//, '-') recipe = JSON.parse(RestClient.get("#{_db}/#{permalink}")) diff --git a/spec/eee_helpers_spec.rb b/spec/eee_helpers_spec.rb index 1373ff3..1b9ce7d 100644 --- a/spec/eee_helpers_spec.rb +++ b/spec/eee_helpers_spec.rb @@ -1,5 +1,18 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper' ) +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'). + should have_selector("a", :class => "active") + end + it "should create an active link if any recipes include the category" do + recipes = [{ 'tag_names' => ['italian'] }, + { 'tag_names' => ['foo'] }] + recipe_category_link(recipes, 'Italian'). + should have_selector("a", :class => "active") + end +end + describe "wiki" do it "should return simple text as unaltered text" do wiki("bob").should contain("bob") @@ -58,6 +71,33 @@ end end +describe "wiki_recipe" do + before(:each) do + @json = '{"_id":"2009-06-16-recipe","title":"Recipe for Foo"}' + end + it "should lookup a recipe from recipe wiki text" do + RestClient. + should_receive(:get). + with(/2009-06-16/). + and_return(@json) + + wiki_recipe(" [recipe:2009/06/16]") + end + it "should return a recipe from recipe wiki text" do + RestClient. + stub!(:get). + and_return(@json) + + wiki_recipe(" [recipe:2009/06/16]"). + should == { "_id" => "2009-06-16-recipe", + "title" => "Recipe for Foo" } + + end + it "should return nil for non-recipe wiki text" do + wiki_recipe("[rcip:2009/06/16]").should be_nil + end +end + describe "recipe_link" do before(:each) do @json = '{"_id":"2009-06-11-recipe","title":"Recipe for Foo"}' diff --git a/spec/eee_spec.rb b/spec/eee_spec.rb index 79e16ca..6cd0932 100644 --- a/spec/eee_spec.rb +++ b/spec/eee_spec.rb @@ -187,9 +187,9 @@ @permalink = @date.to_s + "-" + @title.downcase.gsub(/\W/, '-') RestClient.put "#{@@db}/#{@permalink}", - { :title => @title, - :date => @date }.to_json, - :content_type => 'application/json' + { :title => @title, + :date => @date }.to_json, + :content_type => 'application/json' end @@ -213,8 +213,8 @@ recipe = JSON.parse(data) RestClient.put "#{@@db}/#{@permalink}/sample.jpg?rev=#{recipe['_rev']}", - File.read('spec/fixtures/sample.jpg'), - :content_type => 'image/jpeg' + File.read('spec/fixtures/sample.jpg'), + :content_type => 'image/jpeg' get "/images/#{@permalink}/sample.jpg" last_response.should be_ok diff --git a/spec/views/index.haml_spec.rb b/spec/views/index.haml_spec.rb index 2f94664..b60f602 100644 --- a/spec/views/index.haml_spec.rb +++ b/spec/views/index.haml_spec.rb @@ -11,11 +11,18 @@ }] end - it "should link to the meal titles" do + it "should include the meal titles" do render("/views/index.haml") response.should have_selector("h2", :content => "Bar") end + it "should link to the the meal titles" do + render("/views/index.haml") + response.should have_selector("a", + :href => "/meals/2009/05/15", + :content => "Bar") + end + it "should include a summary of the meals" do render("/views/index.haml") response.should have_selector("p", :content => "Bar summary") diff --git a/views/index.haml b/views/index.haml index e9bc264..2c93989 100644 --- a/views/index.haml +++ b/views/index.haml @@ -1,7 +1,10 @@ .meals %h1 Meals - @meals.each do |meal| - = image_link meal, :width => 200, :height => 150 - %h2= meal["title"] + - date = Date.parse(meal['date']) + %a{:href => date.strftime("/meals/%Y/%m/%d")} + = image_link meal, :width => 200, :height => 150 + %h2 + %a{:href => date.strftime("/meals/%Y/%m/%d")}= meal["title"] = wiki(meal["summary"]) .menu-items= wiki(meal["menu"].join(", ")) diff --git a/views/meal.haml b/views/meal.haml index 7d018c7..eb3019f 100644 --- a/views/meal.haml +++ b/views/meal.haml @@ -1,3 +1,17 @@ +%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 + + - date = Date.parse(@meal['date']) .breadcrumbs = breadcrumbs(date, :day)