diff --git a/couch/_design/meals/views/by_date/map.js b/couch/_design/meals/views/by_date/map.js index 7b1f8bd..8bd74f4 100644 --- a/couch/_design/meals/views/by_date/map.js +++ b/couch/_design/meals/views/by_date/map.js @@ -1,5 +1,5 @@ function (doc) { if (doc['type'] == 'Meal') { - emit(doc['date'], [doc['_id'], doc['title']]); + emit(doc['date'], doc); } } diff --git a/eee.rb b/eee.rb index 4c12a72..b7ea1ff 100644 --- a/eee.rb +++ b/eee.rb @@ -70,9 +70,11 @@ end get %r{/meals/(\d+)/(\d+)} do |year, month| - url = "#{@@db}/_design/meals/_view/by_month?group=true&key=%22#{year}-#{month}%22" + url = "#{@@db}/_design/meals/_view/by_date?" + + "startkey=%22#{year}/#{month}/00%22&" + + "endkey=%22#{year}/#{month}/99%22" data = RestClient.get url - @meals = JSON.parse(data) + @meals = JSON.parse(data)['rows'].map{|r| r['value']} @month = "#{year}-#{month}" url = "#{@@db}/_design/meals/_view/count_by_month?group=true" @@ -83,9 +85,11 @@ end get %r{/meals/(\d+)} do |year| - url = "#{@@db}/_design/meals/_view/by_year?group=true&key=%22#{year}%22" + url = "#{@@db}/_design/meals/_view/by_date?" + + "startkey=%22#{year}/00/00%22&" + + "endkey=%22#{year}/99/99%22" data = RestClient.get url - @meals = JSON.parse(data) + @meals = JSON.parse(data)['rows'].map{|r| r['value']} @year = year url = "#{@@db}/_design/meals/_view/count_by_year?group=true" diff --git a/spec/eee_spec.rb b/spec/eee_spec.rb index 110c32a..a83d5f7 100644 --- a/spec/eee_spec.rb +++ b/spec/eee_spec.rb @@ -148,7 +148,7 @@ it "should ask CouchDB for meal from year YYYY" do RestClient. should_receive(:get). - with(/key=...2009/). + with(%r{by_date.+startkey=.+2009/00/00.+endkey=.+2009/99/99}). and_return('{"rows": [] }') get "/meals/2009" @@ -171,10 +171,10 @@ last_response.should be_ok end - it "should ask CouchDB for meal from year YYYY and month MM" do + it "should ask CouchDB for meals from year YYYY and month MM" do RestClient. should_receive(:get). - with(/key=...2009-05/). + with(%r{by_date.+startkey=.+2009/05/00.+endkey=.+2009/05/99}). and_return('{"rows": [] }') get "/meals/2009/05" diff --git a/spec/views/meal_by_month.haml_spec.rb b/spec/views/meal_by_month.haml_spec.rb index 90f2123..a2867ff 100644 --- a/spec/views/meal_by_month.haml_spec.rb +++ b/spec/views/meal_by_month.haml_spec.rb @@ -2,26 +2,22 @@ describe "meal_by_month.haml" do before(:each) do - assigns[:meals] = { - 'rows' => [ - { "value" => [{ "_id" => '2009-05-14', - "date" => '2009-05-14', - "title" => 'Meal 1', - "summary" => 'Meal 1 Summary', - "menu" => [], - "_attachments" => {"image1.jpg" => { }} - }] - }, - { "value" => [{ "_id" => '2009-05-15', - "date" => '2009-05-15', - "title" => 'Meal 2', - "summary" => 'Meal 2 Summary', - "menu" => %w(foo bar baz), - "_attachments" => {"image2.jpg" => { }} - }] - } - ] - } + assigns[:meals] = + [{ "_id" => '2009-05-14', + "date" => '2009-05-14', + "title" => 'Meal 1', + "summary" => 'Meal 1 Summary', + "menu" => [], + "_attachments" => {"image1.jpg" => { }} + }, + { "_id" => '2009-05-15', + "date" => '2009-05-15', + "title" => 'Meal 2', + "summary" => 'Meal 2 Summary', + "menu" => %w(foo bar baz), + "_attachments" => {"image2.jpg" => { }} + }] + assigns[:year] = 2009 assigns[:month] = '05' assigns[:count_by_year] = [{"key" => "2009-04", "value" => 3}, @@ -63,7 +59,7 @@ end it "should include recipe titles in the menu items" do - assigns[:meals]['rows'][0]['value'][0]["menu"] = + assigns[:meals][0]["menu"] = [" Salad with [recipe:2009/05/23/lemon_dressing] "] self.stub!(:_db).and_return("") diff --git a/spec/views/meal_by_year.haml_spec.rb b/spec/views/meal_by_year.haml_spec.rb index 2eca1da..73ca626 100644 --- a/spec/views/meal_by_year.haml_spec.rb +++ b/spec/views/meal_by_year.haml_spec.rb @@ -2,12 +2,12 @@ describe "meal_by_year.haml" do before(:each) do - assigns[:meals] = { - 'rows' => [ - { "value" => [['2009-05-14', 'Meal 1']]}, - { "value" => [['2009-05-15', 'Meal 2']]}, - ] - } + assigns[:meals] = + [{ 'date' => '2009-05-14', + 'title' => 'Meal 1'}, + { 'date' => '2009-05-15', + 'title' => 'Meal 2'}] + assigns[:year] = 2009 assigns[:count_by_year] = [{"key" => "2008", "value" => 3}, {"key" => "2009", "value" => 3}] diff --git a/views/meal_by_month.haml b/views/meal_by_month.haml index 5a8b264..754184a 100644 --- a/views/meal_by_month.haml +++ b/views/meal_by_month.haml @@ -12,8 +12,7 @@ =link_to_adjacent_view_date(@month, @count_by_year) { |d,v| month_text(d) } .meals - - @meals["rows"].each do |meal_result| - - meal = meal_result['value'][0] + - @meals.each do |meal| - date = Date.parse(meal['date']) = image_link meal, :width => 200, :height => 150 %h2 diff --git a/views/meal_by_year.haml b/views/meal_by_year.haml index 8eca1ed..be5114a 100644 --- a/views/meal_by_year.haml +++ b/views/meal_by_year.haml @@ -12,7 +12,7 @@ =link_to_adjacent_view_date(@year, @count_by_year) %ul.meals - - @meals["rows"].each do |meal| - -date = Date.parse(meal['value'][0][0]) + - @meals.each do |meal| + -date = Date.parse(meal['date']) %li - %a{:href => date.strftime("/meals/%Y/%m/%d")}= meal['value'][0][1] + %a{:href => date.strftime("/meals/%Y/%m/%d")}= meal['title']