Class: Pageturner

Inherits:
Object
  • Object
show all
Defined in:
lib/pageturner.rb

Defined Under Namespace

Classes: Exception

Constant Summary collapse

ASC =
"asc"
COMPARATOR_INTERNAL_BUG =
"sort_direction input has an unexpected value. Contact Pageturner mantainer for support."
DESC =
"desc"
GREATER_THAN_OPERATOR =
">"
LESS_THAN_OPERATOR =
"<"
LOOKAHEAD =
1

Instance Method Summary collapse

Constructor Details

#initialize(anchor_column:, anchor_value:, ar_relation:, sort_direction:, page_size:, anchor_id:, anchor_column_active_record_identifier: nil, anchor_column_sql_identifier: nil) ⇒ Pageturner

Returns a new instance of Pageturner.

Parameters:

  • anchor_column (String)
    • Field to paginate on.

  • anchor_value (Anything, nil)
    • Value of the anchor_column for the record to paginate from.

  • ar_relation (ActiveRecord::Relation)
    • Resource collection to paginate.

  • sort_direction (String)
    • Order of the pagination. Valid values: Pageturner::ASC, Pageturner::DESC.

  • anchor_id (Number)
    • ID of the record to paginate from.

  • anchor_column_active_record_identifier (String, nil) (defaults to: nil)
    • Method chain that references the anchor column in the model you are paginating.

  • anchor_column_sql_identifier (String, nil) (defaults to: nil)
    • SQL identifier that references the anchor column in your database schema.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pageturner.rb', line 16

def initialize(anchor_column:, anchor_value:, ar_relation:, sort_direction:, page_size:, anchor_id:, anchor_column_active_record_identifier: nil, anchor_column_sql_identifier: nil)
  @anchor_column = anchor_column

  @anchor_column_active_record_identifier = anchor_column_active_record_identifier || @anchor_column
  @anchor_column_sql_identifier = anchor_column_sql_identifier || @anchor_column

  @anchor_value = anchor_value

  @ar_relation = ar_relation

  # Always select id to use it as secondary sort to prevent non-deterministic ordering when primary sort is on a non-unique column.
  unless primary_key_selected?(@ar_relation)
    raise Exception::PrimaryKeyNotSelected, "The provided ActiveRecord::Relation does not have the primary key selected. Cursor pagination requires a primary key to paginate undeterministic columns."
  end

  @sort_direction = sort_direction
  @page_size = page_size
  @anchor_id = anchor_id

  unless [Pageturner::ASC, Pageturner::DESC].include?(@sort_direction)
    raise Exception::InvalidSortDirection, "The provided order is not supported. Supported order values are: '#{ASC}', '#{DESC}'"
  end
end

Instance Method Details

#has_next_page?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/pageturner.rb', line 40

def has_next_page?
  # Load the current page.
  self.resources

  @has_next_page
end

#next_cursorObject



54
55
56
57
58
59
60
61
62
# File 'lib/pageturner.rb', line 54

def next_cursor
  {
    anchor_column: @anchor_column,
    anchor_id: self.resources.last&.id,
    anchor_value: try_chain(self.resources.last, @anchor_column_active_record_identifier),
    page_size: @page_size,
    sort_direction: @sort_direction
  }
end

#resourcesObject



47
48
49
50
51
52
# File 'lib/pageturner.rb', line 47

def resources
  # Memoize expensive querying.
  return @result if defined?(@result)

  @result = calculate_resources
end