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.


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

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



18
19
20
21
22
# File 'lib/can_has_validations/validators/write_once_validator.rb', line 18

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