Class: GraphQL::Connections::Keyset::Desc

Inherits:
Base
  • Object
show all
Defined in:
lib/graphql/connections/keyset/desc.rb

Overview

Implements keyset pagination by two fields with desc order

Constant Summary

Constants inherited from Base

Base::SEPARATOR

Instance Attribute Summary

Attributes inherited from Base

#field_key

Attributes inherited from Base

#opaque_cursor

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods inherited from Base

#initialize, #nodes, #primary_key

Constructor Details

This class inherits a constructor from GraphQL::Connections::Keyset::Base

Instance Method Details

#cursor_for(item) ⇒ Object

rubocop:enable Naming/PredicateName, Metrics/AbcSize, Metrics/MethodLength



47
48
49
50
51
# File 'lib/graphql/connections/keyset/desc.rb', line 47

def cursor_for(item)
  cursor = [item[field_key], item[primary_key]].map { |value| serialize(value) }.join(@separator)
  cursor = encode(cursor) if opaque_cursor
  cursor
end

#has_next_pageObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graphql/connections/keyset/desc.rb', line 27

def has_next_page
  if first
    nodes.any? &&
      items.where(arel_table[field_key].eq(nodes.last[field_key]))
        .where(arel_table[primary_key].lt(nodes.last[primary_key]))
        .or(items.where(arel_table[field_key].lt(nodes.last[field_key])))
        .exists?
  elsif before
    items
      .where(arel_table[field_key].lt(before_cursor_date))
      .or(
        items.where(arel_table[field_key].eq(before_cursor_date))
          .where(arel_table[primary_key].lt(before_cursor_primary_key))
      ).exists?
  else
    false
  end
end

#has_previous_pageObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/graphql/connections/keyset/desc.rb', line 8

def has_previous_page
  if last
    nodes.any? &&
      items.where(arel_table[field_key].eq(nodes.first[field_key]))
        .where(arel_table[primary_key].gt(nodes.first[primary_key]))
        .or(items.where(arel_table[field_key].gt(nodes.first[field_key])))
        .exists?
  elsif after
    items
      .where(arel_table[field_key].gt(after_cursor_date))
      .or(
        items.where(arel_table[field_key].eq(after_cursor_date))
          .where(arel_table[primary_key].gt(after_cursor_primary_key))
      ).exists?
  else
    false
  end
end