module Volt::Models::Helpers::Dirty
The dirty module provides helper methods for working with and tracking previous values on model attributes.
Public Instance Methods
attribute_will_change!(attribute_name, old_value)
click to toggle source
# 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)
click to toggle source
Return true if key has changed
# 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()
click to toggle source
Return the list of attributes that have changed since the last 'save' event.
# File lib/volt/models/helpers/dirty.rb, line 8 def changed_attributes @changed_attributes ||= {} end
changes(key)
click to toggle source
Grab all previous versions of for key
# File lib/volt/models/helpers/dirty.rb, line 23 def changes(key) changed_attributes[key] end
clear_tracked_changes!()
click to toggle source
Clear changed attributes
# File lib/volt/models/helpers/dirty.rb, line 41 def clear_tracked_changes! @changed_attributes = {} end
method_missing(method_name, *args, &block)
click to toggle source
Handle change and was method calls Example: name_was or name_changes
Calls superclass method
# 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
revert_changes!()
click to toggle source
Reverts the model attributes back to the pre-change values.
# 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)
click to toggle source
Grab the previous value for the key
# 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