Module: Jinrai::ActiveRecord::FinderMethods::ClassMethods

Defined in:
lib/jinrai/active_record/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#after(cursor, **options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/jinrai/active_record/finder_methods.rb', line 24

def after(cursor, **options)
  sort_order = options[:sort_order] || default_cursor_sort_order
  relation =
    if sort_order == :desc
      cursoring(:lt, :gt, cursor, options[:sort_at])
    elsif sort_order == :asc
      cursoring(:gt, :lt, cursor, options[:sort_at])
    end
  relation.extending_cursor
end

#before(cursor, **options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/jinrai/active_record/finder_methods.rb', line 35

def before(cursor, **options)
  sort_order = options[:sort_order] || default_cursor_sort_order
  relation =
    if sort_order == :desc
      cursoring(:gt, :lt, cursor, options[:sort_at])
    elsif sort_order == :asc
      cursoring(:lt, :gt, cursor, options[:sort_at])
    end
  relation.extending_cursor
end

#cursor(**options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/jinrai/active_record/finder_methods.rb', line 13

def cursor(**options)
  sort_order = options[:sort_order] || default_cursor_sort_order
  relation =
    if sort_order == :desc
      cursoring(:lt, :gt, options[:since], options[:sort_at]).cursoring(:gt, :lt, options[:till], options[:sort_at])
    elsif sort_order == :asc
      cursoring(:gt, :lt, options[:since], options[:sort_at]).cursoring(:lt, :gt, options[:till], options[:sort_at])
    end
  relation.extending_cursor
end

#cursoring(rank, rank_for_primary, cursor, sort_at) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jinrai/active_record/finder_methods.rb', line 51

def cursoring(rank, rank_for_primary, cursor, sort_at)
  sort_at ||= primary_key
  if cursor
    attributes = default_attributes_from_cursor.call(decode_cursor(cursor))
    pointed = find_by!(attributes)

    if sort_at != primary_key
      condition_1 = arel_table[sort_at].send(rank, pointed[sort_at])
      condition_2 = arel_table.grouping(arel_table[sort_at].eq(pointed[sort_at]).and(arel_table[primary_key].send(rank_for_primary, pointed[primary_key])))
      relation = where(condition_1.or(condition_2))
    else
      relation = where(arel_table[primary_key].send(rank, pointed[primary_key]))
    end
  else
    relation = all
  end
  relation.order(sort_at => default_cursor_sort_order)
rescue ::ActiveRecord::StatementInvalid => e
  # TODO: modify error message
  raise Jinrai::ActiveRecord::StatementInvalid, e
rescue ::ActiveRecord::RecordNotFound
  raise Jinrai::ActiveRecord::RecordNotFound,
    "Could not find record cursor pointing, Check cursor_format settings."
end

#extending_cursorObject



46
47
48
# File 'lib/jinrai/active_record/finder_methods.rb', line 46

def extending_cursor
  extending { include Jinrai::ActiveRecord::CursorMethods }.per
end