Skip to content
Browse files

rethink the page URL construction mechanizm to be more bulletproof be…

…cause people want to use custom route params for page
  • Loading branch information...
1 parent 4368fe3 commit 1fb293e6d8a2567812af869516b75285cdc3d17f @mislav committed
Showing with 28 additions and 5 deletions.
  1. +17 −4 lib/will_paginate/view_helpers.rb
  2. +1 −0 test/lib/view_test_process.rb
  3. +10 −1 test/view_test.rb
View
21 lib/will_paginate/view_helpers.rb
@@ -270,19 +270,32 @@ def url_for(page)
stringified_merge @url_params, @template.params if @template.request.get?
stringified_merge @url_params, @options[:params] if @options[:params]
- if param_name.index(/[^\w-]/)
+ if complex = param_name.index(/[^\w-]/)
page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest).
parse_query_parameters("#{param_name}=#{page}")
stringified_merge @url_params, page_param
else
- @url_params[param_name] = page
+ @url_params[param_name] = 1
end
url = @template.url_for(@url_params)
- @url_string = url.sub(%r!([?&/]#{CGI.escape param_name}[=/])#{page}!, '\1@')
- return url
+
+ if complex
+ @url_string = url.sub(%r!([?&]#{CGI.escape param_name}=)#{page}!, '\1@')
+ return url
+ else
+ @url_string = url
+ @url_params[param_name] = 2
+ @template.url_for(@url_params).split(//).each_with_index do |char, i|
+ if char == '2' and url[i, 1] == '1'
+ @url_string[i] = '@'
+ break
+ end
+ end
+ end
end
+ # finally!
@url_string.sub '@', page.to_s
end
View
1 test/lib/view_test_process.rb
@@ -6,6 +6,7 @@
ActionController::Routing::Routes.draw do |map|
map.connect 'dummy/page/:page', :controller => 'dummy'
+ map.connect 'dummy/dots/page.:page', :controller => 'dummy', :action => 'dots'
map.connect ':controller/:action/:id'
end
View
11 test/view_test.rb
@@ -234,6 +234,15 @@ def test_custom_routing_page_param
end
end
+ def test_custom_routing_page_param_with_dot_separator
+ @request.symbolized_path_parameters.update :controller => 'dummy', :action => 'dots'
+ 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
@@ -336,7 +345,7 @@ def assert_links_match pattern, links = nil, numbers = nil
end
end
- assert_equal pages, numbers, "page numbers don't match" if numbers
+ assert_equal numbers, pages, "page numbers don't match" if numbers
end
def assert_no_links_match pattern

0 comments on commit 1fb293e

Please sign in to comment.
Something went wrong with that request. Please try again.