Class: ActiveModel::Validations::WriteOnceValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/can_has_validations/validators/write_once_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object

as of ActiveModel 4, :allow_nil=>true causes a change from a value back to

nil to be allowed. prevent this.


13
14
15
16
17
18
# File 'lib/can_has_validations/validators/write_once_validator.rb', line 13

def validate(record)
  attributes.each do |attribute|
    value = record.read_attribute_for_validation(attribute)
    validate_each(record, attribute, value)
  end
end

#validate_each(record, attribute, value) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/can_has_validations/validators/write_once_validator.rb', line 20

def validate_each(record, attribute, value)
  if record.persisted? && record.send("#{attribute}_changed?")
    if options[:immutable_nil] || !record.send("#{attribute}_was").nil?
      record.errors.add(attribute, :unchangeable, options)
    end
  end
end