Sha256: 33ac55bc6472eedf9e3322b2fb55c2df7b832918e3823996263a7f0d4c35cc07

Contents?: true

Size: 1022 Bytes

Versions: 8

Compression:

Stored size: 1022 Bytes

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Contexts #:nodoc:
    module Paging
      # Paginates the documents.
      #
      # Example:
      #
      # <tt>context.paginate</tt>
      #
      # Returns:
      #
      # A collection of documents paginated.
      def paginate
        @collection ||= execute(true)
        WillPaginate::Collection.create(page, per_page, count) do |pager|
          pager.replace(@collection.to_a)
        end
      end

      # Either returns the page option and removes it from the options, or
      # returns a default value of 1.
      #
      # Returns:
      #
      # An +Integer+ page number.
      def page
        skips, limits = @options[:skip], @options[:limit]
        (skips && limits) ? (skips + limits) / limits : 1
      end

      # Get the number of results per page or the default of 20.
      #
      # Returns:
      #
      # The +Integer+ number of documents in each page.
      def per_page
        (@options[:limit] || 20).to_i
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
mongoid-pre-2.0.0.pre lib/mongoid/contexts/paging.rb
mongoid-1.2.6 lib/mongoid/contexts/paging.rb
mongoid-1.2.5 lib/mongoid/contexts/paging.rb
mongoid-1.2.4 lib/mongoid/contexts/paging.rb
mongoid-1.2.3 lib/mongoid/contexts/paging.rb
mongoid-1.2.2 lib/mongoid/contexts/paging.rb
mongoid-1.2.1 lib/mongoid/contexts/paging.rb
mongoid-1.2.0 lib/mongoid/contexts/paging.rb