Class: MongoModel::Paginator
- Inherits:
-
Array
- Object
- Array
- MongoModel::Paginator
- Defined in:
- lib/mongomodel/support/paginator.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total_entries ⇒ Object
readonly
Returns the value of attribute total_entries.
Instance Method Summary collapse
-
#initialize(scope, page, per_page) ⇒ Paginator
constructor
A new instance of Paginator.
- #next_page ⇒ Object
- #offset ⇒ Object
- #out_of_bounds? ⇒ Boolean
- #previous_page ⇒ Object
- #total_pages ⇒ Object
Constructor Details
#initialize(scope, page, per_page) ⇒ Paginator
Returns a new instance of Paginator.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/mongomodel/support/paginator.rb', line 5 def initialize(scope, page, per_page) @current_page = page.to_i @per_page = per_page.to_i super(scope.offset(offset).limit(per_page)) # Try to autodetect total entries if total_entries.nil? && size < per_page && (current_page == 1 or size > 0) @total_entries = offset + size else @total_entries = scope.count end end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
3 4 5 |
# File 'lib/mongomodel/support/paginator.rb', line 3 def current_page @current_page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
3 4 5 |
# File 'lib/mongomodel/support/paginator.rb', line 3 def per_page @per_page end |
#total_entries ⇒ Object (readonly)
Returns the value of attribute total_entries.
3 4 5 |
# File 'lib/mongomodel/support/paginator.rb', line 3 def total_entries @total_entries end |
Instance Method Details
#next_page ⇒ Object
27 28 29 |
# File 'lib/mongomodel/support/paginator.rb', line 27 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset ⇒ Object
35 36 37 |
# File 'lib/mongomodel/support/paginator.rb', line 35 def offset (current_page - 1) * per_page end |
#out_of_bounds? ⇒ Boolean
31 32 33 |
# File 'lib/mongomodel/support/paginator.rb', line 31 def out_of_bounds? current_page > total_pages end |
#previous_page ⇒ Object
23 24 25 |
# File 'lib/mongomodel/support/paginator.rb', line 23 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#total_pages ⇒ Object
19 20 21 |
# File 'lib/mongomodel/support/paginator.rb', line 19 def total_pages total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil end |