Last active
December 25, 2015 21:29
-
-
Save softr8/7042584 to your computer and use it in GitHub Desktop.
faster kaminary pagination view helper replacement
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
def faster_paginate collection | |
output = '' | |
if collection.num_pages > 1 | |
output << "<nav class='pagination'>" | |
page_base = collection.current_page | |
if page_base > 4 | |
output << content_tag(:span, class: 'first') do | |
link_to raw(t 'views.pagination.first'), url_for(params.merge(page: 1)) | |
end | |
output << content_tag(:span, class: 'page gap') { '... '} | |
end | |
(page_base <= 3 ? 1 : page_base - 3 ).upto(page_base > collection.num_pages - 3 ? collection.num_pages : page_base + 3) do |page| | |
output << content_tag(:span, class: "page") do | |
link_to_unless (collection.current_page == page), page.to_s, url_for(params.merge(page: page)) | |
end | |
end | |
if page_base < collection.num_pages - 3 | |
output << content_tag(:span, class: 'page gap') { '... '} | |
output << content_tag(:span, class: 'last') do | |
link_to raw(t 'views.pagination.last'), url_for(params.merge(page: collection.num_pages)) | |
end | |
end | |
output << "</nav>" | |
end | |
output.html_safe | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment