class Might::Paginator

Paginates ActiveRecord scopes @example

Paginator.new(limit: 10, offset: 100).paginate(Movie.all)

As a side effect of pagination it defines the following methods on collection:

collection#limit
collection#offset
collection#count
collection#total_count

Constants

InvalidLimitOrOffset

Attributes

limit[R]
offset[R]

Public Class Methods

new(options = {}) click to toggle source

@param [{Symbol => Integer}] params @option params [Integer] :limit @option params [Integer] :offset

# File lib/might/paginator.rb, line 23
def initialize(options = {})
  @limit = Integer(options.fetch(:limit))
  @offset = Integer(options.fetch(:offset))

  fail InvalidLimitOrOffset if @limit < 0 || @offset < 0
end

Public Instance Methods

paginate(collection) click to toggle source

Paginate given collection @param [ActiveRecord::CollectionProxy, ActiveRecord::Base] collection @return [ActiveRecord::CollectionProxy]

# File lib/might/paginator.rb, line 34
def paginate(collection)
  collection.offset(offset).limit(limit)
end