Skip to content

Commit

Permalink
Added #href_for_pagin helper method to help build out pagenated hrefs…
Browse files Browse the repository at this point in the history
… based on the current request.

Updated gemspec, think this was a hangover from a previous release attempt
  • Loading branch information
nowk committed Feb 15, 2010
1 parent 409afa4 commit f9dc92c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
14 changes: 14 additions & 0 deletions lib/sinatra/pagin.rb
Expand Up @@ -25,6 +25,20 @@ def page(pg = 1)

@page || 1
end

def href_for_pagin(total_pages, direction = :next)
path_info = request.path_info.gsub(/\/$/, '') # clear off the last slash just in case
page_num = 1

case
when direction === :next
page_num = page+1 >= total_pages ? total_pages : page+1
when direction === :prev
page_num = page-1 <= 0 ? 1 : page-1
end

path_info+"/page/#{page_num}"
end
end
end

Expand Down
1 change: 1 addition & 0 deletions sinatra-pagin.gemspec
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|
"Rakefile",
"VERSION",
"lib/sinatra/pagin.rb",
"sinatra-pagin.gemspec",
"spec/pagin_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb"
Expand Down
96 changes: 88 additions & 8 deletions spec/pagin_spec.rb
Expand Up @@ -140,7 +140,7 @@ def get_absolute(uri)
describe "Helpers" do
include Sinatra::Pagin::Helpers

describe "current_page" do
describe "page" do
it "should return 1 if @page.nil?" do
page.should == 1
end
Expand All @@ -152,15 +152,95 @@ def get_absolute(uri)
end
end

it "should actually work inside the app" do
app.get "/get/page/value/if" do
"The page is #{page}"
context "within an actual app" do
before(:all) do
app.get "/get/page/value/if" do
"The page is #{page}"
end
end

get "/get/page/value/if/page/123"
last_response.should be_ok
last_response.body.should == "The page is 123"
last_request.url.should == "http://example.org/get/page/value/if"
it "should actually work inside the app" do
get "/get/page/value/if/page/123"
last_response.should be_ok
last_response.body.should == "The page is 123"
last_request.url.should == "http://example.org/get/page/value/if"
end
end
end

describe "href_for_pagin" do
describe "" do
before(:each) do
stub!(:request)
request.stub!(:path_info).and_return '/2009/10'
end

it "should return a paginated uri based on the current request" do
page 2

next_page = href_for_pagin(3, :next)
prev_page = href_for_pagin(3, :prev)

next_page.should == "/2009/10/page/3"
prev_page.should == "/2009/10/page/1"
end

it "should return ../page/1 for :prev if page is 1" do
page 1
href_for_pagin(3, :prev).should == "/2009/10/page/1"
end

it "should return ../page/*total_pages for :next if page is the last page" do
page 3
href_for_pagin(3, :next).should == "/2009/10/page/3"
end
end

context "within an actual app" do
before(:all) do
app.get "/this/path/should/be/base" do
total_pages = 4
<<-HTML
<a href="#{href_for_pagin(total_pages, :prev)}">previous</a> |
<a href="#{href_for_pagin(total_pages, :next)}">next</a>
HTML
end
end

it "should return the proper hrefs given ../page/1" do
get "/this/path/should/be/base/page/1"
last_response.should be_ok
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/1" do |a|
a.inner_text.should == 'previous'
end
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/2" do |a|
a.inner_text.should == 'next'
end
end

[2, 3].each do |p|
it "should return the proper hrefs given ../page/#{p}" do
get "/this/path/should/be/base/page/#{p}"
last_response.should be_ok
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/#{p-1}" do |a|
a.inner_text.should == 'previous'
end
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/#{p+1}" do |a|
a.inner_text.should == 'next'
end
end
end

it "should return the proper hrefs given ../page/4" do
get "/this/path/should/be/base/page/4"
last_response.should be_ok
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/3" do |a|
a.inner_text.should == 'previous'
end
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/4" do |a|
a.inner_text.should == 'next'
end
end
end
end
end
Expand Down

0 comments on commit f9dc92c

Please sign in to comment.