module CursorPagination::PageScopeMethods

Public Instance Methods

first_page?() click to toggle source
# File lib/cursor_pagination/page_scope_methods.rb, line 7
def first_page?
  previous_cursor.value == -1
end
last_page?() click to toggle source
# File lib/cursor_pagination/page_scope_methods.rb, line 11
def last_page?
  next_cursor.value == -1
end
next_cursor() click to toggle source
# File lib/cursor_pagination/page_scope_methods.rb, line 37
def next_cursor
  Cursor.new(if last.nil?
    -1
  else
    # try to get something after last cursor
    cursor = Cursor.from_entity last, cursor_options[:columns]
    _origin_scope.cursor(cursor, cursor_options).per(1).count.zero? ? -1 : cursor.value
  end)
end
per(num) click to toggle source
# File lib/cursor_pagination/page_scope_methods.rb, line 3
def per(num)
  limit(num)
end
previous_cursor() click to toggle source
# File lib/cursor_pagination/page_scope_methods.rb, line 15
def previous_cursor
  Cursor.new(if current_cursor.empty?
    -1
  else
    scope = _origin_scope.limit(limit_value+1).reverse_order
    columns = cursor_options[:columns]

    cursor_value = [*current_cursor.value]
    scope = scope.where _cursor_to_where(columns, cursor_value, true)
    result = scope.to_a

    case result.size
    when limit_value+1
      Cursor.value_from_entity result.last, columns
    when 0
      -1 #no previous page
    else
      nil #first page, incomplete
    end
  end)
end