Module: Mongoid::Contexts::Paging
- Included in:
- MongoDoc::Contexts::Mongo, Enumerable
- Defined in:
- lib/mongoid/contexts/paging.rb
Instance Method Summary collapse
-
#page ⇒ Object
Either returns the page option and removes it from the options, or returns a default value of 1.
-
#paginate ⇒ Object
Paginates the documents.
-
#per_page ⇒ Object
Get the number of results per page or the default of 20.
Instance Method Details
#page ⇒ Object
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 = [:skip], [:limit] (skips && limits) ? (skips + limits) / limits : 1 end |
#paginate ⇒ Object
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_page ⇒ Object
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 ([:limit] || 20).to_i end |