Class: RailsCursorPagination::Paginator

Inherits:
Object
  • Object
show all
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

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

Raises:

  • If any parameter is not valid

Parameters:

  • Relation that will be paginated.

  • (defaults to: nil)

    Number of records to return in pagination. Can be combined with either after or before as an alternative to first or last.

  • (defaults to: nil)

    Number of records to return in a forward pagination. Can be combined with after.

  • (defaults to: nil)

    Cursor to paginate forward from. Can be combined with first.

  • (defaults to: nil)

    Number of records to return. Must be used together with before.

  • (defaults to: nil)

    Cursor to paginate upto (excluding). Can be combined with last.

  • (defaults to: nil)

    Column to order by. If none is provided, will default to ID column. NOTE: this will cause the query to filter on both the given column as well as the ID column. So you might want to add a compound index to your database similar to:

      CREATE INDEX <index_name> ON <table_name> (<order_by_field>, id)
    
  • (defaults to: nil)

    Ordering to apply, either :asc or :desc. Defaults to :asc.



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.

Parameters:

  • (defaults to: false)

Returns:

  • with keys :page, :page_info, and optional :total



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