Module: Mongoid::Dirty

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Dirty
Included in:
Components
Defined in:
lib/mongoid/dirty.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#changesHash

Note:

This is overriding the AM::Dirty implementation to handle enumerable fields being in the hash when not actually changed.

Get the changed values for the document. This is a hash with the name of the field as the keys, and the values being an array of previous and current pairs.

Examples:

Get the changes.

document.changes

Returns:

  • (Hash)

    The changed values.

Since:

  • 2.1.0



20
21
22
23
24
25
26
27
28
29
# File 'lib/mongoid/dirty.rb', line 20

def changes
  {}.tap do |hash|
    changed.each do |name|
      change = attribute_change(name)
      if change
        hash[name] = change if change[0] != change[1]
      end
    end
  end
end

#move_changesObject

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.

Examples:

Move the changes to previous.

person.move_changes

Since:

  • 2.1.0



40
41
42
43
44
45
46
47
# File 'lib/mongoid/dirty.rb', line 40

def move_changes
  @_children = nil
  @previously_changed = changes
  atomic_pulls.clear
  atomic_unsets.clear
  delayed_atomic_sets.clear
  changed_attributes.clear
end

#remove_change(name) ⇒ Object

Remove a change from the dirty attributes hash. Used by the single field atomic updators.

Examples:

Remove a flagged change.

model.remove_change(:field)

Parameters:

Since:

  • 2.1.0



58
59
60
# File 'lib/mongoid/dirty.rb', line 58

def remove_change(name)
  changed_attributes.delete(name.to_s)
end

#settersHash

Gets all the new values for each of the changed fields, to be passed to a MongoDB $set modifier.

Examples:

Get the setters for the atomic updates.

person = Person.new(:title => "Sir")
person.title = "Madam"
person.setters # returns { "title" => "Madam" }

Returns:

  • (Hash)

    A Hash of atomic setters.



71
72
73
74
75
76
77
78
# File 'lib/mongoid/dirty.rb', line 71

def setters
  {}.tap do |modifications|
    changes.each_pair do |field, changes|
      key = embedded? ? "#{atomic_position}.#{field}" : field
      modifications[key] = changes[1]
    end
  end
end