Skip to content

Instantly share code, notes, and snippets.

@fabdbt
Created December 21, 2015 08:51
Show Gist options
  • Save fabdbt/98a7e1c915f96c245fe7 to your computer and use it in GitHub Desktop.
Save fabdbt/98a7e1c915f96c245fe7 to your computer and use it in GitHub Desktop.
Bootstrap 4 will paginate
module PaginateHelper
class BootstrapRenderer < WillPaginate::ActionView::LinkRenderer
def prepare collection, options, template
options[:params] ||= Hash.new
options[:params]['_'] = nil
super(collection, options, template)
end
protected
def html_container html
tag(:nav, tag(:ul, html, class: 'pagination'))
end
def previous_or_next_page page, text, classname
classname = [classname, 'page-link'].compact.join(' ')
if page
link(text, page, class: classname)
else
tag(:li, tag(:a, text, class: classname), class: 'disabled page-item')
end
end
def page_number page
unless page == current_page
tag(:li, link(page, page, rel: rel_value(page)), class: 'page-item')
else
tag(:li, tag(:a, page, class: 'page-link'), class: 'page-item active')
end
end
def link text, target, **attributes
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
attributes[:class] = [attributes[:class], 'page-link'].compact.join(' ')
tag(:li, @template.link_to(text.to_s.html_safe, target, attributes), class: 'page-item')
end
def gap
tag(:li, tag(:a, '...', class: 'page-link'), class: 'page-item')
end
end
# Optional helper to use Bootstrap pagination
def bootstrap_will_paginate collection, **options
will_paginate(collection, options.merge(renderer: PaginateHelper::BootstrapRenderer))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment