Sha256: 01782879366ce09614ac9b279bfba7dc52de600a7fe1eedcb1382ad56e4adf37

Contents?: true

Size: 1.25 KB

Versions: 19

Compression:

Stored size: 1.25 KB

Contents

module MongoMapper
  module Pagination
    class PaginationProxy < BasicObject
      attr_accessor :subject
      attr_reader :total_entries, :per_page, :current_page
      alias limit per_page
      
      def initialize(total_entries, current_page, per_page=nil)
        @total_entries    = total_entries.to_i
        self.per_page     = per_page
        self.current_page = current_page
      end
      
      def total_pages
        (total_entries / per_page.to_f).ceil
      end
      
      def out_of_bounds?
        current_page > total_pages
      end
      
      def previous_page
        current_page > 1 ? (current_page - 1) : nil
      end
      
      def next_page
        current_page < total_pages ? (current_page + 1) : nil
      end
      
      def skip
        (current_page - 1) * per_page
      end
      alias offset skip # for will paginate support
      
      
      def method_missing(name, *args, &block)
        @subject.send(name, *args, &block)
      end
      
      private
        def per_page=(value)
          value = 25 if value.blank?
          @per_page = value.to_i
        end
        
        def current_page=(value)
          value = value.to_i
          value = 1 if value < 1
          @current_page = value
        end
    end
  end
end

Version data entries

19 entries across 19 versions & 5 rubygems

Version Path
mongo_mapper-0.6.6 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.12.4 lib/mongo_mapper/pagination.rb
danielharan-mongo_mapper-0.6.5 lib/mongo_mapper/pagination.rb
mongo_mapper-0.6.5 lib/mongo_mapper/pagination.rb
webbynode-mongo_mapper-0.6.4 lib/mongo_mapper/pagination.rb
mongo_mapper-0.6.4 lib/mongo_mapper/pagination.rb
mongo_mapper-0.6.3 lib/mongo_mapper/pagination.rb
mongo_mapper-0.6.2 lib/mongo_mapper/pagination.rb
mongo_mapper-0.6.1 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.11.18 lib/mongo_mapper/pagination.rb
mongo_mapper-0.6.0 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.11.8 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.11.6 lib/mongo_mapper/pagination.rb
djsun-mongo_mapper-0.5.8.2 lib/mongo_mapper/pagination.rb
djsun-mongo_mapper-0.5.8.1 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.11.2 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.10.31 lib/mongo_mapper/pagination.rb
mongo_mapper-0.5.8 lib/mongo_mapper/pagination.rb
mongo_mapper-0.5.7 lib/mongo_mapper/pagination.rb