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

#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



16
17
18
19
20
21
22
23
24
# File 'lib/mongoid/dirty.rb', line 16

def move_changes
  @_children = nil
  @previously_changed = changes
  atomic_pulls.clear
  atomic_unsets.clear
  delayed_atomic_sets.clear
  delayed_atomic_pulls.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



35
36
37
# File 'lib/mongoid/dirty.rb', line 35

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.



48
49
50
51
52
53
54
55
56
57
# File 'lib/mongoid/dirty.rb', line 48

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