Skip to content

Commit

Permalink
Remaining sorting and pagination steps.
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed May 5, 2009
1 parent bc2530c commit b2fa43d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 24 deletions.
29 changes: 29 additions & 0 deletions features/step_definitions/recipe_search.rb
Expand Up @@ -233,3 +233,32 @@
:content => "delicious recipe 8")
end
end

Then /^the results should be ordered by date in descending order$/ do
response.should have_selector("tr:nth-child(2) .date",
:content => "2008-06-17")
response.should have_selector("tr:nth-child(3) .date",
:content => "2008-06-16")
end

Then /^the results should be ordered by date in ascending order$/ do
response.should have_selector("tr:nth-child(2) .date",
:content => "2008-04-29")
response.should have_selector("tr:nth-child(3) .date",
:content => "2008-04-30")
end

Then /^the results should be ordered by preparation time in ascending order$/ do
response.should have_selector("tr:nth-child(2) .prep",
:content => "1")
response.should have_selector("tr:nth-child(3) .prep",
:content => "2")
end

Then /^the results should be ordered by the number of ingredients in ascending order$/ do
response.should have_selector("tr:nth-child(2) .ingredients",
:content => "ingredient 1")
response.should have_selector("tr:nth-child(3) .ingredients",
:content => "ingredient 1, ingredient 2")
end

12 changes: 8 additions & 4 deletions features/support/env.rb
Expand Up @@ -61,11 +61,15 @@
idx(doc);
ret.field('sort_title', doc['title'], 'yes', 'not_analyzed');
ret.field('sort_date', doc['date'], 'yes', 'not_analyzed');
ret.field('sort_title', doc['title'], 'yes', 'not_analyzed');
ret.field('sort_date', doc['date'], 'yes', 'not_analyzed');
ret.field('sort_prep', doc['prep_time'], 'yes', 'not_analyzed');
ret.field('date', doc['date'], 'yes');
ret.field('title', doc['title'], 'yes');
ret.field('sort_ingredient', doc['preparations'].length, 'yes', 'not_analyzed');
ret.field('date', doc['date'], 'yes');
ret.field('title', doc['title'], 'yes');
ret.field('prep_time', doc['prep_time'], 'yes');
return ret;
}
Expand Down
14 changes: 10 additions & 4 deletions helpers.rb
Expand Up @@ -97,19 +97,25 @@ def pagination(query, results)
%Q|<div class="pagination">#{links.join}</div>|
end

def sort_link(text, sort_field, query, results)
def sort_link(text, sort_field, results, options = { })
id = "sort-by-#{text.downcase}"

query = options[:query] || results['query']

# Current state of sort on the requested field
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"
if sort_field_current
order = sort_field_current["reverse"] ? "" : "&order=desc"
elsif options[:reverse]
order = "&order=desc"
end

url = "/recipes/search?q=#{query}&sort=#{sort}"
url = "/recipes/search?q=#{query}&sort=#{sort_field}#{order}"
%Q|<a href="#{url}" id="#{id}">#{text}</a>|
end
end
Expand Down
39 changes: 27 additions & 12 deletions spec/eee_helpers_spec.rb
Expand Up @@ -134,34 +134,49 @@
end

describe "sort_link" do
before(:each) do
@current_results = { }
end

it "should link the supplied text" do
sort_link("Foo", "sort_foo", "query", { }).
sort_link("Foo", "sort_foo", @current_results, :query => "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", @current_results, :query => "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).
@current_results["sort_order"] =
[{ "field" => "sort_foo",
"reverse" => false }]

sort_link("Foo", "sort_foo", @current_results, :query => "query").
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).
@current_results["sort_order"] =
[{ "field" => "sort_foo",
"reverse" => true }]

sort_link("Foo", "sort_foo", @current_results, :query => "query").
should have_selector("a",
:href => "/recipes/search?q=query&sort=sort_foo")
end

it "should link to descending sort if instructed to reverse" do
sort_link("Foo",
"sort_foo",
@current_results,
:query => "query",
:reverse => true).
should have_selector("a",
:href => "/recipes/search?q=query&sort=sort_foo&order=desc")
end

end
17 changes: 13 additions & 4 deletions views/search.haml
@@ -1,13 +1,22 @@
%table
%tr
%th
= sort_link("Name", "sort_title", @query, @results)
= sort_link("Date", "sort_date", @query, @results)
%th= "Date"
= sort_link("Name", "sort_title", @results, :query => @query)
%th
= sort_link("Date", "sort_date", @results, :query => @query, :reverse => true)
%th
= sort_link("Prep", "sort_prep", @results, :query => @query)
%th
= sort_link("Ingredients", "sort_ingredient", @results, :query => @query)
- @results['rows'].each_with_index do |result, i|
%tr{:class => "row#{i % 2}"}
%td
%a{:href => "/recipes/#{result['_id']}"}= result['title']
%td= result['date']
%td
%span.date= result['date']
%td
%span.prep= result['prep_time'].to_i
%td
%span.ingredients= result['ingredient']

= pagination(@query, @results)

0 comments on commit b2fa43d

Please sign in to comment.