Skip to content

Commit

Permalink
Reworking of the link to next/previous meal years in a more worky way
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed May 18, 2009
1 parent 9e3a69f commit 390bdbe
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion eee.rb
Expand Up @@ -24,7 +24,7 @@

url = "#{@@db}/_design/meals/_view/count_by_year?group=true"
data = RestClient.get url
@count_by_year = data['rows']
@count_by_year = JSON.parse(data)['rows']

haml :meal_by_year
end
Expand Down
6 changes: 4 additions & 2 deletions features/step_definitions/meal_details.rb
Expand Up @@ -5,10 +5,11 @@

meal = {
:title => title,
:date => date,
:date => date.to_s,
:serves => 4,
:summary => "meal summary",
:description => "meal description"
:description => "meal description",
:type => "Meal"
}

RestClient.put "#{@@db}/#{permalink}",
Expand All @@ -22,6 +23,7 @@
end

When /^I follow the link to the list of meals in 2008$/ do
save_and_open_page
click_link "2008"
end

Expand Down
13 changes: 9 additions & 4 deletions helpers.rb
Expand Up @@ -119,10 +119,15 @@ def sort_link(text, sort_field, results, options = { })
%Q|<a href="#{url}" id="#{id}">#{text}</a>|
end

def link_to_next_year(current, couch_view)
next_result = couch_view.detect do |result|
result['key'].to_i > current.to_i
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}

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

if next_result
%Q|<a href="/meals/#{next_result['key']}">#{next_result['key']}</a>|
else
Expand Down
9 changes: 7 additions & 2 deletions spec/eee_helpers_spec.rb
Expand Up @@ -187,12 +187,17 @@
{"key" => "2009", "value" => 3}]
end
it "should link to the next year after the current one" do
link_to_next_year(2008, @count_by_year).
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")
end
it "should return empty if there are no more years" do
link_to_next_year(2009, @count_by_year).
link_to_year_in_set(2009, @count_by_year).
should == ""
end
end
4 changes: 2 additions & 2 deletions views/meal_by_year.haml
@@ -1,9 +1,9 @@
%h1= "Meals from #{@year}"

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

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

0 comments on commit 390bdbe

Please sign in to comment.