Sha256: 201a76f3bf46ca1423197a5549160a8449c875fa6eb6294d97924187c6ca4212

Contents?: true

Size: 1.12 KB

Versions: 5

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] || Pagers.config[:length])
                padding = (options[:padding] || Pagers.config[: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

5 entries across 5 versions & 1 rubygems

Version Path
pagers-3.1.5 lib/pagers/active_record/base.rb
pagers-3.1.4 lib/pagers/active_record/base.rb
pagers-3.1.3 lib/pagers/active_record/base.rb
pagers-3.1.2 lib/pagers/active_record/base.rb
pagers-3.1.1 lib/pagers/active_record/base.rb