Sha256: 4024e4e45d2ef3ff58f37d0252e374bfb3ebe0a0a989fc22556134f6a0f2583f

Contents?: true

Size: 1.4 KB

Versions: 41

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module Validations
    class NumericalityValidator < ActiveModel::Validations::NumericalityValidator # :nodoc:
      def validate_each(record, attribute, value, precision: nil, scale: nil)
        precision = [column_precision_for(record, attribute) || Float::DIG, Float::DIG].min
        scale     = column_scale_for(record, attribute)
        super(record, attribute, value, precision: precision, scale: scale)
      end

      private
        def column_precision_for(record, attribute)
          record.class.type_for_attribute(attribute.to_s)&.precision
        end

        def column_scale_for(record, attribute)
          record.class.type_for_attribute(attribute.to_s)&.scale
        end
    end

    module ClassMethods
      # Validates whether the value of the specified attribute is numeric by
      # trying to convert it to a float with +Kernel.Float+ (if
      # <tt>only_integer</tt> is +false+) or applying it to the regular
      # expression <tt>/\A[\+\-]?\d+\z/</tt> (if <tt>only_integer</tt> is set to
      # +true+). +Kernel.Float+ precision defaults to the column's precision
      # value or 15.
      #
      # See ActiveModel::Validations::HelperMethods.validates_numericality_of for more information.
      def validates_numericality_of(*attr_names)
        validates_with NumericalityValidator, _merge_attributes(attr_names)
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/validations/numericality.rb
activerecord-8.0.1 lib/active_record/validations/numericality.rb
activerecord-8.0.0.1 lib/active_record/validations/numericality.rb
activerecord-7.2.2.1 lib/active_record/validations/numericality.rb
activerecord-7.1.5.1 lib/active_record/validations/numericality.rb
activerecord-8.0.0 lib/active_record/validations/numericality.rb
activerecord-7.2.2 lib/active_record/validations/numericality.rb
activerecord-7.1.5 lib/active_record/validations/numericality.rb
activerecord-8.0.0.rc2 lib/active_record/validations/numericality.rb
activerecord-7.2.1.2 lib/active_record/validations/numericality.rb
activerecord-7.1.4.2 lib/active_record/validations/numericality.rb
activerecord-8.0.0.rc1 lib/active_record/validations/numericality.rb
activerecord-7.2.1.1 lib/active_record/validations/numericality.rb
activerecord-7.1.4.1 lib/active_record/validations/numericality.rb
activerecord-8.0.0.beta1 lib/active_record/validations/numericality.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/validations/numericality.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/validations/numericality.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/validations/numericality.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/validations/numericality.rb
omg-activerecord-8.0.0.alpha3 lib/active_record/validations/numericality.rb