Skip to content

Instantly share code, notes, and snippets.

@rayboyd
Created March 21, 2009 14:11
Show Gist options
  • Save rayboyd/82855 to your computer and use it in GitHub Desktop.
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.
#
# 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&#45;%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] = '&laquo; Prev'
WillPaginate::ViewHelpers.pagination_options[:next_label] = 'Next &raquo;'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment