Skip to content

Instantly share code, notes, and snippets.

@xijo
Created August 24, 2011 21:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xijo/1169244 to your computer and use it in GitHub Desktop.
Save xijo/1169244 to your computer and use it in GitHub Desktop.
WillPaginate custom link renderer for twitters bootstrap pagination structure
class BootstrapPaginationRenderer < WillPaginate::ViewHelpers::LinkRenderer
include ActionView::Helpers::UrlHelper
def to_html
@current_page = @collection.current_page.to_i
links = @options[:page_links] ? windowed_links : []
links.unshift(page_link_or_span(previous_page, 'prev', @options[:previous_label].html_safe))
links.push(page_link_or_span(next_page, 'next', @options[:next_label].html_safe))
html = links.join(@options[:separator])
@template.content_tag(:div, content_tag(:ul, html.html_safe), :class => "pagination")
end
protected
def windowed_links
windowed_page_numbers.map { |n| page_link_or_span(n, (n == current_page ? 'active' : nil)) }
end
def page_link_or_span(page, span_class, text = nil)
text ||= page.to_s
page_link(page, text, :class => span_class)
end
def previous_or_next_page(page, text, classname)
page || @current_page
end
def page_link(page, text, attributes = {})
@template.content_tag(:li, @template.link_to(text, @template.params.merge("page" => page)), attributes)
end
def page_span(page, text, attributes = {})
@template.content_tag(:li, text, attributes)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment