Module: AttributeGuard::ClassMethods
- Defined in:
- lib/attribute_guard.rb
Instance Method Summary collapse
-
#lock_attributes(*attributes, error: :locked, mode: :error) ⇒ void
Locks the given attributes so that they cannot be changed directly.
-
#locked_attribute_names ⇒ Array<String>
Returns the names of the locked attributes.
Instance Method Details
#lock_attributes(*attributes, error: :locked, mode: :error) ⇒ void
This method returns an undefined value.
Locks the given attributes so that they cannot be changed directly. Subclasses inherit the locked attributes from their parent classes.
You can optionally specify a mode of what to do when a locked attribute is changed. The default is to add an error to the model, but you can also specify :warn to log a warning or a Proc to call with the record and attribute name.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/attribute_guard.rb', line 79 def lock_attributes(*attributes, error: :locked, mode: :error) locked = locked_attributes.dup error = error.dup.freeze if error.is_a?(String) attributes.flatten.each do |attribute| locked[attribute.to_s] = [error, mode] end self.locked_attributes = locked end |
#locked_attribute_names ⇒ Array<String>
Returns the names of the locked attributes.
93 94 95 |
# File 'lib/attribute_guard.rb', line 93 def locked_attribute_names locked_attributes.keys end |