Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed a bug in the helper#page method

  • Loading branch information...
commit b920930c2660375c4b225cd9769e62c2d9a32ecb 1 parent 04b0ad4
@nowk authored
Showing with 20 additions and 4 deletions.
  1. +7 −2 lib/sinatra/pagin.rb
  2. +13 −2 spec/pagin_spec.rb
View
9 lib/sinatra/pagin.rb
@@ -9,7 +9,8 @@ def self.registered(app)
page_pattern = /\/page\/(\d+)(\/)?(\.[^\.\/]+)?$/
request.path_info.match(page_pattern)
- if @page = $1
+ if $1
+ page $1
request.path_info = request.path_info.gsub!(page_pattern, '')+$3.to_s
request.path_info.gsub!(/^(?!\/)/) { |s| "/"+s } # force the first slash if not avail
end
@@ -17,7 +18,11 @@ def self.registered(app)
end
module Helpers
- def page
+ def page(pg = 1)
+ unless pg == 1
+ @page = pg.to_i
+ end
+
@page || 1
end
end
View
15 spec/pagin_spec.rb
@@ -167,10 +167,21 @@ def get_absolute(uri)
[1, 21, 302, "4", "501", "602"].each do |p|
it "should return value of @page if @page is #{p}" do
- @page = p
- page.should == p
+ page p
+ page.should == p.to_i
end
end
+
+ it "should actually work inside the app" do
+ app.get "/get/page/value/if" do
+ "The page is #{page}"
+ 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"
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.