Skip to content

Instantly share code, notes, and snippets.

@minikomi
Forked from jsmpereira/gist:1094024
Created July 20, 2011 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minikomi/1094800 to your computer and use it in GitHub Desktop.
Save minikomi/1094800 to your computer and use it in GitHub Desktop.
Padrino pagination
#
# 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
@minikomi
Copy link
Author

note - Padrino Will Paginate with custom selectors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment