lib/active_model/validator.rb in activemodel-6.0.6.1 vs lib/active_model/validator.rb in activemodel-6.1.0.rc1

- old
+ new

@@ -83,11 +83,11 @@ # validates :title, presence: true, title: true # end # # It can be useful to access the class that is using that validator when there are prerequisites such # as an +attr_accessor+ being present. This class is accessible via <tt>options[:class]</tt> in the constructor. - # To setup your validator override the constructor. + # To set up your validator override the constructor. # # class MyValidator < ActiveModel::Validator # def initialize(options={}) # super # options[:class].attr_accessor :custom_attribute @@ -145,11 +145,11 @@ # Performs validation on the supplied record. By default this will call # +validate_each+ to determine validity therefore subclasses should # override +validate_each+ with validation logic. def validate(record) attributes.each do |attribute| - value = record.read_attribute_for_validation(attribute) + value = read_attribute_for_validation(record, attribute) next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) validate_each(record, attribute, value) end end @@ -162,9 +162,14 @@ # Hook method that gets called by the initializer allowing verification # that the arguments supplied are valid. You could for example raise an # +ArgumentError+ when invalid options are supplied. def check_validity! end + + private + def read_attribute_for_validation(record, attr_name) + record.read_attribute_for_validation(attr_name) + end end # +BlockValidator+ is a special +EachValidator+ which receives a block on initialization # and call this block for each attribute being validated. +validates_each+ uses this validator. class BlockValidator < EachValidator #:nodoc: