Sha256: cba85a18b0fbd854a0b7c5f4501f10680d79a85191d5eb2643f50d089b13323f

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

module MongoMapper
  module Pagination
    class PaginationProxy
      instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|proxy_|^object_id$)/ }
      
      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

9 entries across 9 versions & 3 rubygems

Version Path
tpitale-mongo_mapper-0.6.9 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.12.30 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.12.29 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.12.23 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.12.22 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.12.16 lib/mongo_mapper/pagination.rb
mongo_mapper-0.6.8 lib/mongo_mapper/pagination.rb
mongo_mapper-unstable-2009.12.14 lib/mongo_mapper/pagination.rb
mongo_mapper-0.6.7 lib/mongo_mapper/pagination.rb