Last active
January 12, 2019 16:51
-
-
Save anujmiddha/2742ddf54e6d55dfaf4f22b731e2f5ed to your computer and use it in GitHub Desktop.
will_paginate custom LinkRenderer with Semantic UI
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
module ApplicationHelper | |
# change the default link renderer for will_paginate | |
def will_paginate(collection_or_options = nil, options = {}) | |
if collection_or_options.is_a? Hash | |
options, collection_or_options = collection_or_options, nil | |
end | |
unless options[:renderer] | |
options = options.merge :renderer => SemanticUiLinkRenderer | |
end | |
super *[collection_or_options, options].compact | |
end | |
end |
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
class SemanticUiLinkRenderer < WillPaginate::ActionView::LinkRenderer | |
def container_attributes | |
{class: "ui pagination menu"} | |
end | |
def page_number(page) | |
if page == current_page | |
link(page, page, rel: rel_value(page), class: "active item") | |
else | |
link(page, page, rel: rel_value(page), class: "item") | |
end | |
end | |
def previous_page | |
num = @collection.current_page > 1 && @collection.current_page - 1 | |
previous_or_next_page(num, "Previous") | |
end | |
def next_page | |
num = @collection.current_page < total_pages && @collection.current_page + 1 | |
previous_or_next_page(num, "Next") | |
end | |
def gap | |
text = @template.will_paginate_translate(:page_gap) { '…' } | |
%(<div class="disabled item">#{text}</div>) | |
end | |
def previous_or_next_page(page, text) | |
if page | |
link(text, page, class: "item") | |
else | |
"<div class=\"disabled item\">#{text}</div>" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment