Sha256: e4b44b9abb2a3bfc6fc64e5231f643f3165cdd2c0ed19b08d706a3b27cd9b3ef

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module Pagers
  module ActiveRecord
    module Base
      extend ActiveSupport::Concern

      module ClassMethods

        def inherited(subclass)
          subclass.class_eval do
            if superclass == ::ActiveRecord::Base
              scope :page, ->(number, options={}) {
                length = options[:length] || Rails.application.config.pagers[:length]
                padding = options[:padding] || Rails.application.config.pagers[:padding]
                current_page = [number.to_i, 1].max
                offset_value = (length * (current_page - 1)) + padding
                limit_value = length
                values = Module.new do
                  define_method :padding do
                    padding
                  end
                  define_method :page_length do
                    length
                  end
                  define_method :current_page do
                    current_page
                  end
                end
                limit(limit_value).offset(offset_value).extending(Pagers::ActiveRecord::Relation, values)
              }
            end
          end
          super
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pagers-3.0.2 lib/pagers/active_record/base.rb
pagers-3.0.1 lib/pagers/active_record/base.rb