Skip to content

Commit

Permalink
Fix for the linking between meals-by-month views.
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed May 25, 2009
1 parent 7829342 commit d951f02
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 26 deletions.
19 changes: 18 additions & 1 deletion features/step_definitions/meal_details.rb
Expand Up @@ -10,7 +10,8 @@
:serves => 4,
:summary => "meal summary",
:description => "meal description",
:type => "Meal"
:type => "Meal",
:menu => []
}

RestClient.put "#{@@db}/#{permalink}",
Expand All @@ -32,6 +33,10 @@
response.status.should == 200
end

When /^I follow the link to the list of meals in April of 2009$/ do
click_link "2009-04"
end

Then /^the "([^\"]*)" meal should be included in the list$/ do |title|
response.should have_selector("li a", :content => title)
end
Expand All @@ -47,3 +52,15 @@
Then /^I should not see the "([^\"]*)" meal among the meals of this month$/ do |title|
response.should_not have_selector("h2", :content => title)
end

Then /^I should not see a link to June of 2009$/ do
response.should_not have_selector("a", :content => "2009-06")
end

Then /^I should not see a link to February of 2009$/ do
response.should_not have_selector("a", :content => "2009-02")
end

Then /^I should see a link to May of 2009$/ do
response.should have_selector("a", :content => "2009-05")
end
13 changes: 7 additions & 6 deletions helpers.rb
Expand Up @@ -121,17 +121,18 @@ def sort_link(text, sort_field, results, options = { })
%Q|<a href="#{url}" id="#{id}">#{text}</a>|
end

def link_to_year_in_set(current, couch_view, options={})
compare_years = options[:previous] ?
Proc.new { |year, current_year| year < current_year} :
Proc.new { |year, current_year| year > current_year}
def link_to_adjacent_view_date(current, couch_view, options={})
compare = options[:previous] ?
Proc.new { |date_fragment, current| date_fragment < current} :
Proc.new { |date_fragment, current| date_fragment > current}

next_result = couch_view.
send(options[:previous] ? :reverse : :map).
detect{|result| compare_years[result['key'].to_i, current.to_i]}
detect{|result| compare[result['key'], current.to_s]}

if next_result
%Q|<a href="/meals/#{next_result['key']}">#{next_result['key']}</a>|
next_uri = next_result['key'].gsub(/-/, '/')
%Q|<a href="/meals/#{next_uri}">#{next_result['key']}</a>|
else
""
end
Expand Down
47 changes: 30 additions & 17 deletions spec/eee_helpers_spec.rb
Expand Up @@ -198,23 +198,36 @@

end

describe "link_to_next_year" do
before(:each) do
@count_by_year = [{"key" => "2008", "value" => 3},
{"key" => "2009", "value" => 3}]
end
it "should link to the next year after the current one" do
link_to_year_in_set(2008, @count_by_year).
should have_selector("a",
:href => "/meals/2009")
end
it "should link to the previous before the current one" do
link_to_year_in_set(2009, @count_by_year, :previous => true).
should have_selector("a",
:href => "/meals/2008")
describe "link_to_adjacent_view_date" do
context "couchdb view by_year" do
before(:each) do
@count_by_year = [{"key" => "2008", "value" => 3},
{"key" => "2009", "value" => 3}]
end
it "should link to the next year after the current one" do
link_to_adjacent_view_date(2008, @count_by_year).
should have_selector("a",
:href => "/meals/2009")
end
it "should link to the previous before the current one" do
link_to_adjacent_view_date(2009, @count_by_year, :previous => true).
should have_selector("a",
:href => "/meals/2008")
end
it "should return empty if there are no more years" do
link_to_adjacent_view_date(2009, @count_by_year).
should == ""
end
end
it "should return empty if there are no more years" do
link_to_year_in_set(2009, @count_by_year).
should == ""
context "couchdb view by_month" do
before(:each) do
@count_by_month = [{"key" => "2009-04", "value" => 3},
{"key" => "2009-05", "value" => 3}]
end
it "should link to the next month after the current one" do
link_to_adjacent_view_date("2009-04", @count_by_month).
should have_selector("a",
:href => "/meals/2009/05")
end
end
end
5 changes: 5 additions & 0 deletions views/meal_by_month.haml
@@ -1,5 +1,10 @@
%h1= "Meals from #{@year}-#{@month}"

%div.navigation
=link_to_adjacent_view_date("#{@year}-#{@month}", @count_by_year, :previous => true)
|
=link_to_adjacent_view_date("#{@year}-#{@month}", @count_by_year)

.meals
- @meals["rows"].each do |meal|
= image_link meal['value'][0], :width => 200, :height => 150
Expand Down
4 changes: 2 additions & 2 deletions views/meal_by_year.haml
@@ -1,9 +1,9 @@
%h1= "Meals from #{@year}"

%div.navigation
=link_to_year_in_set(@year, @count_by_year, :previous => true)
=link_to_adjacent_view_date(@year, @count_by_year, :previous => true)
|
=link_to_year_in_set(@year, @count_by_year)
=link_to_adjacent_view_date(@year, @count_by_year)

%ul
- @meals["rows"].each do |meal|
Expand Down

0 comments on commit d951f02

Please sign in to comment.