From 331ec0d47bc78ecf453ba83238041ac7cb912621 Mon Sep 17 00:00:00 2001 From: eee-c Date: Thu, 23 Apr 2009 22:52:32 -0400 Subject: [PATCH] First pass at pagination --- features/recipe_search.feature | 2 +- features/step_definitions/recipe_search.rb | 4 ++++ helpers.rb | 10 ++++++++++ spec/eee_helpers_spec.rb | 15 +++++++++++++++ spec/views/search.haml_spec.rb | 13 +++++++++---- views/search.haml | 4 +++- 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/features/recipe_search.feature b/features/recipe_search.feature index db019da..9ced0f6 100644 --- a/features/recipe_search.feature +++ b/features/recipe_search.feature @@ -55,7 +55,7 @@ Feature: Search for recipes And a 0.5 second wait to allow the search index to be updated When I search for "yummy" Then I should see 20 results - And 3 pages of results + And I should see 3 pages of results And I should not be able to go to a previous page When I click page 3 Then I should see 10 results diff --git a/features/step_definitions/recipe_search.rb b/features/step_definitions/recipe_search.rb index 9c3f2df..29a9f5c 100644 --- a/features/step_definitions/recipe_search.rb +++ b/features/step_definitions/recipe_search.rb @@ -153,3 +153,7 @@ Then /^I should see (\d+) results$/ do |count| response.should have_selector("table a", :count => count.to_i) end + +Then /^I should see 3 pages of results$/ do + response.should have_selector(".pagination a", :content => "3") +end diff --git a/helpers.rb b/helpers.rb index a79765c..39501b3 100644 --- a/helpers.rb +++ b/helpers.rb @@ -52,5 +52,15 @@ def image_link(doc) %Q|| end + + def pagination(skip, limit, total) + total_pages = (total + limit - 1) / limit + + links = (1..total_pages).map do |page| + %Q|#{page}| + end + + %Q|| + end end end diff --git a/spec/eee_helpers_spec.rb b/spec/eee_helpers_spec.rb index dc139dc..3f3a220 100644 --- a/spec/eee_helpers_spec.rb +++ b/spec/eee_helpers_spec.rb @@ -68,3 +68,18 @@ image_link(doc).should be_nil end end + +describe "pagination" do + it "should have a link to other pages" do + pagination(0, 20, 41). + should have_selector("a", :content => "2") + end + it "should have 3 pages, when results.size > 2 * page size" do + pagination(0, 20, 41). + should have_selector("a", :content => "3") + end + it "should have only 2 pages, when results.size == 2 * page size" do + pagination(0, 20, 40). + should_not have_selector("a", :content => "3") + end +end diff --git a/spec/views/search.haml_spec.rb b/spec/views/search.haml_spec.rb index e6ac2d1..e07e2ef 100644 --- a/spec/views/search.haml_spec.rb +++ b/spec/views/search.haml_spec.rb @@ -2,15 +2,12 @@ describe "search.haml" do before(:each) do - assigns[:results] = - @results = { - 'rows' => + assigns[:results] = @results = [ { '_id' => 'id-one', 'title' => 'One', 'date' => '2009-04-15' }, { '_id' => 'id-two', 'title' => 'Two', 'date' => '2009-04-14' }, { '_id' => 'id-three', 'title' => 'Three', 'date' => '2009-04-13' }, ] - } end it "should display the recipe's title" do @@ -40,4 +37,12 @@ render("/views/search.haml") response.should have_selector("td", :content => '2009-04-15') end + + it "should pass skip, limit, and total count to pagination helper" do + assigns[:skip] = 1 + assigns[:limit] = 2 + assigns[:total] = 3 + self.should_receive(:pagination).with(1,2,3) + render("/views/search.haml") + end end diff --git a/views/search.haml b/views/search.haml index c71895c..e36a222 100644 --- a/views/search.haml +++ b/views/search.haml @@ -2,8 +2,10 @@ %tr %th= "Name" %th= "Date" - - @results['rows'].each_with_index do |result, i| + - @results.each_with_index do |result, i| %tr{:class => "row#{i % 2}"} %td %a{:href => "/recipes/#{result['_id']}"}= result['title'] %td= result['date'] + += pagination(@skip, @limit, @total)