Class: ActionController::Pagination::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/action_controller/pagination.rb

Overview

A class representing a paginator for an Active Record collection.

Defined Under Namespace

Classes: Page, Window

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, item_count, items_per_page, current_page = 1) ⇒ Paginator

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.

Raises:

  • (ArgumentError)


214
215
216
217
218
219
220
221
222
223
224
# File 'lib/action_controller/pagination.rb', line 214

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 Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



225
226
227
# File 'lib/action_controller/pagination.rb', line 225

def controller
  @controller
end

#item_countObject (readonly)

Returns the value of attribute item_count.



225
226
227
# File 'lib/action_controller/pagination.rb', line 225

def item_count
  @item_count
end

#items_per_pageObject (readonly)

Returns the value of attribute items_per_page.



225
226
227
# File 'lib/action_controller/pagination.rb', line 225

def items_per_page
  @items_per_page
end

Instance Method Details

#[](number) ⇒ Object

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



272
273
274
# File 'lib/action_controller/pagination.rb', line 272

def [](number)
  @pages[number] ||= Page.new(self, number)
end

#current_pageObject Also known as: current

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



240
241
242
# File 'lib/action_controller/pagination.rb', line 240

def current_page
  @current_page ||= self[@current_page_number]
end

#current_page=(page) ⇒ Object

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.



230
231
232
233
234
235
236
237
# File 'lib/action_controller/pagination.rb', line 230

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) ⇒ Object

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



277
278
279
280
281
# File 'lib/action_controller/pagination.rb', line 277

def each(&block)
  page_count.times do |n|
    yield self[n+1]
  end
end

#first_pageObject Also known as: first

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



246
247
248
# File 'lib/action_controller/pagination.rb', line 246

def first_page
  @first_page ||= self[1]
end

#has_page_number?(number) ⇒ Boolean

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

Returns:

  • (Boolean)


266
267
268
# File 'lib/action_controller/pagination.rb', line 266

def has_page_number?(number)
  number >= 1 and number <= page_count
end

#last_pageObject Also known as: last

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



252
253
254
# File 'lib/action_controller/pagination.rb', line 252

def last_page
  @last_page ||= self[page_count] 
end

#page_countObject Also known as: length

Returns the number of pages in this paginator.



258
259
260
261
# File 'lib/action_controller/pagination.rb', line 258

def page_count
  @page_count ||= @item_count.zero? ? 1 :
                    (q,r=@item_count.divmod(@items_per_page); r==0? q : q+1)
end