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.
-
#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.
18 19 20 21 |
# File 'lib/jsonapi/utils/support/pagination.rb', line 18 def apply_pagination(records, = {}) return records unless apply_pagination?() records.is_a?(Array) ? records[paginate_with(:range)] : paginate_with(:paginator).apply(records, nil) end |
#pagination_params(records, options) ⇒ Hash
Mount pagination params for JSONAPI::ResourcesOperationResult. It can also be used anywhere else as a helper method.
36 37 38 39 |
# File 'lib/jsonapi/utils/support/pagination.rb', line 36 def pagination_params(records, ) return {} unless JSONAPI.configuration.top_level_links_include_pagination 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.
53 54 55 |
# File 'lib/jsonapi/utils/support/pagination.rb', line 53 def record_count_for(records, ) @record_count ||= count_records(records, ) end |