Skip to content

Instantly share code, notes, and snippets.

@raihan2006i
Forked from henrik/will_paginate.rb
Last active January 3, 2016 14:29
Show Gist options
  • Save raihan2006i/8476998 to your computer and use it in GitHub Desktop.
Save raihan2006i/8476998 to your computer and use it in GitHub Desktop.
bootstrap 3 and bootstrap 2 renderer for will_paginate
/ 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 }
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment