A class representing a paginator for an Active Record collection.

Namespace
Methods
#
C
E
F
H
L
N
P
Included Modules
Attributes
[R] controller
[R] item_count
[R] items_per_page
Class Public methods
new(controller, item_count, items_per_page, current_page=1)

Creates a new Paginator on the given controller for a set of items of size item_count and having items_per_page items per page. Raises ArgumentError if #items_per_page is out of bounds (i.e., less than or equal to zero). The page CGI parameter for links defaults to “page” and can be overridden with page_parameter.

# File rails/actionpack/lib/action_controller/pagination.rb, line 221
def initialize(controller, item_count, items_per_page, current_page=1)
  raise ArgumentError, 'must have at least one item per page' if
    items_per_page <= 0

  @controller = controller
  @item_count = item_count || 0
  @items_per_page = items_per_page
  @pages = {}
  
  self.current_page = current_page
end
Instance Public methods
[](number)

Returns a new Page representing the page with the given index number.

# File rails/actionpack/lib/action_controller/pagination.rb, line 279
def [](number)
  @pages[number] ||= Page.new(self, number)
end
current()
current_page()

Returns a Page object representing this paginator’s current page.

Also aliased as: current
# File rails/actionpack/lib/action_controller/pagination.rb, line 247
def current_page
  @current_page ||= self[@current_page_number]
end
current_page=(page)

Sets the current page number of this paginator. If page is a Page object, its number attribute is used as the value; if the page does not belong to this Paginator, an ArgumentError is raised.

# File rails/actionpack/lib/action_controller/pagination.rb, line 237
def current_page=(page)
  if page.is_a? Page
    raise ArgumentError, 'Page/Paginator mismatch' unless
      page.paginator == self
  end
  page = page.to_i
  @current_page_number = has_page_number?(page) ? page : 1
end
each(&block)

Successively yields all the paginator’s pages to the given block.

# File rails/actionpack/lib/action_controller/pagination.rb, line 284
def each(&block)
  page_count.times do |n|
    yield self[n+1]
  end
end
first()
first_page()

Returns a new Page representing the first page in this paginator.

Also aliased as: first
# File rails/actionpack/lib/action_controller/pagination.rb, line 253
def first_page
  @first_page ||= self[1]
end
has_page_number?(number)

Returns true if this paginator contains the page of index number.

# File rails/actionpack/lib/action_controller/pagination.rb, line 273
def has_page_number?(number)
  number >= 1 and number <= page_count
end
last()
last_page()

Returns a new Page representing the last page in this paginator.

Also aliased as: last
# File rails/actionpack/lib/action_controller/pagination.rb, line 259
def last_page
  @last_page ||= self[page_count] 
end
length()
page_count()

Returns the number of pages in this paginator.

Also aliased as: length
# File rails/actionpack/lib/action_controller/pagination.rb, line 265
def page_count
  @page_count ||= @item_count.zero? ? 1 :
                    (q,r=@item_count.divmod(@items_per_page); r==0? q : q+1)
end