Module: Mongoid::Contexts::Paging

Included in:
MongoDoc::Contexts::Mongo, Enumerable
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.



27
28
29
30
# File 'lib/mongoid/contexts/paging.rb', line 27

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

#paginateObject

Paginates the documents.

Example:

context.paginate

Returns:

A collection of documents paginated.



14
15
16
17
18
19
# File 'lib/mongoid/contexts/paging.rb', line 14

def paginate
  @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.



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

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