Action Pack pagination for Active Record collections
DEPRECATION WARNING: Pagination will be moved to a plugin in Rails 2.0. Install the classic_pagination plugin for forward compatibility:
script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination
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.
Automatic pagination for every action in a controller
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.
Pagination for a single action
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.
Custom/“classic” pagination
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, :count => 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 ActionController::Pagination::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
:select
-
:select parameter passed to Model.find(:all, *params)
:count
-
parameter passed as :select option to Model.count(*params)
Source: show
# File rails/actionpack/lib/action_controller/pagination.rb, line 130 def paginate(collection_id, options={}) Pagination.validate_options!(collection_id, options, true) paginator_and_collection_for(collection_id, options) end
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.
Source: show
# File rails/actionpack/lib/action_controller/pagination.rb, line 178 def count_collection_for_pagination(model, options) model.count(:conditions => options[:conditions], :joins => options[:join] || options[:joins], :include => options[:include], :select => options[:count]) end
Returns a collection of items for the given model
and +options+, ordered by +options+, for the current page in the given
paginator
. Override this method to implement a custom finder.
Source: show
# File rails/actionpack/lib/action_controller/pagination.rb, line 188 def find_collection_for_pagination(model, options, paginator) model.find(:all, :conditions => options[:conditions], :order => options[:order_by] || options[:order], :joins => options[:join] || options[:joins], :include => options[:include], :select => options[:select], :limit => options[:per_page], :offset => paginator.current.offset) end