Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tweak will_paginate Rails helper so it can be used in custom context

  • Loading branch information...
commit b385f386b2613797f391903e8179a098cb4da1e6 1 parent af48602
@mislav authored
View
32 lib/will_paginate/view_helpers/action_view.rb
@@ -72,14 +72,18 @@ def paginated_section(*args, &block)
end
def will_paginate_translate(keys, options = {})
- if Array === keys
- defaults = keys.dup
- key = defaults.shift
+ if respond_to? :translate
+ if Array === keys
+ defaults = keys.dup
+ key = defaults.shift
+ else
+ defaults = nil
+ key = keys
+ end
+ translate(key, options.merge(:default => defaults, :scope => :will_paginate))
else
- defaults = nil
- key = keys
+ super
end
- translate(key, options.merge(:default => defaults, :scope => :will_paginate))
end
protected
@@ -101,9 +105,8 @@ def default_url_params
def url(page)
@base_url_params ||= begin
- url_params = base_url_params
+ url_params = merge_get_params(default_url_params)
merge_optional_params(url_params)
- url_params
end
url_params = @base_url_params.dup
@@ -112,15 +115,16 @@ def url(page)
@template.url_for(url_params)
end
- def base_url_params
- url_params = default_url_params
- # page links should preserve GET parameters
- symbolized_update(url_params, @template.params) if get_request?
+ def merge_get_params(url_params)
+ if @template.respond_to? :request and @template.request and @template.request.get?
+ symbolized_update(url_params, @template.params)
+ end
url_params
end
def merge_optional_params(url_params)
symbolized_update(url_params, @options[:params]) if @options[:params]
+ url_params
end
def add_current_page_param(url_params, page)
@@ -132,10 +136,6 @@ def add_current_page_param(url_params, page)
end
end
- def get_request?
- @template.request.get?
- end
-
private
def parse_query_parameters(params)
View
39 spec/view_helpers/action_view_spec.rb
@@ -293,6 +293,45 @@ def renderer.gap() '<span class="my-gap">~~</span>' end
end
end
+ it "renders using ActionView helpers on a custom object" do
+ helper = Object.new
+ class << helper
+ attr_reader :controller
+ include ActionView::Helpers::UrlHelper
+ include Routes.url_helpers
+ include WillPaginate::ActionView
+ end
+ helper.default_url_options[:controller] = 'dummy'
+
+ collection = WillPaginate::Collection.new(2, 1, 3)
+ @render_output = helper.will_paginate(collection)
+
+ assert_select 'a[href]', 4 do |links|
+ urls = links.map {|l| l['href'] }.uniq
+ urls.should == ['/dummy/page/1', '/dummy/page/3']
+ end
+ end
+
+ it "renders using ActionDispatch helper on a custom object" do
+ helper = Object.new
+ class << helper
+ include ActionDispatch::Routing::UrlFor
+ include Routes.url_helpers
+ include WillPaginate::ActionView
+ end
+ helper.default_url_options[:host] = 'example.com'
+ helper.default_url_options[:controller] = 'dummy'
+ # helper.default_url_options[:only_path] = true
+
+ collection = WillPaginate::Collection.new(2, 1, 3)
+ @render_output = helper.will_paginate(collection)
+
+ assert_select 'a[href]', 4 do |links|
+ urls = links.map {|l| l['href'] }.uniq
+ urls.should == ['http://example.com/dummy/page/1', 'http://example.com/dummy/page/3']
+ end
+ end
+
private
def translation(data)
Please sign in to comment.
Something went wrong with that request. Please try again.