Created
March 21, 2009 14:11
-
-
Save rayboyd/82855 to your computer and use it in GitHub Desktop.
Renders a helpful message with numbers of displayed vs. total entries. Drop into config/init folder and restart the server.
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
# | |
# WillPaginate::ViewHelpers | |
# | |
module WillPaginate | |
module ViewHelpers | |
# | |
# Renders a helpful message with numbers of displayed vs. total entries. | |
# | |
# + Only displayed if the collection.size is within the range of | |
# + 0 to collection.per_page variable. This makes for a very relevant | |
# + and concise output stream. | |
# | |
# <%= page_entries_info @posts %> | |
# #-> Viewing [a]-[b] of [n] [model name] | |
# | |
def page_entries_info(collection, options = {}) | |
entry_name = options[:entry_name] || | |
(collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' ')) | |
if collection.total_pages < 2 | |
case collection.size | |
when 0..collection.per_page; nil | |
else; "<p><i>Viewing <b>#{collection.size}</b> #{entry_name.pluralize.capitalize}</i></p>" | |
end | |
else | |
# Should really use an en dash here (8211). Crap looking tho. | |
%{<p><i>Viewing <b>%d-%d</b> of <b>%d</b> #{entry_name.pluralize}</i></p>} % [ | |
collection.offset + 1, | |
collection.offset + collection.length, | |
collection.total_entries | |
] | |
end | |
end | |
end # module | |
end # class | |
# | |
# Override some default view helpers | |
# | |
WillPaginate::ViewHelpers.pagination_options[:inner_window] = 2 | |
WillPaginate::ViewHelpers.pagination_options[:outer_window] = 1 | |
WillPaginate::ViewHelpers.pagination_options[:previous_label] = '« Prev' | |
WillPaginate::ViewHelpers.pagination_options[:next_label] = 'Next »' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment