Module | ActionController::Pagination |
In: |
lib/action_controller/pagination.rb
|
The Pagination module aids in the process of paging large collections of Active Record objects. It offers macro-style automatic fetching of your model for multiple views, or explicit fetching for single actions. And if the magic isn’t flexible enough for your needs, you can create your own paginators with a minimal amount of code.
The Pagination module can handle as much or as little as you wish. In the controller, have it automatically query your model for pagination; or, if you prefer, create Paginator objects yourself.
Pagination is included automatically for all controllers.
For help rendering pagination links, see ActionView::Helpers::PaginationHelper.
class PersonController < ApplicationController model :person paginate :people, :order => 'last_name, first_name', :per_page => 20 # ... end
Each action in this controller now has access to a @people instance variable, which is an ordered collection of model objects for the current page (at most 20, sorted by last name and first name), and a @person_pages Paginator instance. The current page is determined by the @params[‘page’] variable.
def list @person_pages, @people = paginate :people, :order => 'last_name, first_name' end
Like the previous example, but explicitly creates @person_pages and @people for a single action, and uses the default of 10 items per page.
def list @person_pages = Paginator.new self, Person.count, 10, @params['page'] @people = Person.find :all, :order => 'last_name, first_name', :limit => @person_pages.items_per_page, :offset => @person_pages.current.offset end
Explicitly creates the paginator from the previous example and uses Paginator#to_sql to retrieve @people from the model.
OPTIONS | = | Hash.new | A hash holding options for controllers using macro-style pagination | |
DEFAULT_OPTIONS | = | { :class_name => nil, :singular_name => nil, :per_page => 10, :conditions => nil, :order_by => nil, :order => nil, :join => nil, :joins => nil, :include => nil, :select => nil, :parameter => 'page' | The default options for pagination |
Returns a paginator and a collection of Active Record model instances for the paginator’s current page. This is designed to be used in a single action; to automatically paginate multiple actions, consider ClassMethods#paginate.
options are:
:singular_name: | the singular name to use, if it can’t be inferred by |
singularizing the collection name
:class_name: | the class name to use, if it can’t be inferred by camelizing the singular name |
:per_page: | the maximum number of items to include in a single page. Defaults to 10 |
:conditions: | optional conditions passed to Model.find(:all, *params) and Model.count |
:order: | optional order parameter passed to Model.find(:all, *params) |
:order_by: | (deprecated, used :order) optional order parameter passed to Model.find(:all, *params) |
:joins: | optional joins parameter passed to Model.find(:all, *params) and Model.count |
:join: | (deprecated, used :joins or :include) optional join parameter passed to Model.find(:all, *params) and Model.count |
:include: | optional eager loading parameter passed to Model.find(:all, *params) and Model.count |
Returns the total number of items in the collection to be paginated for the model and given conditions. Override this method to implement a custom counter.