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() click to toggle source

Apply the pagination logic. Raise error if over the max page size. @return the scope object we are chaining/modifying

Calls superclass method 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
apply?() click to toggle source

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_custom_scope() click to toggle source

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_standard_scope() click to toggle source

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
custom_scope() click to toggle source

@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

number() click to toggle source
# File lib/jsonapi_compliable/scoping/paginate.rb, line 73
def number
  (page_param[:number] || resource.default_page_number).to_i
end
page_param() click to toggle source
# File lib/jsonapi_compliable/scoping/paginate.rb, line 69
def page_param
  @page_param ||= (query_hash[:page] || {})
end
size() click to toggle source
# File lib/jsonapi_compliable/scoping/paginate.rb, line 77
def size
  (page_param[:size] || resource.default_page_size).to_i
end