Sha256: 93af4afd27679547594e119695e0856e53be3e698a51138712360a8f01a359a0

Contents?: true

Size: 1.62 KB

Versions: 15

Compression:

Stored size: 1.62 KB

Contents

module MongoMapper
  module Plugins
    module Pagination
      class Proxy
        instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|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 send(method, *args, &block)
          if respond_to?(method)
            super
          else
            subject.send(method, *args, &block)
          end
        end

        def ===(other)
          other === subject
        end

        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
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
mongo_mapper-unstable-2010.2.27 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.26 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.25 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.24 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.23 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.22 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.19 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.18 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.17 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.16 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.15 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.12 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.11 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-unstable-2010.2.10 lib/mongo_mapper/plugins/pagination/proxy.rb
mongo_mapper-0.7.0 lib/mongo_mapper/plugins/pagination/proxy.rb