Sha256: c2b230e0f9ee7b73efd0289930a6830ab7ddf87369015553d180133655ea078b

Contents?: true

Size: 1.08 KB

Versions: 9

Compression:

Stored size: 1.08 KB

Contents

require 'mongoid'
require 'will_paginate/collection'

module WillPaginate
  module Mongoid
    module CriteriaMethods
      def paginate(options = {})
        extend CollectionMethods
        @current_page = WillPaginate::PageNumber(options[:page] || @current_page || 1)
        @page_multiplier = current_page - 1
        pp = (options[:per_page] || per_page || WillPaginate.per_page).to_i
        limit(pp).skip(@page_multiplier * pp)
      end

      def per_page(value = :non_given)
        if value == :non_given
          options[:limit] == 0 ? nil : options[:limit] # in new Mongoid versions a nil limit is saved as 0
        else
          limit(value)
        end
      end

      def page(page)
        paginate(:page => page)
      end
    end

    module CollectionMethods
      attr_reader :current_page

      def total_entries
        @total_entries ||= count
      end

      def total_pages
        (total_entries / per_page.to_f).ceil
      end

      def offset
        @page_multiplier * per_page
      end
    end

    ::Mongoid::Criteria.send(:include, CriteriaMethods)
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
will_paginate-3.1.8 lib/will_paginate/mongoid.rb
will_paginate-3.1.7 lib/will_paginate/mongoid.rb
will_paginate-3.1.6 lib/will_paginate/mongoid.rb
will_paginate-3.1.5 lib/will_paginate/mongoid.rb
will_paginate-3.1.3 lib/will_paginate/mongoid.rb
will_paginate-3.1.2 lib/will_paginate/mongoid.rb
will_paginate-3.1.1 lib/will_paginate/mongoid.rb
will_paginate-3.1.0 lib/will_paginate/mongoid.rb
will_paginate_seo-3.0.4 lib/will_paginate/mongoid.rb