Module: Mongoid::Contexts::Paging
- Included in:
- Enumerable, Mongo
- 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(pager_options = {}) ⇒ 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.
35 36 37 38 |
# File 'lib/mongoid/contexts/paging.rb', line 35 def page skips, limits = [:skip], [: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(={}) if [:per_page] [:limit] = [:per_page].to_i if [:page] [:skip] = ([:page].to_i - 1) * [: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_page ⇒ Object
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 ([:limit] || 20).to_i end |