Module: CursorPaginationR4::ActiveRecord::RelationMethods
- Defined in:
- lib/cursor-paginate-r4/active_record.rb
Instance Attribute Summary collapse
-
#cursor_key ⇒ Object
Returns the value of attribute cursor_key.
Instance Method Summary collapse
- #after(options) ⇒ Object
- #before(options) ⇒ Object
- #has_next? ⇒ Boolean
-
#load ⇒ Object
Override ActiveRecord::Relation#load.
- #next_cursor ⇒ Object
Instance Attribute Details
#cursor_key ⇒ Object
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() = HashWithIndifferentAccess.new() rel = if ::ActiveRecord::Relation === self self else all end rel.cursor_key = rel.model.columns.map(&:name).find { |column| .key? column } raise CursorPaginationR4::InvalidColumnGiven unless rel.cursor_key cursor = [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() = HashWithIndifferentAccess.new() rel = if ::ActiveRecord::Relation === self self else all end rel.cursor_key = rel.model.columns.map(&:name).find { |column| .key? column } raise CursorPaginationR4::InvalidColumnGiven unless rel.cursor_key cursor = [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
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 |
#load ⇒ Object
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_cursor ⇒ Object
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 |