module Kaminari
# = Helpers
module ActionViewExtension
# A helper that renders the pagination links.
#
# <%= paginate @articles %>
#
# ==== Options
# * :window - The "inner window" size (4 by default).
# * :outer_window - The "outer window" size (0 by default).
# * :left - The "left outer window" size (0 by default).
# * :right - The "right outer window" size (0 by default).
# * :params - url_for parameters for the links (:controller, :action, etc.)
# * :param_name - parameter name for page number in the links (:page by default)
# * :remote - Ajax? (false by default)
# * :ANY_OTHER_VALUES - Any other hash key & values would be directly passed into each tag as :locals value.
def paginate(scope, options = {}, &block)
paginator = Kaminari::Helpers::Paginator.new self, options.reverse_merge(:current_page => scope.current_page, :num_pages => scope.num_pages, :per_page => scope.limit_value, :param_name => Kaminari.config.param_name, :remote => false)
paginator.to_s
end
# A simple "Twitter like" pagination link that creates a link to the next page.
#
# ==== Examples
# Basic usage:
#
# <%= link_to_next_page @items, 'Next Page' %>
#
# Ajax:
#
# <%= link_to_next_page @items, 'Next Page', :remote => true %>
#
# By default, it renders nothing if there are no more results on the next page.
# You can customize this output by passing a block.
#
# <%= link_to_next_page @users, 'Next Page' do %>
# No More Pages
# <% end %>
def link_to_next_page(scope, name, options = {}, &block)
params = options.delete(:params) || {}
param_name = options.delete(:param_name) || Kaminari.config.param_name
link_to_unless scope.last_page?, name, params.merge(param_name => (scope.current_page + 1)), options.reverse_merge(:rel => 'next') do
block.call if block
end
end
# Renders a helpful message with numbers of displayed vs. total entries.
# Ported from mislav/will_paginate
#
# ==== Examples
# Basic usage:
#
# <%= page_entries_info @posts %>
# #-> Displaying posts 6 - 10 of 26 in total
#
# By default, the message will use the humanized class name of objects
# in collection: for instance, "project types" for ProjectType models.
# Override this with the :entry_name parameter:
#
# <%= page_entries_info @posts, :entry_name => 'item' %>
# #-> Displaying items 6 - 10 of 26 in total
def page_entries_info(collection, options = {})
entry_name = options[:entry_name] || (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
if collection.num_pages < 2
case collection.total_count
when 0; "No #{entry_name.pluralize} found"
when 1; "Displaying 1 #{entry_name}"
else; "Displaying all #{collection.total_count} #{entry_name.pluralize}"
end
else
offset = (collection.current_page - 1) * collection.limit_value
%{Displaying #{entry_name.pluralize} %d - %d of %d in total} % [
offset + 1,
offset + collection.count,
collection.total_count
]
end
end
end
end