Skip to content

Instantly share code, notes, and snippets.

@artanikin
Created October 21, 2016 19:05
Show Gist options
  • Save artanikin/87af8422ac3db73e5d14d6b0528866ce to your computer and use it in GitHub Desktop.
Save artanikin/87af8422ac3db73e5d14d6b0528866ce to your computer and use it in GitHub Desktop.
Will_paginate bootstrap 4 renderer
module BootstrapPaginationHelper
class LinkRenderer < WillPaginate::ActionView::LinkRenderer
protected
def page_number(page)
if page == current_page
link(page, "#", :class => 'active')
else
link(page, page, :rel => rel_value(page))
end
end
def gap
text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
%(<li class="page-item disabled"><a>#{text}</a></li>)
end
def next_page
num = @collection.current_page < @collection.total_pages && @collection.current_page + 1
previous_or_next_page(num, @options[:next_label], 'next')
end
def previous_page
num = (@collection.current_page - 1 > 0) && @collection.current_page - 1
previous_or_next_page(num, @options[:previous_label], 'previous')
end
def previous_or_next_page(page, text, classname)
if page
link(text, page, :class => classname)
else
link(text, "#", :class => classname + ' disabled')
end
end
def html_container(html)
tag(:nav, tag(:ul, html), container_attributes)
end
private
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
unless target == "#"
attributes[:href] = target
end
attributes[:class] ||= ''
classname = attributes[:class]
attributes[:class] += ' page-link'
tag(:li, tag(:a, text, attributes), :class => classname + ' page-item')
end
end
end
@artanikin
Copy link
Author

  1. Add in you Gemfile gem 'will_paginate', '~> 3.1.0'
  2. Add to this gist to config/initializers folder
  3. In your template call this helper <%= will_paginate @collections, renderer: 'BootstrapPaginationHelper::LinkRenderer' %>

@guihatano
Copy link

Thanks, I just needed to change a little thing for bootstrap 4.0.0.alpha6. Check it out on my fork. Greetings!

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