Sha256: 213a3bd3018ee30d840ccfe3b6f3889ec940fde062054007cff40256b471a959

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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)) + (current_page == 1 ? padding : 0)
                limit_value = length - (current_page == 1 ? padding : 0)
                values = Module.new do
                  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

1 entries across 1 versions & 1 rubygems

Version Path
pagers-3.0.0 lib/pagers/active_record/base.rb