Module: JSONAPI::Utils::Support::Pagination
- Included in:
- Response::Support
- Defined in:
- lib/jsonapi/utils/support/pagination.rb
Constant Summary collapse
- RecordCountError =
Class.new(ArgumentError)
Instance Method Summary collapse
-
#apply_pagination(records, options = {}) ⇒ ActiveRecord::Relation, Array
Apply proper pagination to the records.
-
#include_page_count? ⇒ Boolean
Check whether pagination’s page count should be included on the “meta” key.
-
#include_pagination_links? ⇒ Boolean
Check whether pagination links should be included.
-
#pagination_params(records, options) ⇒ Hash
Mount pagination params for JSONAPI::ResourcesOperationResult.
-
#record_count_for(records, options) ⇒ Integer
Apply memoization to the record count result avoiding duplicate counts.
Instance Method Details
#apply_pagination(records, options = {}) ⇒ ActiveRecord::Relation, Array
Apply proper pagination to the records.
36 37 38 39 40 41 |
# File 'lib/jsonapi/utils/support/pagination.rb', line 36 def apply_pagination(records, = {}) if !apply_pagination?() then records elsif records.is_a?(Array) then records[paginate_with(:range)] else paginate_with(:paginator).apply(records, nil) end end |
#include_page_count? ⇒ Boolean
Check whether pagination’s page count should be included on the “meta” key.
21 22 23 |
# File 'lib/jsonapi/utils/support/pagination.rb', line 21 def include_page_count? JSONAPI.configuration. end |
#include_pagination_links? ⇒ Boolean
Check whether pagination links should be included.
11 12 13 14 |
# File 'lib/jsonapi/utils/support/pagination.rb', line 11 def include_pagination_links? JSONAPI.configuration.default_paginator != :none && JSONAPI.configuration.top_level_links_include_pagination end |
#pagination_params(records, options) ⇒ Hash
Mount pagination params for JSONAPI::ResourcesOperationResult. It can also be used anywhere else as a helper method.
57 58 59 60 |
# File 'lib/jsonapi/utils/support/pagination.rb', line 57 def pagination_params(records, ) return {} unless include_pagination_links? paginator.links_page_params(record_count: record_count_for(records, )) end |
#record_count_for(records, options) ⇒ Integer
Apply memoization to the record count result avoiding duplicate counts.
74 75 76 |
# File 'lib/jsonapi/utils/support/pagination.rb', line 74 def record_count_for(records, ) @record_count ||= count_records(records, ) end |