Skip to content

Commit

Permalink
Add support for page parameter in custom routes like "/foo/page/2"
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Apr 21, 2008
1 parent fb38b83 commit 4d2da56
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
@@ -1,3 +1,9 @@
== 2.2.2, released 2008-04-21

* Add support for page parameter in custom routes like "/foo/page/2"
* Change output of "page_entries_info" on single-page collection and erraneous
output with empty collection as reported by Tim Chater

== 2.2.1, released 2008-04-08

* take less risky path when monkeypatching named_scope; fix that it no longer
Expand Down
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -114,7 +114,7 @@ contributions or just simply awesome ideas:
Chris Wanstrath, Dr. Nic Williams, K. Adam Christensen, Mike Garey, Bence
Golda, Matt Aimonetti, Charles Brian Quinn, Desi McAdam, James Coglan, Matijs
van Zuijlen, Maria, Brendan Ribera, Todd Willey, Bryan Helmkamp, Jan Berkel,
Lourens Naudé, Rick Olson.
Lourens Naudé, Rick Olson, Russell Norris.


== Usable pagination in the UI
Expand Down
2 changes: 1 addition & 1 deletion lib/will_paginate/view_helpers.rb
Expand Up @@ -280,7 +280,7 @@ def url_for(page)
end

url = @template.url_for(@url_params)
@url_string = url.sub(/([?&]#{CGI.escape param_name}=)#{page}/, '\1@')
@url_string = url.sub(%r!([?&/]#{CGI.escape param_name}[=/])#{page}!, '\1@')
return url
end
@url_string.sub '@', page.to_s
Expand Down
8 changes: 4 additions & 4 deletions test/lib/view_test_process.rb
Expand Up @@ -5,15 +5,19 @@
WillPaginate.enable_actionpack

ActionController::Routing::Routes.draw do |map|
map.connect 'dummy/page/:page', :controller => 'dummy'
map.connect ':controller/:action/:id'
end

ActionController::Base.perform_caching = false

class DummyRequest
attr_accessor :symbolized_path_parameters

def initialize
@get = true
@params = {}
@symbolized_path_parameters = { :controller => 'foo', :action => 'bar' }
end

def get?
Expand All @@ -24,10 +28,6 @@ def post
@get = false
end

def symbolized_path_parameters
{ :controller => 'foo', :action => 'bar' }
end

def relative_url_root
''
end
Expand Down
19 changes: 18 additions & 1 deletion test/view_test.rb
Expand Up @@ -220,6 +220,15 @@ def test_complex_custom_page_param
end
end

def test_custom_routing_page_param
@request.symbolized_path_parameters.update :controller => 'dummy', :action => nil
paginate :per_page => 2 do
assert_select 'a[href]', 6 do |links|
assert_links_match %r{/page/(\d+)$}, links, [2, 3, 4, 5, 6, 2]
end
end
end

## internal hardcore stuff ##

class LegacyCollection < WillPaginate::Collection
Expand Down Expand Up @@ -307,14 +316,22 @@ def validate_page_numbers expected, links, param_name = :page
})
end

def assert_links_match pattern, links = nil
def assert_links_match pattern, links = nil, numbers = nil
links ||= assert_select 'div.pagination a[href]' do |elements|
elements
end

pages = [] if numbers

links.each do |el|
assert_match pattern, el['href']
if numbers
el['href'] =~ pattern
pages << $1.to_i
end
end

assert_equal pages, numbers, "page numbers don't match" if numbers
end

def assert_no_links_match pattern
Expand Down

0 comments on commit 4d2da56

Please sign in to comment.