Skip to content

Commit

Permalink
tweak will_paginate Rails helper so it can be used in custom context
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Aug 10, 2011
1 parent af48602 commit b385f38
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/will_paginate/view_helpers/action_view.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions spec/view_helpers/action_view_spec.rb
Expand Up @@ -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)
Expand Down

0 comments on commit b385f38

Please sign in to comment.