Module: ActionController::Pagination

Defined in:
lib/action_controller/pagination.rb

Overview

Action Pack pagination for Active Record collections

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', 
            :conditions => @person_pages.current.to_sql
end

Explicitly creates the paginator from the previous example and uses Paginator#to_sql to retrieve @people from the model.

Defined Under Namespace

Modules: ClassMethods Classes: Paginator

Constant Summary collapse

OPTIONS =

A hash holding options for controllers using macro-style pagination

Hash.new
DEFAULT_OPTIONS =

The default options for pagination

{
  :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'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:


79
80
81
82
# File 'lib/action_controller/pagination.rb', line 79

def self.included(base) #:nodoc:
  super
  base.extend(ClassMethods)
end

.validate_options!(collection_id, options, in_action) ⇒ Object

:nodoc:

Raises:

  • (ActionController::ActionControllerError)

84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/action_controller/pagination.rb', line 84

def self.validate_options!(collection_id, options, in_action) #:nodoc:
  options.merge!(DEFAULT_OPTIONS) {|key, old, new| old}

  valid_options = DEFAULT_OPTIONS.keys
  valid_options << :actions unless in_action

  unknown_option_keys = options.keys - valid_options
  raise ActionController::ActionControllerError,
        "Unknown options: #{unknown_option_keys.join(', ')}" unless
          unknown_option_keys.empty?

  options[:singular_name] ||= Inflector.singularize(collection_id.to_s)
  options[:class_name]  ||= Inflector.camelize(options[:singular_name])
end

Instance Method Details

#paginate(collection_id, options = {}) ⇒ Object

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


121
122
123
124
# File 'lib/action_controller/pagination.rb', line 121

def paginate(collection_id, options={})
  Pagination.validate_options!(collection_id, options, true)
  paginator_and_collection_for(collection_id, options)
end