Skip to content

Commit

Permalink
Propagate sorting through pagination.
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed May 3, 2009
1 parent 432d2f7 commit bc2530c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion eee.rb
Expand Up @@ -20,7 +20,7 @@
@query = params[:q]

page = params[:page].to_i
skip = (page < 2) ? 0 : ((page - 1) * 20) + 1
skip = (page < 2) ? 0 : ((page - 1) * 20)

couchdb_url = "#{@@db}/_fti?limit=20" +
"&q=#{@query}" +
Expand Down
16 changes: 12 additions & 4 deletions features/step_definitions/recipe_search.rb
Expand Up @@ -208,6 +208,7 @@
end

Then /^I should see page (.+)$/ do |page|
@page = page.to_i
response.should have_selector(".pagination span.current",
:content => page)
end
Expand All @@ -220,8 +221,15 @@
end

Then /^the results should be ordered by name in descending order$/ do
response.should have_selector("tr:nth-child(2) a",
:content => "delicious recipe 9")
response.should have_selector("tr:nth-child(3) a",
:content => "delicious recipe 8")
if @page == 2
response.should have_selector("tr:nth-child(2) a",
:content => "delicious recipe 36")
response.should have_selector("tr:nth-child(3) a",
:content => "delicious recipe 35")
else
response.should have_selector("tr:nth-child(2) a",
:content => "delicious recipe 9")
response.should have_selector("tr:nth-child(3) a",
:content => "delicious recipe 8")
end
end
7 changes: 7 additions & 0 deletions helpers.rb
Expand Up @@ -63,6 +63,13 @@ def pagination(query, results)

link = "/recipes/search?q=#{query}"

if results['sort_order']
link += "&sort=#{results['sort_order'].first['field']}"
if results['sort_order'].first['reverse']
link += "&order=desc"
end
end

links = []

links <<
Expand Down
19 changes: 19 additions & 0 deletions spec/eee_helpers_spec.rb
Expand Up @@ -112,6 +112,25 @@
pagination(@query, @results).
should have_selector("span.current", :content => "1")
end
context "with sorting applied" do
before(:each) do
@results["sort_order"] = [{ "field" => "sort_foo",
"reverse" => false}]
end
it "should have a link to other pages with sorting applied" do
pagination(@query, @results).
should have_selector("a",
:content => "2",
:href => "/recipes/search?q=foo&sort=sort_foo&page=2")
end
it "should have a link to other pages with reverse sorting applied" do
@results["sort_order"].first["reverse"] = true
pagination(@query, @results).
should have_selector("a",
:content => "2",
:href => "/recipes/search?q=foo&sort=sort_foo&order=desc&page=2")
end
end
end

describe "sort_link" do
Expand Down
2 changes: 1 addition & 1 deletion spec/eee_spec.rb
Expand Up @@ -91,7 +91,7 @@

it "should paginate" do
RestClient.should_receive(:get).
with(/skip=21/).
with(/skip=20/).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')

get "/recipes/search?q=title:eggs&page=2"
Expand Down

0 comments on commit bc2530c

Please sign in to comment.