Class: JsonapiCompliable::Scoping::Paginate
- Defined in:
- lib/jsonapi_compliable/scoping/paginate.rb
Overview
Apply pagination logic to the scope
If the user requests a page size greater than MAX_PAGE_SIZE
, a
JsonapiCompliable::Errors::UnsupportedPageSize
error will be
raised.
Notably, this will not fire when the `default: false` option is passed. This is the case for sideloads - if the user requests “give me the post and its comments”, we shouldn't implicitly limit those comments to 20. BUT if the user requests, “give me the post and 3 of its comments”, we should honor that pagination.
This can be confusing because there are also 'default' and 'customized' pagination procs. The default comes 'for free'. Customized pagination looks like
class PostResource < ApplicationResource
paginate do |scope, current_page, per_page|
# ... the custom logic ...
end
end
We should use the default unless the user has customized.
Constant Summary
- MAX_PAGE_SIZE =
1_000
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#apply ⇒ Object
Apply the pagination logic.
-
#apply? ⇒ Boolean
We want to apply this logic unless we've explicitly received the default: false option.
-
#apply_custom_scope ⇒ Object
Apply the custom pagination proc.
-
#apply_standard_scope ⇒ Object
Apply default pagination proc via the Resource adapter.
-
#custom_scope ⇒ Proc, Nil
The custom pagination proc.
Methods inherited from Base
Constructor Details
This class inherits a constructor from JsonapiCompliable::Scoping::Base
Instance Method Details
#apply ⇒ Object
Apply the pagination logic. Raise error if over the max page size.
30 31 32 33 34 35 36 37 |
# File 'lib/jsonapi_compliable/scoping/paginate.rb', line 30 def apply if size > MAX_PAGE_SIZE raise JsonapiCompliable::Errors::UnsupportedPageSize .new(size, MAX_PAGE_SIZE) else super end end |
#apply? ⇒ Boolean
We want to apply this logic unless we've explicitly received the default: false option. In that case, only apply if pagination was explicitly specified in the request.
44 45 46 47 48 49 50 |
# File 'lib/jsonapi_compliable/scoping/paginate.rb', line 44 def apply? if @opts[:default] == false not [page_param[:size], page_param[:number]].all?(&:nil?) else true end end |
#apply_custom_scope ⇒ Object
Apply the custom pagination proc
63 64 65 |
# File 'lib/jsonapi_compliable/scoping/paginate.rb', line 63 def apply_custom_scope custom_scope.call(@scope, number, size, resource.context) end |
#apply_standard_scope ⇒ Object
Apply default pagination proc via the Resource adapter
58 59 60 |
# File 'lib/jsonapi_compliable/scoping/paginate.rb', line 58 def apply_standard_scope resource.adapter.paginate(@scope, number, size) end |
#custom_scope ⇒ Proc, Nil
Returns the custom pagination proc
53 54 55 |
# File 'lib/jsonapi_compliable/scoping/paginate.rb', line 53 def custom_scope resource.pagination end |