Class: Warped::Queries::Paginate
- Inherits:
-
Object
- Object
- Warped::Queries::Paginate
- Defined in:
- lib/warped/queries/paginate.rb
Overview
Paginate a scope
This class provides a way to paginate a scope and return the metadata.
To see the maximum number of records per page and the default number of records per page, check the MAX_PER_PAGE
and DEFAULT_PER_PAGE
constants.
Constant Summary collapse
- MAX_PER_PAGE =
100
- DEFAULT_PER_PAGE =
10
Class Method Summary collapse
-
.call(scope, page: nil, per_page: nil) ⇒ Array<Hash, ActiveRecord::Relation>
The metadata and the paginated scope.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(scope, page: nil, per_page: nil) ⇒ Array<Hash, ActiveRecord::Relation>
constructor
The metadata and the paginated scope.
Constructor Details
#initialize(scope, page: nil, per_page: nil) ⇒ Array<Hash, ActiveRecord::Relation>
Returns the metadata and the paginated scope.
36 37 38 39 40 41 |
# File 'lib/warped/queries/paginate.rb', line 36 def initialize(scope, page: nil, per_page: nil) super() @scope = scope @page = page @per_page = per_page end |
Class Method Details
.call(scope, page: nil, per_page: nil) ⇒ Array<Hash, ActiveRecord::Relation>
Returns the metadata and the paginated scope.
28 29 30 |
# File 'lib/warped/queries/paginate.rb', line 28 def self.call(scope, page: nil, per_page: nil) new(scope, page:, per_page:).call end |
Instance Method Details
#call ⇒ Object
43 44 45 46 47 48 |
# File 'lib/warped/queries/paginate.rb', line 43 def call offset = (page - 1) * per_page paginated_scope = scope.limit(per_page).offset(offset) [(scope), paginated_scope] end |