Skip to content

Commit

Permalink
Homepage: list of meals, links to meal pages, meal tags/categories
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed Jun 17, 2009
1 parent 6cca629 commit 34c97a1
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 9 deletions.
2 changes: 2 additions & 0 deletions eee.rb
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions features/step_definitions/site.rb
Expand Up @@ -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
13 changes: 12 additions & 1 deletion helpers.rb
Expand Up @@ -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|<a class="active">#{category}</a>|
else
%Q|<a>#{category}</a>|
Expand All @@ -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}"))
Expand Down
40 changes: 40 additions & 0 deletions 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")
Expand Down Expand Up @@ -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"}'
Expand Down
10 changes: 5 additions & 5 deletions spec/eee_spec.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion spec/views/index.haml_spec.rb
Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions 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(", "))
14 changes: 14 additions & 0 deletions 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)
Expand Down

0 comments on commit 34c97a1

Please sign in to comment.