Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2012 15:54
Show Gist options
  • Save anonymous/4277363 to your computer and use it in GitHub Desktop.
Save anonymous/4277363 to your computer and use it in GitHub Desktop.
#use = will_paginate @users, :method => :post, :params => params.slice("q", "keyword"), :form_action => search_service_plans_path
require 'will_paginate/view_helpers/action_view'
module WillPaginate
module ActionView
def will_paginate(collection = nil, options = {})
options, collection = collection, nil if collection.is_a? Hash
# Taken from original will_paginate code to handle if the helper is not passed a collection object.
collection ||= infer_collection_from_controller
options[:renderer] ||= BootstrapLinkRenderer
super.try :html_safe
end
class BootstrapLinkRenderer < LinkRenderer
def to_html
html = pagination.map do |item|
item.is_a?(Fixnum) ?
page_number(item) :
send(item)
end.join(@options[:link_separator])
html += tag(:li, link(@template.page_entries_info(@collection), "#", :onclick => "return false;"), :class => "disabled next next_page")
@options[:container] ? html_container(html) : html
end
protected
def html_container(html)
tag :div, tag(:ul, html) + post_form, container_attributes
end
def post_form
if @options[:method] == :post
tag(:form, post_form_fields,
:action => @options[:form_action],
:class => "paginate_form hide", :method => "post") + post_paginate_js
else
""
end
end
def post_form_fields
@template.utf8_enforcer_tag + @template.hidden_field_tag("authenticity_token", @template.form_authenticity_token) + hidden_field_tags(@options[:params])
end
def post_paginate_js
@template.javascript_tag <<-EOF
$(function() {
$(".pagination a[data-page]").click(function() {
var page = $(this).data("page");
var ele = $("<input/>", {
name: "page",
value: page
});
$(this).parents(".pagination").find("form.paginate_form").append(ele).submit();
})
})
EOF
end
def hidden_field_tags(hash, namespace = nil)
text = ""
hash.each do |k, value|
case value
when Array
value.each do |item|
text << @template.hidden_field_tag("#{input_key(k, namespace)}[]", item)
end
when Hash
text << hidden_field_tags(value, input_key(k, namespace))
else
text << @template.hidden_field_tag(input_key(k, namespace), value)
end
end
text.html_safe
end
def input_key(key, namespace = nil)
if namespace
"#{namespace}[#{key}]"
else
key
end
end
def page_number(page)
if @options[:method] == :post
tag :li, link(page, "javascript:void(0)", "data-page" => page, :rel => rel_value(page)), :class => ('active' if page == current_page)
else
tag :li, link(page, page, :rel => rel_value(page)), :class => ('active' if page == current_page)
end
end
def gap
tag :li, link(super, '#'), :class => 'disabled'
end
def previous_or_next_page(page, text, classname)
if @options[:method] == :post
tag :li, link(text, "javascript:void(0)", "data-page" => page ? page : nil), :class => [classname[0..3], classname, ('disabled' unless page)].join(' ')
else
tag :li, link(text, page || '#'), :class => [classname[0..3], classname, ('disabled' unless page)].join(' ')
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment