class Paginator
Constants
- VERSION
Attributes
Public Class Methods
Source
# File lib/active_scaffold/paginator.rb, line 21 def initialize(count, per_page, &select) @count = count @per_page = per_page raise MissingSelectError, 'Must provide block to select data for each page' unless select @select = select end
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)
-
Public Instance Methods
Source
# File lib/active_scaffold/paginator.rb, line 45 def each each_with_index do |item, _| yield item end end
Iterate through pages
Source
# File lib/active_scaffold/paginator.rb, line 52 def each_with_index 1.upto(number_of_pages) do |number| yield(page(number), number - 1) end end
Iterate through pages with indices
Source
# File lib/active_scaffold/paginator.rb, line 35 def first page 1 end
First page object
Source
# File lib/active_scaffold/paginator.rb, line 40 def last page number_of_pages end
Last page object
Source
# File lib/active_scaffold/paginator.rb, line 30 def number_of_pages (@count / @per_page).to_i + ((@count % @per_page).positive? ? 1 : 0) end
Total number of pages
Source
# File lib/active_scaffold/paginator.rb, line 59 def page(number) number = [1, number.to_i].max Page.new(self, number) do offset = (number - 1) * @per_page args = [offset] args << @per_page if @select.arity == 2 @select.call(*args) end end
Retrieve page object by number