Module: Mongoid::Dirty

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

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#changedArray<String>

Get the changed attributes for the document.

Examples:

Get the changed attributes.

model.changed

Returns:

Since:

  • 2.4.0



14
15
16
# File 'lib/mongoid/dirty.rb', line 14

def changed
  changed_attributes.keys
end

#changed?true, false

Has the document changed?

Examples:

Has the document changed?

model.changed?

Returns:

  • (true, false)

    If the document is changed.

Since:

  • 2.4.0



26
27
28
# File 'lib/mongoid/dirty.rb', line 26

def changed?
  changed_attributes.any? || children_changed?
end

#changed_attributesHash<String, Object>

Get the attribute changes.

Examples:

Get the attribute changes.

model.changed_attributes

Returns:

Since:

  • 2.4.0



52
53
54
# File 'lib/mongoid/dirty.rb', line 52

def changed_attributes
  @changed_attributes ||= {}
end

#changesHash<String, Array<Object, Object> ] The changes.

Get all the changes for the document.

Examples:

Get all the changes.

model.changes

Returns:

Since:

  • 2.4.0



64
65
66
67
68
69
70
# File 'lib/mongoid/dirty.rb', line 64

def changes
  changed.inject({}.with_indifferent_access) do |changes, attr|
    changes.tap do |hash|
      hash[attr] = attribute_change(attr)
    end
  end
end

#children_changed?true, false

Have any children (embedded documents) of this document changed?

Examples:

Have any children changed?

model.children_changed?

Returns:

  • (true, false)

    If any children have changed.

Since:

  • 2.4.1



38
39
40
41
42
# File 'lib/mongoid/dirty.rb', line 38

def children_changed?
  _children.any? do |child|
    child.changed?
  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



81
82
83
84
85
86
87
88
# 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

#previous_changesHash<String, Array<Object, Object> ] The previous changes.

Get the previous changes on the document.

Examples:

Get the previous changes.

model.previous_changes

Returns:

Since:

  • 2.4.0



98
99
100
# File 'lib/mongoid/dirty.rb', line 98

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.

Examples:

Remove a flagged change.

model.remove_change(:field)

Parameters:

Since:

  • 2.1.0



111
112
113
# File 'lib/mongoid/dirty.rb', line 111

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.

@todo: Durran: Refactor 3.0

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mongoid/dirty.rb', line 126

def setters
  {}.tap do |modifications|
    changes.each_pair do |name, changes|
      if changes
        old, new = changes
        field = fields[name]
        key = embedded? ? "#{atomic_position}.#{name}" : name
        if field && field.resizable?
          field.add_atomic_changes(
            self,
            name,
            key,
            modifications,
            new,
            old
          )
        else
          modifications[key] = new
        end
      end
    end
  end
end