Module: Mongoid::Changeable
Overview
Defines behaviour for dirty tracking.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#changed ⇒ Array<String>
Get the changed attributes for the document.
-
#changed? ⇒ true, false
Has the document changed?.
-
#changed_attributes ⇒ Hash<String, Object>
Get the attribute changes.
-
#changes ⇒ Hash<String, Array<Object, Object> ] The changes.
Get all the changes for the document.
-
#children_changed? ⇒ true, false
Have any children (embedded documents) of this document changed?.
-
#move_changes ⇒ Object
Call this method after save, so the changes can be properly switched.
-
#post_persist ⇒ Object
Things that need to execute after a document has been persisted.
-
#previous_changes ⇒ Hash<String, Array<Object, Object> ] The previous changes.
Get the previous changes on the document.
-
#remove_change(name) ⇒ Object
Remove a change from the dirty attributes hash.
-
#setters ⇒ Hash
Gets all the new values for each of the changed fields, to be passed to a MongoDB $set modifier.
Instance Method Details
#changed ⇒ Array<String>
Get the changed attributes for the document.
18 19 20 |
# File 'lib/mongoid/changeable.rb', line 18 def changed changed_attributes.keys end |
#changed? ⇒ true, false
Has the document changed?
30 31 32 |
# File 'lib/mongoid/changeable.rb', line 30 def changed? changes.values.any? { |val| val } || children_changed? end |
#changed_attributes ⇒ Hash<String, Object>
Get the attribute changes.
56 57 58 |
# File 'lib/mongoid/changeable.rb', line 56 def changed_attributes @changed_attributes ||= {} end |
#changes ⇒ Hash<String, Array<Object, Object> ] The changes.
Get all the changes for the document.
68 69 70 71 72 73 74 75 |
# File 'lib/mongoid/changeable.rb', line 68 def changes _changes = {} changed.each do |attr| change = attribute_change(attr) _changes[attr] = change if change end _changes end |
#children_changed? ⇒ true, false
Have any children (embedded documents) of this document changed?
42 43 44 45 46 |
# File 'lib/mongoid/changeable.rb', line 42 def children_changed? _children.any? do |child| child.changed? end end |
#move_changes ⇒ Object
Call this method after save, so the changes can be properly switched.
This will unset the memoized children array, set new record to false, set the document as validated, and move the dirty changes.
86 87 88 89 90 91 92 93 |
# File 'lib/mongoid/changeable.rb', line 86 def move_changes @_children = nil @previous_changes = changes Atomic::UPDATES.each do |update| send(update).clear end changed_attributes.clear end |
#post_persist ⇒ Object
Things that need to execute after a document has been persisted.
101 102 103 104 |
# File 'lib/mongoid/changeable.rb', line 101 def post_persist reset_persisted_children move_changes end |
#previous_changes ⇒ Hash<String, Array<Object, Object> ] The previous changes.
Get the previous changes on the document.
114 115 116 |
# File 'lib/mongoid/changeable.rb', line 114 def previous_changes @previous_changes ||= {} end |
#remove_change(name) ⇒ Object
Remove a change from the dirty attributes hash. Used by the single field atomic updators.
127 128 129 |
# File 'lib/mongoid/changeable.rb', line 127 def remove_change(name) changed_attributes.delete(name.to_s) end |
#setters ⇒ Hash
Gets all the new values for each of the changed fields, to be passed to a MongoDB $set modifier.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/mongoid/changeable.rb', line 142 def setters mods = {} changes.each_pair do |name, changes| if changes old, new = changes field = fields[name] key = atomic_attribute_name(name) if field && field.resizable? field.add_atomic_changes(self, name, key, mods, new, old) else mods[key] = new unless atomic_unsets.include?(key) end end end mods end |