This file contains 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
/ app/views/posts/index.html.haml | |
/ Centered pagination in Bootstrap 3.x | |
.text-center | |
= will_paginate @posts, {:class => 'pagination', :version => :bootstrap3 } | |
/ Centered pagination in Bootstrap 2.x | |
= will_paginate @posts, {:class => 'pagination pagination-centered', :version => :bootstrap2 } |
This file contains 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
# config/initializers/will_paginate.rb | |
module WillPaginate | |
module ActionView | |
def will_paginate(collection = nil, options = {}) | |
options[:version] ||= :bootstrap2 | |
if options[:version] == :bootstrap3 | |
options[:renderer] ||= BootstrapLinkRenderer3 | |
else | |
options[:renderer] ||= BootstrapLinkRenderer2 | |
end | |
options.delete(:version) | |
super.try :html_safe | |
end | |
class BootstrapLinkRenderer2 < LinkRenderer | |
protected | |
def html_container(html) | |
tag :div, tag(:ul, html), container_attributes | |
end | |
def page_number(page) | |
tag :li, link(page, page, :rel => rel_value(page)), :class => ('active' if page == current_page) | |
end | |
def previous_or_next_page(page, text, classname) | |
tag :li, link(text, page || '#'), :class => [classname[0..3], classname, ('disabled' unless page)].join(' ') | |
end | |
def gap | |
tag :li, link(super, '#'), :class => 'disabled' | |
end | |
end | |
class BootstrapLinkRenderer3 < BootstrapLinkRenderer2 | |
protected | |
def html_container(html) | |
tag :ul, html, container_attributes | |
end | |
end | |
end | |
end |