Sha256: adb095d6613e7408af8ab601aa5eacfd270e88910fc2ee43d5f618f56c4d4cb5

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

module MongoMapper
  module Pagination
    class PaginationProxy < BlankSlate
      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
      
      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

7 entries across 7 versions & 2 rubygems

Version Path
fcoury-mongomapper-0.3.0 lib/mongomapper/pagination.rb
fcoury-mongomapper-0.3.1 lib/mongomapper/pagination.rb
fcoury-mongomapper-0.3.3 lib/mongomapper/pagination.rb
fcoury-mongomapper-0.3.4 lib/mongomapper/pagination.rb
fcoury-mongomapper-0.3.5 lib/mongomapper/pagination.rb
fcoury-mongomapper-0.4.0 lib/mongomapper/pagination.rb
ramsingla-mongomapper-0.2.1 lib/mongomapper/pagination.rb