Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Finish off paginiation
  • Loading branch information
eee-c committed Apr 27, 2009
1 parent 49a8320 commit e402d13
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
9 changes: 7 additions & 2 deletions eee.rb
Expand Up @@ -17,12 +17,17 @@
end

get '/recipes/search' do
skip = (((params[:page] ? params[:page].to_i : 1) - 1) * 20) + 1
page = params[:page].to_i
skip = (page < 2) ? 0 : ((page - 1) * 20) + 1
data = RestClient.get "#{@@db}/_fti?limit=20&skip=#{skip}&q=#{params[:q]}"
@results = JSON.parse(data)

@query = params[:q]

if @results['rows'].size == 0 && page > 1
redirect("/recipes/search?q=#{@query}")
return
end

haml :search
end

Expand Down
25 changes: 22 additions & 3 deletions features/step_definitions/recipe_search.rb
Expand Up @@ -129,7 +129,8 @@
end

When /^I search for "(.+)"$/ do |keyword|
visit("/recipes/search?q=#{keyword}")
@query = "/recipes/search?q=#{keyword}"
visit(@query)
end

When /^I search titles for "(.+)"$/ do |keyword|
Expand All @@ -144,6 +145,14 @@
click_link(page)
end

When /^I click the (.+) page$/ do |link_text|
click_link(link_text)
end

When /^I visit page \"?(.+?)\"?$/ do |page|
visit(@query + "&page=#{page}")
end

Then /^I should see the "(.+)" recipe in the search results$/ do |title|
response.should have_selector("a",
:href => "/recipes/id-#{title.gsub(/\W/, '-')}",
Expand All @@ -162,7 +171,17 @@
response.should have_selector(".pagination a", :content => "3")
end

Then /^I should not be able to go to a previous page$/ do
response.should have_selector(".pagination span", :content => "« Previous")
Then /^I should not be able to go to a (.+) page$/ do |link_text|
response.should have_selector(".pagination span",
:content => link_text.capitalize())
end

Then /^I should be able to go to a (.+) page$/ do |link_text|
response.should have_selector(".pagination a",
:content => link_text.capitalize())
end

Then /^I should see page (.+)$/ do |page|
response.should have_selector(".pagination span.current",
:content => page)
end
6 changes: 5 additions & 1 deletion helpers.rb
Expand Up @@ -73,7 +73,11 @@ def pagination(query, results)
end

links << (1..last_page).map do |page|
%Q|<a href="#{link}&page=#{page}">#{page}</a>|
if page == current_page
%Q|<span class="current">#{page}</span>|
else
%Q|<a href="#{link}&page=#{page}">#{page}</a>|
end
end

links <<
Expand Down
4 changes: 4 additions & 0 deletions spec/eee_helpers_spec.rb
Expand Up @@ -108,4 +108,8 @@
pagination(@query, @results).
should have_selector("span", :content => "« Previous")
end
it "should mark the current page" do
pagination(@query, @results).
should have_selector("span.current", :content => "1")
end
end
8 changes: 8 additions & 0 deletions spec/eee_spec.rb
Expand Up @@ -96,5 +96,13 @@

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

it "should display page 1 when passing a bad page number" do
RestClient.should_receive(:get).
with(/skip=0/).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')

get "/recipes/search?q=title:eggs&page=foo"
end
end
end

0 comments on commit e402d13

Please sign in to comment.