Skip to content

Commit

Permalink
rethink the page URL construction mechanizm to be more bulletproof be…
Browse files Browse the repository at this point in the history
…cause people want to use custom route params for page
  • Loading branch information
mislav committed Apr 23, 2008
1 parent 4368fe3 commit 1fb293e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
21 changes: 17 additions & 4 deletions lib/will_paginate/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions test/lib/view_test_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 10 additions & 1 deletion test/view_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1fb293e

Please sign in to comment.