Class: Might::Paginator
- Inherits:
-
Object
- Object
- Might::Paginator
- Defined in:
- lib/might/paginator.rb
Overview
Paginates ActiveRecord scopes As a side effect of pagination it defines the following methods on collection:
collection#limit
collection#offset
collection#count
collection#total_count
Constant Summary collapse
- InvalidLimitOrOffset =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Paginator
constructor
A new instance of Paginator.
-
#paginate(collection) ⇒ ActiveRecord::CollectionProxy
Paginate given collection.
Constructor Details
#initialize(options = {}) ⇒ Paginator
Returns a new instance of Paginator.
23 24 25 26 27 28 |
# File 'lib/might/paginator.rb', line 23 def initialize( = {}) @limit = Integer(.fetch(:limit)) @offset = Integer(.fetch(:offset)) fail InvalidLimitOrOffset if @limit < 0 || @offset < 0 end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
17 18 19 |
# File 'lib/might/paginator.rb', line 17 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
17 18 19 |
# File 'lib/might/paginator.rb', line 17 def offset @offset end |
Instance Method Details
#paginate(collection) ⇒ ActiveRecord::CollectionProxy
Paginate given collection
34 35 36 |
# File 'lib/might/paginator.rb', line 34 def paginate(collection) collection.offset(offset).limit(limit) end |