Module: CursorPaginationR4::ActiveRecord::RelationMethods

Defined in:
lib/cursor-paginate-r4/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cursor_keyObject

Returns the value of attribute cursor_key.



7
8
9
# File 'lib/cursor-paginate-r4/active_record.rb', line 7

def cursor_key
  @cursor_key
end

Instance Method Details

#after(options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cursor-paginate-r4/active_record.rb', line 26

def after(options)
  options = HashWithIndifferentAccess.new(options)

  rel = if ::ActiveRecord::Relation === self
          self
        else
          all
        end

  rel.cursor_key = rel.model.columns.map(&:name).find { |column| options.key? column }
  raise CursorPaginationR4::InvalidColumnGiven unless rel.cursor_key

  cursor = options[rel.cursor_key]

  rel.where(cursor ? arel_table[rel.cursor_key].gteq(cursor) : nil).reorder(arel_table[rel.cursor_key].asc)
end

#before(options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cursor-paginate-r4/active_record.rb', line 9

def before(options)
  options = HashWithIndifferentAccess.new(options)

  rel = if ::ActiveRecord::Relation === self
          self
        else
          all
        end

  rel.cursor_key = rel.model.columns.map(&:name).find { |column| options.key? column }
  raise CursorPaginationR4::InvalidColumnGiven unless rel.cursor_key

  cursor = options[rel.cursor_key]

  rel.where(cursor ? arel_table[rel.cursor_key].lt(cursor) : nil).reorder(arel_table[rel.cursor_key].desc)
end

#has_next?Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/cursor-paginate-r4/active_record.rb', line 43

def has_next?
  set_next if @has_next.nil?
  @has_next
end

#loadObject

Override ActiveRecord::Relation#load



54
55
56
57
58
59
60
# File 'lib/cursor-paginate-r4/active_record.rb', line 54

def load
  return super unless cursor_key

  rel = super
  rel.set_next
  rel
end

#next_cursorObject



48
49
50
51
# File 'lib/cursor-paginate-r4/active_record.rb', line 48

def next_cursor
  set_next if @has_next.nil?
  @next_cursor
end