class JsonapiCompliable::Scoping::Paginate
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. @see Resource.paginate
Constants
- MAX_PAGE_SIZE
Public Instance Methods
Apply the pagination logic. Raise error if over the max page size. @return the scope object we are chaining/modifying
JsonapiCompliable::Scoping::Base#apply
# 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
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.
@return [Boolean] should we apply this logic?
# 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 the custom pagination proc
# File lib/jsonapi_compliable/scoping/paginate.rb, line 63 def apply_custom_scope custom_scope.call(@scope, number, size, resource.context) end
Apply default pagination proc via the Resource
adapter
# File lib/jsonapi_compliable/scoping/paginate.rb, line 58 def apply_standard_scope resource.adapter.paginate(@scope, number, size) end
@return [Proc, Nil] the custom pagination proc
# File lib/jsonapi_compliable/scoping/paginate.rb, line 53 def custom_scope resource.pagination end
Private Instance Methods
# File lib/jsonapi_compliable/scoping/paginate.rb, line 73 def number (page_param[:number] || resource.default_page_number).to_i end
# File lib/jsonapi_compliable/scoping/paginate.rb, line 69 def page_param @page_param ||= (query_hash[:page] || {}) end
# File lib/jsonapi_compliable/scoping/paginate.rb, line 77 def size (page_param[:size] || resource.default_page_size).to_i end