Get the changed attributes for the document.
@example Get the changed attributes.
model.changed
@return [ Array<String> ] The changed attributes.
@since 2.4.0
# File lib/mongoid/dirty.rb, line 13 def changed changed_attributes.keys end
Has the document changed?
@example Has the document changed?
model.changed?
@return [ true, false ] If the document is changed.
@since 2.4.0
# File lib/mongoid/dirty.rb, line 25 def changed? changes.values.any? { |val| val } || children_changed? end
Get the attribute changes.
@example Get the attribute changes.
model.changed_attributes
@return [ Hash<String, Object> ] The attribute changes.
@since 2.4.0
# File lib/mongoid/dirty.rb, line 51 def changed_attributes @changed_attributes ||= {} end
Get all the changes for the document.
@example Get all the changes.
model.changes
@return [ Hash<String, Array<Object, Object> ] The changes.
@since 2.4.0
# File lib/mongoid/dirty.rb, line 63 def changes _changes = {} changed.each do |attr| change = attribute_change(attr) _changes[attr] = change if change end _changes end
Have any children (embedded documents) of this document changed?
@example Have any children changed?
model.children_changed?
@return [ true, false ] If any children have changed.
@since 2.4.1
# File lib/mongoid/dirty.rb, line 37 def children_changed? _children.any? do |child| child.changed? end end
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.
@example Move the changes to previous.
person.move_changes
@since 2.1.0
# File lib/mongoid/dirty.rb, line 81 def move_changes @_children = nil @previous_changes = changes Atomic::UPDATES.each do |update| send(update).clear end changed_attributes.clear end
Things that need to execute after a document has been persisted.
@example Handle post persistence.
document.post_persist
@since 3.0.0
# File lib/mongoid/dirty.rb, line 96 def post_persist reset_persisted_children move_changes clear_timeless_option end
Get the previous changes on the document.
@example Get the previous changes.
model.previous_changes
@return [ Hash<String, Array<Object, Object> ] The previous changes.
@since 2.4.0
# File lib/mongoid/dirty.rb, line 110 def previous_changes @previous_changes ||= {} end
Remove a change from the dirty attributes hash. Used by the single field atomic updators.
@example Remove a flagged change.
model.remove_change(:field)
@param [ Symbol, String ] name The name of the field.
@since 2.1.0
# File lib/mongoid/dirty.rb, line 123 def remove_change(name) changed_attributes.delete(name.to_s) end
Gets all the new values for each of the changed fields, to be passed to a MongoDB $set modifier.
@example Get the setters for the atomic updates.
person = Person.new(:title => "Sir") person.title = "Madam" person.setters # returns { "title" => "Madam" }
@return [ Hash ] A Hash
of atomic setters.
@since 2.0.0
# File lib/mongoid/dirty.rb, line 138 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