Class: Paginator
- Defined in:
- lib/active_scaffold/paginator.rb,
lib/active_scaffold/extensions/paginator_extensions.rb
Defined Under Namespace
Classes: ArgumentError, MissingCountError, MissingSelectError, Page
Constant Summary collapse
- VERSION =
'1.0.9'
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
Instance Method Summary collapse
-
#each ⇒ Object
Iterate through pages.
-
#each_with_index ⇒ Object
Iterate through pages with indices.
-
#first ⇒ Object
First page object.
-
#infinite? ⇒ Boolean
Is this an “infinite” paginator.
-
#initialize(count, per_page, &select) ⇒ Paginator
constructor
Instantiate a new Paginator object.
-
#last ⇒ Object
Last page object.
-
#number_of_pages ⇒ Object
Total number of pages.
-
#number_of_pages_with_infinite ⇒ Object
Total number of pages.
-
#page(number) ⇒ Object
Retrieve page object by number.
Constructor Details
#initialize(count, per_page, &select) ⇒ Paginator
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)
-
21 22 23 24 25 26 27 |
# File 'lib/active_scaffold/paginator.rb', line 21 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 |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
10 11 12 |
# File 'lib/active_scaffold/paginator.rb', line 10 def count @count end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
10 11 12 |
# File 'lib/active_scaffold/paginator.rb', line 10 def per_page @per_page end |
Instance Method Details
#each ⇒ Object
Iterate through pages
45 46 47 48 49 |
# File 'lib/active_scaffold/paginator.rb', line 45 def each each_with_index do |item, _| yield item end end |
#each_with_index ⇒ Object
Iterate through pages with indices
52 53 54 55 56 |
# 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 |
#first ⇒ Object
First page object
35 36 37 |
# File 'lib/active_scaffold/paginator.rb', line 35 def first page 1 end |
#infinite? ⇒ Boolean
Is this an “infinite” paginator
11 12 13 |
# File 'lib/active_scaffold/extensions/paginator_extensions.rb', line 11 def infinite? @count.nil? end |
#last ⇒ Object
Last page object
40 41 42 |
# File 'lib/active_scaffold/paginator.rb', line 40 def last page number_of_pages end |
#number_of_pages ⇒ Object
Total number of pages
30 31 32 |
# File 'lib/active_scaffold/paginator.rb', line 30 def number_of_pages (@count / @per_page).to_i + (@count % @per_page > 0 ? 1 : 0) end |
#number_of_pages_with_infinite ⇒ Object
Total number of pages
5 6 7 |
# File 'lib/active_scaffold/extensions/paginator_extensions.rb', line 5 def number_of_pages_with_infinite number_of_pages_without_infinite if @count end |
#page(number) ⇒ Object
Retrieve page object by number
59 60 61 62 63 64 65 66 67 |
# File 'lib/active_scaffold/paginator.rb', line 59 def page(number) number = (n = number.to_i) > 0 ? n : 1 Page.new(self, number) do offset = (number - 1) * @per_page args = [offset] args << @per_page if @select.arity == 2 @select.call(*args) end end |