Module: PagingCursor::ActiveRecord::FinderMethods

Defined in:
lib/paging_cursor/active_record.rb

Overview

TODO: option to set which column is used for pagination

default = :id

Instance Method Summary collapse

Instance Method Details

#after(cursor = nil) ⇒ Object



28
29
30
31
32
33
# File 'lib/paging_cursor/active_record.rb', line 28

def after(cursor=nil)
  result = where(arel_table[primary_key].gt(cursor || 0)).reorder(arel_table[primary_key].asc)
  result.sort_order = :asc
  result.cursored = true
  result
end

#before(cursor = nil) ⇒ Object



21
22
23
24
25
26
# File 'lib/paging_cursor/active_record.rb', line 21

def before(cursor=nil)
  result = where(cursor ? arel_table[primary_key].lt(cursor) : nil).reorder(arel_table[primary_key].desc)
  result.sort_order = :desc
  result.cursored = true
  result
end

#cursor(options = {}) ⇒ Object

default order = after



11
12
13
14
15
16
17
18
19
# File 'lib/paging_cursor/active_record.rb', line 11

def cursor(options={})
  options = HashWithIndifferentAccess.new(options)
  if options.has_key?(:before) || (!options.has_key?(:after) && PagingCursor.config.default_sort_order == :desc)
    result = before(options[:before])
  else
    result = after(options[:after])
  end
  result.limit(options[:limit] || self.cursor_page_limit)
end