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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/attribute_guard.rb', line 115 def lock_attributes(*attributes, error: :locked, mode: :error) unless mode == :error || mode == :warn || mode == :raise || mode.respond_to?(:call) raise ArgumentError.new("Invalid mode: #{mode.inspect}") end 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.
133 134 135 |
# File 'lib/attribute_guard.rb', line 133 def locked_attribute_names locked_attributes.keys end |