Skip to content

Commit

Permalink
Descending sort order.
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed May 2, 2009
1 parent 720e793 commit 432d2f7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
3 changes: 2 additions & 1 deletion eee.rb
Expand Up @@ -27,7 +27,8 @@
"&skip=#{skip}"

if params[:sort] =~ /\w/
couchdb_url += "&sort=#{params[:sort]}"
order = params[:order] =~ /desc/ ? "%5C" : ""
couchdb_url += "&sort=#{order}#{params[:sort]}"
end

data = RestClient.get couchdb_url
Expand Down
7 changes: 7 additions & 0 deletions features/step_definitions/recipe_search.rb
Expand Up @@ -218,3 +218,10 @@
response.should have_selector("tr:nth-child(3) a",
:content => "delicious recipe 10")
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")
end
14 changes: 12 additions & 2 deletions helpers.rb
Expand Up @@ -90,9 +90,19 @@ def pagination(query, results)
%Q|<div class="pagination">#{links.join}</div>|
end

def sort_link(text, sort_on, query)
def sort_link(text, sort_field, query, results)
id = "sort-by-#{text.downcase}"
url = "/recipes/search?q=#{query}&sort=#{sort_on}"

sort_field_current =
results["sort_order"] &&
results["sort_order"].detect { |sort_options|
sort_options["field"] == sort_field
}

sort = sort_field_current.nil? || sort_field_current["reverse"] ?
sort_field : "#{sort_field}&order=desc"

url = "/recipes/search?q=#{query}&sort=#{sort}"
%Q|<a href="#{url}" id="#{id}">#{text}</a>|
end
end
Expand Down
24 changes: 22 additions & 2 deletions spec/eee_helpers_spec.rb
Expand Up @@ -116,12 +116,32 @@

describe "sort_link" do
it "should link the supplied text" do
sort_link("Foo", "sort_foo", "query").
sort_link("Foo", "sort_foo", "query", { }).
should have_selector("a",
:content => "Foo")
end
it "should link to the query with the supplied sort field" do
sort_link("Foo", "sort_foo", "query").
sort_link("Foo", "sort_foo", "query", { }).
should have_selector("a",
:href => "/recipes/search?q=query&sort=sort_foo")
end

it "should link in descending order if already sorted on the sort field in ascending order" do
results = {
"sort_order" => [{ "field" => "sort_foo",
"reverse" => false}]
}
sort_link("Foo", "sort_foo", "query", results).
should have_selector("a",
:href => "/recipes/search?q=query&sort=sort_foo&order=desc")
end

it "should link in ascending order if already sorted on the sort field in descending order" do
results = {
"sort_order" => [{ "field" => "sort_foo",
"reverse" => true}]
}
sort_link("Foo", "sort_foo", "query", results).
should have_selector("a",
:href => "/recipes/search?q=query&sort=sort_foo")
end
Expand Down
10 changes: 10 additions & 0 deletions spec/eee_spec.rb
Expand Up @@ -121,5 +121,15 @@

get "/recipes/search?q=title:egg&sort="
end

it "should reverse sort when order=desc is supplied" do
RestClient.stub!(:get).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')

RestClient.should_receive(:get).with(/sort=%5C/)

get "/recipes/search?q=title:egg&sort=sort_foo&order=desc"
end

end
end
4 changes: 2 additions & 2 deletions views/search.haml
@@ -1,8 +1,8 @@
%table
%tr
%th
= sort_link("Name", "sort_title", @query)
= sort_link("Date", "sort_date", @query)
= sort_link("Name", "sort_title", @query, @results)
= sort_link("Date", "sort_date", @query, @results)
%th= "Date"
- @results['rows'].each_with_index do |result, i|
%tr{:class => "row#{i % 2}"}
Expand Down

0 comments on commit 432d2f7

Please sign in to comment.