class Paginator

Constants

VERSION

Attributes

count[R]
per_page[R]

Public Instance Methods

each() { |item| ... } click to toggle source

Iterate through pages

# File lib/active_scaffold/paginator.rb, line 46
def each
  each_with_index do |item, index|
    yield item
  end
end
each_with_index() { |page(number), number - 1| ... } click to toggle source

Iterate through pages with indices

# File lib/active_scaffold/paginator.rb, line 53
def each_with_index
  1.upto(number_of_pages) do |number|
    yield(page(number), number - 1)
  end
end
first() click to toggle source

First page object

# File lib/active_scaffold/paginator.rb, line 36
def first
  page 1
end
infinite?() click to toggle source

Is this an “infinite” paginator

# File lib/active_scaffold/extensions/paginator_extensions.rb, line 12
def infinite?
  @count.nil?
end
last() click to toggle source

Last page object

# File lib/active_scaffold/paginator.rb, line 41
def last
  page number_of_pages
end
number_of_pages() click to toggle source

Total number of pages

# File lib/active_scaffold/paginator.rb, line 31
def number_of_pages
   (@count / @per_page).to_i + (@count % @per_page > 0 ? 1 : 0)
end
number_of_pages_with_infinite() click to toggle source

Total number of pages

# File lib/active_scaffold/extensions/paginator_extensions.rb, line 6
def number_of_pages_with_infinite
  number_of_pages_without_infinite unless infinite?
end
page(number) click to toggle source

Retrieve page object by number

# File lib/active_scaffold/paginator.rb, line 60
def page(number)
  number = (n = number.to_i) > 0 ? n : 1
  Page.new(self, number, lambda { 
    offset = (number - 1) * @per_page
    args = [offset]
    args << @per_page if @select.arity == 2
    @select.call(*args)
  })
end

Public Class Methods

new(count, per_page, &select) click to toggle source

Instantiate a new Paginator object

Provide:

  • A total count of the number of objects to paginate

  • The number of objects in each page

  • A block that returns the array of items

    • The block is passed the item offset (and the number of items to show per page, for convenience, if the arity is 2)

# File lib/active_scaffold/paginator.rb, line 22
def initialize(count, per_page, &select)
  @count, @per_page = count, per_page
  unless select
    raise MissingSelectError, "Must provide block to select data for each page"
  end
  @select = select
end