Created
July 19, 2011 23:46
-
-
Save jsmpereira/1094024 to your computer and use it in GitHub Desktop.
Padrino pagination
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
# | |
# Adapted from http://pastebin.com/FL5KeQQH | |
# | |
# As mentioned on the link above put this on app/helpers/pagination.rb | |
# and you can use in your view | |
# | |
# =will_paginate @posts | |
# | |
# I'm also using this https://gist.github.com/837683 | |
# | |
# This code allows for the helper to recognize | |
# custom page params and multiple pagination on the same page. | |
# That way you can also: | |
# | |
# =will_paginate @posts, :param_name => "post_page" | |
# | |
# Also it should deal correctly with multiple GET params being them | |
# page params or otherwise. | |
# | |
# I bet there's a more elegant way to approach this, but regex is not | |
# my forte. | |
# | |
# Hope this is useful. Please do contribute ;) | |
require 'will_paginate/view_helpers/base' | |
require 'will_paginate/view_helpers/link_renderer' | |
WillPaginate::ViewHelpers::LinkRenderer.class_eval do | |
protected | |
def url(page) | |
url = @template.request.url | |
if page == 1 | |
if url =~ /[?+]#{param_name}=[0-9][&+]/ # ?PAGE=7&somevar=abcd | |
url = url.gsub(/[?+]#{param_name}=[0-9][&+]+/, '?') | |
elsif url =~ /[&+]#{param_name}=[0-9][&+]/ # ?somevar=1234&PAGE=7&somevar=abcd | |
url = url.gsub(/[&+]#{param_name}=[0-9][&+]+/, '&') | |
elsif url =~ /[&+]#{param_name}=[0-9]/ # ?somevar=1234&somevar=abcd&PAGE=7 | |
url = url.gsub(/[&+]#{param_name}=[0-9]+/, '') | |
elsif | |
url = url.gsub(/[?+]#{param_name}=[0-9]+/, '') # ?PAGE=7 and no more params | |
end | |
url | |
else | |
if url =~ /#{param_name}=[0-9]+/ | |
url.gsub(/#{param_name}=[0-9]+/, "#{param_name}=#{page}") | |
else | |
if @template.request.params.empty? | |
url + "?#{param_name}=#{page}" | |
else | |
url + "&#{param_name}=#{page}" | |
end | |
end | |
end | |
end | |
end | |
include WillPaginate::ViewHelpers::Base |
Cool. Thanks for the feedback.
I'm going to try to do some refactoring soon. Cleanup a bit. Too much regex.
Yeah.. I think building the URL directly from the params might be best..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey man I've been having problems with this sometimes not over-riding the base will paginate properly, but switching the include to
helpers WillPaginate::ViewHelpers::Base
and moving that above the class_eval block seems to have solved them for now..