Sha256: 6c006e1d6778a5074f5cea00ec8f1b54d5d71d6c7ab309d9fc8acd84c6d26fcb

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

module Pagers
  module Extensions
    module ActiveRecord
      module Base
        extend ActiveSupport::Concern

        module ClassMethods

          def inherited(subclass)
            subclass.class_eval do
              if superclass == ::ActiveRecord::Base
                scope :page, ->(number, options={}) {
                  padding = (options[:padding] || Pagers.configuration.padding)
                  length = (options[:length] || Pagers.configuration.length)
                  current_page = [number.to_i, 1].max
                  limit = length
                  offset = ((length * (current_page - 1)) + padding)
                  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).offset(offset).extending(
                    Pagers::Extensions::ActiveRecord::Relation,
                    values
                  )
                }
              end
            end
            super
          end
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pagers-5.1.0 lib/pagers/extensions/active_record/base.rb
pagers-4.0.0.1 lib/pagers/extensions/active_record/base.rb
pagers-4.0.0.0 lib/pagers/extensions/active_record/base.rb