Class ActionController::Pagination::Paginator
In: vendor/rails/actionpack/lib/action_controller/pagination.rb
Parent: Object

A class representing a paginator for an Active Record collection.

Methods

Included Modules

Enumerable

Classes and Modules

Class ActionController::Pagination::Paginator::Page
Class ActionController::Pagination::Paginator::Window

Attributes

controller  [R] 
item_count  [R] 
items_per_page  [R] 

Public Class methods

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.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/pagination.rb, line 221
221:       def initialize(controller, item_count, items_per_page, current_page=1)
222:         raise ArgumentError, 'must have at least one item per page' if
223:           items_per_page <= 0
224: 
225:         @controller = controller
226:         @item_count = item_count || 0
227:         @items_per_page = items_per_page
228:         @pages = {}
229:         
230:         self.current_page = current_page
231:       end

Public Instance methods

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

[Source]

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

Alias for current_page

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/pagination.rb, line 247
247:       def current_page
248:         @current_page ||= self[@current_page_number]
249:       end

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.

[Source]

     # File vendor/rails/actionpack/lib/action_controller/pagination.rb, line 237
237:       def current_page=(page)
238:         if page.is_a? Page
239:           raise ArgumentError, 'Page/Paginator mismatch' unless
240:             page.paginator == self
241:         end
242:         page = page.to_i
243:         @current_page_number = has_page_number?(page) ? page : 1
244:       end

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

[Source]

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

Alias for first_page

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/pagination.rb, line 253
253:       def first_page
254:         @first_page ||= self[1]
255:       end

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

[Source]

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

Alias for last_page

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/pagination.rb, line 259
259:       def last_page
260:         @last_page ||= self[page_count] 
261:       end
length()

Alias for page_count

Returns the number of pages in this paginator.

[Source]

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

[Validate]