Module: Mongoid::Pagination::ClassMethods

Defined in:
lib/mongoid/pagination.rb

Instance Method Summary collapse

Instance Method Details

#paginate(opts = {}) ⇒ Mongoid::Criteria

Paginate the results

Parameters:

  • opts (Hash) (defaults to: {})
  • [Integer] (Hash)

    a customizable set of options

Returns:

  • (Mongoid::Criteria)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongoid/pagination.rb', line 16

def paginate(opts = {})
  return criteria if opts[:limit].blank? and opts[:page].blank? and opts[:offset].blank?

  limit = (opts[:limit] || 25).to_i
  page  = (opts[:page]  || 1).to_i

  if opts[:page].blank?
    offset = (opts[:offset] || 0)
  else
    if page > 1
      offset = (page - 1) * limit
    else
      offset = 0
    end
  end

  per_page(limit).offset(offset)
end

#per_page(page_limit = 25) ⇒ Mongoid::Criteria

Limit the result set

Parameters:

  • page_limit (Integer) (defaults to: 25)

    the max number of results to return

Returns:

  • (Mongoid::Criteria)


39
40
41
# File 'lib/mongoid/pagination.rb', line 39

def per_page(page_limit = 25)
  limit(page_limit.to_i)
end