Module: Mongoid::Contexts::Paging

Included in:
Enumerable, Mongo
Defined in:
lib/mongoid/contexts/paging.rb

Instance Method Summary collapse

Instance Method Details

#pageObject

Either returns the page option and removes it from the options, or returns a default value of 1.

Returns:

An Integer page number.



35
36
37
38
# File 'lib/mongoid/contexts/paging.rb', line 35

def page
  skips, limits = options[:skip], options[:limit]
  (skips && limits) ? (skips + limits) / limits : 1
end

#paginate(pager_options = {}) ⇒ Object

Paginates the documents.

Example:

context.paginate(:page => 6, :per_page => 25)

Returns:

A collection of documents paginated. All previous limit and skip call will be ignored.



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

def paginate(pager_options={})
  if pager_options[:per_page]
    options[:limit] = pager_options[:per_page].to_i
    if pager_options[:page]
      options[:skip]  = (pager_options[:page].to_i - 1) * pager_options[:per_page].to_i
    end
  end

  @collection ||= execute(true)
  WillPaginate::Collection.create(page, per_page, count) do |pager|
    pager.replace(@collection.to_a)
  end
end

#per_pageObject

Get the number of results per page or the default of 20.

Returns:

The Integer number of documents in each page.



45
46
47
# File 'lib/mongoid/contexts/paging.rb', line 45

def per_page
  (options[:limit] || 20).to_i
end