Module: Volt::Models::Helpers::Dirty
- Included in:
- Volt::Model
- Defined in:
- lib/volt/models/helpers/dirty.rb
Overview
The dirty module provides helper methods for working with and tracking previous values on model attributes.
Instance Method Summary collapse
- #attribute_will_change!(attribute_name, old_value) ⇒ Object
-
#changed?(key = nil) ⇒ Boolean
Return true if key has changed.
-
#changed_attributes ⇒ Object
Return the list of attributes that have changed since the last ‘save’ event.
-
#changes(key) ⇒ Object
Grab all previous versions of for key.
-
#clear_tracked_changes! ⇒ Object
Clear changed attributes.
-
#method_missing(method_name, *args, &block) ⇒ Object
Handle change and was method calls Example: name_was or name_changes.
-
#revert_changes! ⇒ Object
Reverts the model attributes back to the pre-change values.
-
#was(key) ⇒ Object
Grab the previous value for the key.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Handle change and was method calls Example: name_was or name_changes
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/volt/models/helpers/dirty.rb', line 66 def method_missing(method_name, *args, &block) # Quick check to see if changes or was are being called, this check # keeps us from needing to parse out the parts if we're not going # to use them. if method_name =~ /[_](changes|was)$/ # Break apart the method call # TODO: no destructuring because of https://github.com/opal/opal/issues/663 *parts = method_name.to_s.split('_') action = parts.pop key = parts.join('_').to_sym # Handle changes or was calls. case action when 'changes' return changes(key) when 'was' return was(key) end end # Otherwise, run super super end |
Instance Method Details
#attribute_will_change!(attribute_name, old_value) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/volt/models/helpers/dirty.rb', line 57 def attribute_will_change!(attribute_name, old_value) # Don't track nil models old_value = nil if old_value.nil? (changed_attributes[attribute_name] ||= []) << old_value end |
#changed?(key = nil) ⇒ Boolean
Return true if key has changed
13 14 15 16 17 18 19 20 |
# File 'lib/volt/models/helpers/dirty.rb', line 13 def changed?(key = nil) if key # return the changed values for the keys changed_attributes.key?(key) else changed_attributes.present? end end |
#changed_attributes ⇒ Object
Return the list of attributes that have changed since the last ‘save’ event.
8 9 10 |
# File 'lib/volt/models/helpers/dirty.rb', line 8 def changed_attributes @changed_attributes ||= {} end |
#changes(key) ⇒ Object
Grab all previous versions of for key
23 24 25 |
# File 'lib/volt/models/helpers/dirty.rb', line 23 def changes(key) changed_attributes[key] end |
#clear_tracked_changes! ⇒ Object
Clear changed attributes
41 42 43 |
# File 'lib/volt/models/helpers/dirty.rb', line 41 def clear_tracked_changes! @changed_attributes = {} end |
#revert_changes! ⇒ Object
Reverts the model attributes back to the pre-change values.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/volt/models/helpers/dirty.rb', line 46 def revert_changes! # Reassign the first value since we started tracking if @changed_attributes @changed_attributes.each_pair do |key, value| @attributes[key] = value.first end end clear_tracked_changes! end |
#was(key) ⇒ Object
Grab the previous value for the key
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/volt/models/helpers/dirty.rb', line 28 def was(key) val = changed_attributes[key] # Doing val && val[0] doesn't work in opal # https://github.com/opal/opal/issues/664 if val val[0] else nil end end |