Class: RailsCursorPagination::Paginator
- Inherits:
-
Object
- Object
- RailsCursorPagination::Paginator
- Defined in:
- lib/rails_cursor_pagination/paginator.rb
Overview
Use this Paginator class to effortlessly paginate through ActiveRecord relations using cursor pagination. For more details on how this works, read the top-level documentation of the ‘RailsCursorPagination` module.
Usage:
RailsCursorPagination::Paginator
.new(relation, order_by: :author, first: 2, after: "WyJKYW5lIiw0XQ==")
.fetch
Instance Method Summary collapse
-
#fetch(with_total: false) ⇒ Hash
Get the paginated result, including the actual ‘page` with its data items and cursors as well as some meta data in `page_info` and an optional `total` of records across all pages.
-
#initialize(relation, limit: nil, first: nil, after: nil, last: nil, before: nil, order_by: nil, order: nil) ⇒ Paginator
constructor
Create a new instance of the ‘RailsCursorPagination::Paginator`.
Constructor Details
#initialize(relation, limit: nil, first: nil, after: nil, last: nil, before: nil, order_by: nil, order: nil) ⇒ Paginator
Create a new instance of the ‘RailsCursorPagination::Paginator`
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rails_cursor_pagination/paginator.rb', line 43 def initialize(relation, limit: nil, first: nil, after: nil, last: nil, before: nil, order_by: nil, order: nil) order_by ||= :id order ||= :asc ensure_valid_params_values!(relation, order, limit, first, last) ensure_valid_params_combinations!(first, last, limit, before, after) @order_field = order_by @order_direction = order @relation = relation @cursor = before || after @is_forward_pagination = before.blank? @page_size = first || last || limit || RailsCursorPagination::Configuration.instance.default_page_size if Configuration.instance.max_page_size && Configuration.instance.max_page_size < @page_size @page_size = Configuration.instance.max_page_size end @memos = {} end |
Instance Method Details
#fetch(with_total: false) ⇒ Hash
Get the paginated result, including the actual ‘page` with its data items and cursors as well as some meta data in `page_info` and an optional `total` of records across all pages.
78 79 80 81 82 83 84 |
# File 'lib/rails_cursor_pagination/paginator.rb', line 78 def fetch(with_total: false) { **(with_total ? { total: total } : {}), page_info: page_info, page: page } end |