Skip to content

Commit

Permalink
another fix with pagination and custom routing: account for that the …
Browse files Browse the repository at this point in the history
…first page number may be implicit
  • Loading branch information
mislav committed May 4, 2008
1 parent 43abd3b commit 802d830
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/will_paginate/view_helpers.rb
Expand Up @@ -297,7 +297,8 @@ def page_span(page, text, attributes = {})
# Returns URL params for +page_link_or_span+, taking the current GET params
# and <tt>:params</tt> option into account.
def url_for(page)
unless @url_string
page_one = page == 1
unless @url_string and !page_one
@url_params = { :escape => false }
# page links should preserve GET parameters
stringified_merge @url_params, @template.params if @template.request.get?
Expand All @@ -309,19 +310,20 @@ def url_for(page)

stringified_merge @url_params, page_param
else
@url_params[param_name] = 1
@url_params[param_name] = page_one ? 1 : 2
end

url = @template.url_for(@url_params)
return url if page_one

if complex
@url_string = url.sub(%r!([?&]#{CGI.escape param_name}=)#{page}!, '\1@')
return url
else
@url_string = url
@url_params[param_name] = 2
@url_params[param_name] = 3
@template.url_for(@url_params).split(//).each_with_index do |char, i|
if char == '2' and url[i, 1] == '1'
if char == '3' and url[i, 1] == '2'
@url_string[i] = '@'
break
end
Expand Down
6 changes: 5 additions & 1 deletion test/lib/view_test_process.rb
Expand Up @@ -7,6 +7,10 @@
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 'ibocorp/:page', :controller => 'ibocorp',
:requirements => { :page => /\d+/ },
:defaults => { :page => 1 }

map.connect ':controller/:action/:id'
end

Expand Down Expand Up @@ -79,7 +83,7 @@ def assert_links_match pattern, links = nil, numbers = nil
assert_match pattern, el['href']
if numbers
el['href'] =~ pattern
pages << $1.to_i
pages << ($1.nil?? nil : $1.to_i)
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/view_test.rb
Expand Up @@ -287,6 +287,15 @@ def test_custom_routing_page_param_with_dot_separator
end
end

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

## internal hardcore stuff ##

class LegacyCollection < WillPaginate::Collection
Expand Down

0 comments on commit 802d830

Please sign in to comment.