Class: ActiveModelValidators::ImmutabilityValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ActiveModelValidators::ImmutabilityValidator
- Defined in:
- lib/active_model_validators/immutability_validator.rb
Overview
Attributes immutability validator
Instance Method Summary collapse
-
#validate_each(record, attribute, value) ⇒ Object
Adds error if there an attempt to change an immutable attribute.
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
Adds error if there an attempt to change an immutable attribute
-
record- ActiveRecord model -
attr- model attribute which is supposed to be immutable -
value- value
Example
class Model < ActiveRecord::Base
validates :my_atribute, :'active_model_validators/immutability' => true
end
my_model_instance = MyModel.create(my_attribute: 'foo')
my_model_instance.my_attribute = 'bar'
my_model_instance.valid? # => false
20 21 22 23 24 25 26 |
# File 'lib/active_model_validators/immutability_validator.rb', line 20 def validate_each(record, attribute, value) return if record.new_record? if record.public_send("#{attribute}_changed?") record.errors.add(attribute, :immutability) end end |