Created
February 7, 2019 02:37
-
-
Save laurenashpole/0e3a734703d6168e89f5038c2734ee63 to your computer and use it in GitHub Desktop.
A Rails helper for using render_to_string with will_paginate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module WillPaginateHelper | |
class StringLinkRenderer < WillPaginate::ActionView::LinkRenderer | |
protected | |
def url(page) | |
@base_url_params ||= begin | |
url_params = merge_get_params(default_url_params) | |
url_params[:only_path] = true | |
merge_optional_params(url_params) | |
end | |
url_params = @base_url_params.dup | |
add_current_page_param(url_params, page) | |
if @options[:url_builder] | |
@options[:url_builder].call(url_params) | |
else | |
@template.url_for(url_params) | |
end | |
end | |
end | |
def will_paginate_string(collection = nil, options = {}, request = nil) | |
params = { | |
:host => request[:host], | |
:controller => request[:controller], | |
:action => request[:action] | |
} | |
unless request.query_parameters.empty? | |
request.query_parameters.each do |k, v| | |
params[k] = v | |
end | |
end | |
options.merge!( | |
:renderer => WillPaginateHelper::StringLinkRenderer, | |
:url_builder => ->(params) { Rails.application.routes.url_for(params) }, | |
:params => params | |
) | |
will_paginate(collection, options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment