Skip to content

Commit

Permalink
First pass at pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed Apr 24, 2009
1 parent e90481c commit 331ec0d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion features/recipe_search.feature
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions features/step_definitions/recipe_search.rb
Expand Up @@ -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
10 changes: 10 additions & 0 deletions helpers.rb
Expand Up @@ -52,5 +52,15 @@ def image_link(doc)

%Q|<img src="/images/#{doc['_id']}/#{filename}"/>|
end

def pagination(skip, limit, total)
total_pages = (total + limit - 1) / limit

links = (1..total_pages).map do |page|
%Q|<a href="">#{page}</a>|
end

%Q|<div class="pagination">#{links.join}</div>|
end
end
end
15 changes: 15 additions & 0 deletions spec/eee_helpers_spec.rb
Expand Up @@ -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
13 changes: 9 additions & 4 deletions spec/views/search.haml_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion views/search.haml
Expand Up @@ -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)

0 comments on commit 331ec0d

Please sign in to comment.