Class: ActiveModel::EachValidator
- Defined in:
- lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/validator.rb
Overview
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.
Direct Known Subclasses
BlockValidator, Validations::AbsenceValidator, Validations::AcceptanceValidator, Validations::ComparisonValidator, Validations::ConfirmationValidator, Validations::ExclusionValidator, Validations::FormatValidator, Validations::InclusionValidator, Validations::LengthValidator, Validations::NumericalityValidator, Validations::PresenceValidator, Validations::WithValidator, ActiveRecord::Validations::AssociatedValidator, ActiveRecord::Validations::UniquenessValidator
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Attributes inherited from Validator
Instance Method Summary collapse
-
#check_validity! ⇒ Object
Hook method that gets called by the initializer allowing verification that the arguments supplied are valid.
-
#initialize(options) ⇒ EachValidator
constructor
Returns a new validator instance.
-
#validate(record) ⇒ Object
Performs validation on the supplied record.
-
#validate_each(record, attribute, value) ⇒ Object
Override this method in subclasses with the validation logic, adding errors to the records
errors
array where necessary.
Methods inherited from Validator
Constructor Details
#initialize(options) ⇒ EachValidator
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.
138 139 140 141 142 143 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/validator.rb', line 138 def initialize() @attributes = Array(.delete(:attributes)) raise ArgumentError, ":attributes cannot be blank" if @attributes.empty? super check_validity! end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
133 134 135 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/validator.rb', line 133 def attributes @attributes end |
Instance Method Details
#check_validity! ⇒ Object
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.
166 167 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/validator.rb', line 166 def check_validity! end |
#validate(record) ⇒ Object
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.
148 149 150 151 152 153 154 155 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/validator.rb', line 148 def validate(record) attributes.each do |attribute| value = record.read_attribute_for_validation(attribute) next if (value.nil? && [:allow_nil]) || (value.blank? && [:allow_blank]) value = prepare_value_for_validation(value, record, attribute) validate_each(record, attribute, value) end end |
#validate_each(record, attribute, value) ⇒ Object
Override this method in subclasses with the validation logic, adding errors to the records errors
array where necessary.
159 160 161 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/validator.rb', line 159 def validate_each(record, attribute, value) raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method" end |