Permalink
Browse files

another fix with pagination and custom routing: account for that the …

…first page number may be implicit
  • Loading branch information...
1 parent 43abd3b commit 802d8301923e570bc43ae61320fd02b223e22e8f @mislav committed May 4, 2008
Showing with 20 additions and 5 deletions.
  1. +6 −4 lib/will_paginate/view_helpers.rb
  2. +5 −1 test/lib/view_test_process.rb
  3. +9 −0 test/view_test.rb
@@ -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?
@@ -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
@@ -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
@@ -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
View
@@ -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

0 comments on commit 802d830

Please sign in to comment.