class GearedPagination::PortionAtCursor

Attributes

cursor[R]
orders[R]
ratios[R]

Public Class Methods

new(cursor: Cursor.new, ordered_by:, per_page: Ratios.new) click to toggle source
# File lib/geared_pagination/portions/portion_at_cursor.rb, line 10
def initialize(cursor: Cursor.new, ordered_by:, per_page: Ratios.new)
  @cursor, @orders, @ratios = cursor, ordered_by, per_page
end

Public Instance Methods

cache_key() click to toggle source
# File lib/geared_pagination/portions/portion_at_cursor.rb, line 27
def cache_key
  "#{page_number}:#{ratios.cache_key}"
end
from(scope) click to toggle source
# File lib/geared_pagination/portions/portion_at_cursor.rb, line 14
def from(scope)
  if scope.order_values.none? && scope.limit_value.nil?
    selection_from(scope).order(orderings).limit(limit)
  else
    raise ArgumentError, "Can't paginate relation with ORDER BY or LIMIT clauses (got #{scope.to_sql})"
  end
end
next_param(scope) click to toggle source
# File lib/geared_pagination/portions/portion_at_cursor.rb, line 22
def next_param(scope)
  Cursor.encode page_number: page_number + 1, values: from(scope).last&.slice(*attributes) || {}
end

Private Instance Methods

attributes() click to toggle source
# File lib/geared_pagination/portions/portion_at_cursor.rb, line 44
def attributes
  orders.collect(&:attribute)
end
limit() click to toggle source
# File lib/geared_pagination/portions/portion_at_cursor.rb, line 40
def limit
  ratios[page_number]
end
orderings() click to toggle source
# File lib/geared_pagination/portions/portion_at_cursor.rb, line 36
def orderings
  orders.map { |order| [ order.attribute, order.direction ] }.to_h
end
selection_from(scope) click to toggle source
# File lib/geared_pagination/portions/portion_at_cursor.rb, line 32
def selection_from(scope)
  Selection.new(scope, orders).from(cursor)
end