Module: Mongoid::Dirty
Overview
:nodoc:
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.
-
#move_changes ⇒ Object
Call this method after save, so the changes can be properly switched.
-
#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.
14 15 16 |
# File 'lib/mongoid/dirty.rb', line 14 def changed changed_attributes.keys end |
#changed? ⇒ true, false
Has the document changed?
26 27 28 |
# File 'lib/mongoid/dirty.rb', line 26 def changed? changed_attributes.any? end |
#changed_attributes ⇒ Hash<String, Object>
Get the attribute changes.
38 39 40 |
# File 'lib/mongoid/dirty.rb', line 38 def changed_attributes @changed_attributes ||= {} end |
#changes ⇒ Hash<String, Array<Object, Object> ] The changes.
Get all the changes for the document.
50 51 52 53 54 55 56 |
# File 'lib/mongoid/dirty.rb', line 50 def changes changed.inject({}.with_indifferent_access) do |changes, attr| changes.tap do |hash| hash[attr] = attribute_change(attr) end 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.
67 68 69 70 71 72 73 74 |
# File 'lib/mongoid/dirty.rb', line 67 def move_changes @_children = nil @previous_changes = changes Atomic::UPDATES.each do |update| send(update).clear end changed_attributes.clear end |
#previous_changes ⇒ Hash<String, Array<Object, Object> ] The previous changes.
Get the previous changes on the document.
84 85 86 |
# File 'lib/mongoid/dirty.rb', line 84 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.
97 98 99 |
# File 'lib/mongoid/dirty.rb', line 97 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.
@todo: Durran: Refactor 3.0
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mongoid/dirty.rb', line 112 def setters {}.tap do |modifications| changes.each_pair do |name, changes| if changes old, new = changes field = fields[name] key = ? "#{atomic_position}.#{name}" : name if field && field.resizable? pushes, pulls = new - (old || []), (old || []) - new field.add_atomic_changes(self, key, modifications, pushes, pulls) else modifications[key] = new end end end end end |