Class: AttributeGuard::LockedAttributesValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- AttributeGuard::LockedAttributesValidator
- Defined in:
- lib/attribute_guard.rb
Overview
Validator that checks for changes to locked attributes.
Instance Method Summary collapse
Instance Method Details
#validate(record) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/attribute_guard.rb', line 61 def validate(record) unless record.respond_to?(:new_record?) raise "AttributeGuard can only be used with models that respond to :new_record?" end return if record.new_record? record.class.send(:locked_attributes).each do |attribute, params| if record.changes.include?(attribute) && record.attribute_locked?(attribute) , mode = params if mode == :warn log_warning(record, attribute) elsif mode == :raise raise LockedAttributeError.new((record, attribute)) elsif mode.is_a?(Proc) mode.call(record, attribute) else record.errors.add(attribute, ) end end end end |