module PagingCursor::ActiveRecord::FinderMethods

TODO: option to set which column is used for pagination

default = :id

Public Instance Methods

after(cursor=nil) click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source

default order = after

# 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