Module: CursorPagination::PageScopeMethods

Defined in:
lib/cursor_pagination/page_scope_methods.rb

Instance Method Summary collapse

Instance Method Details

#first_page?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/cursor_pagination/page_scope_methods.rb', line 7

def first_page?
  previous_cursor == -1
end

#last_page?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/cursor_pagination/page_scope_methods.rb', line 11

def last_page?
  next_cursor == -1
end

#next_cursorObject



29
30
31
32
33
34
# File 'lib/cursor_pagination/page_scope_methods.rb', line 29

def next_cursor
  return -1 if last.nil?
  # try to get something after last cursor
  cursor = last.send(cursor_options[:column])
  _origin_scope.cursor(cursor, cursor_options).per(1).count.zero? ? -1 : cursor
end

#per(num) ⇒ Object



3
4
5
# File 'lib/cursor_pagination/page_scope_methods.rb', line 3

def per(num)
  limit(num)
end

#previous_cursorObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cursor_pagination/page_scope_methods.rb', line 15

def previous_cursor
  options = cursor_options
  result = _origin_scope.where("#{options[:column]} #{options[:reverse] ? '>' : '<'}= ?", current_cursor).limit(limit_value+1).to_a
  case result.size
  when limit_value+1
    result.first.send(options[:column])
  when 0
    -1 #no previous page
  else
    nil #first page, incomplete
  end

end