EachValidator is a validator which iterates through the attributes given in the options hash invoking the validate_each method passing in the record, attribute and value.
All Active Model validations are built on top of this validator.
Returns a new validator instance. All options will be available via the options reader, however the :attributes option will be removed and instead be made available through the attributes reader.
# File lib/active_model/validator.rb, line 139 139: def initialize(options) 140: @attributes = Array.wrap(options.delete(:attributes)) 141: raise ":attributes cannot be blank" if @attributes.empty? 142: super 143: check_validity! 144: end
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.
# File lib/active_model/validator.rb, line 166 166: def check_validity! 167: end
Performs validation on the supplied record. By default this will call validates_each to determine validity therefore subclasses should override validates_each with validation logic.
# File lib/active_model/validator.rb, line 149 149: def validate(record) 150: attributes.each do |attribute| 151: value = record.read_attribute_for_validation(attribute) 152: next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) 153: validate_each(record, attribute, value) 154: end 155: end
Override this method in subclasses with the validation logic, adding errors to the records errors array where necessary.
# File lib/active_model/validator.rb, line 159 159: def validate_each(record, attribute, value) 160: raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method" 161: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.