Module: Mongoid::Changeable

Extended by:
ActiveSupport::Concern
Included in:
Composable
Defined in:
build/mongoid-7.0/lib/mongoid/changeable.rb

Overview

Defines behaviour for dirty tracking.

Since:

  • 4.0.0

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:

  • (Array<String>)

    The changed attributes.

Since:

  • 2.4.0



18
19
20
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 18

def changed
  changed_attributes.keys.select { |attr| attribute_change(attr) }
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



30
31
32
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 30

def changed?
  changes.values.any? { |val| val } || children_changed?
end

#changed_attributesHash<String, Object>

Get the attribute changes.

Examples:

Get the attribute changes.

model.changed_attributes

Returns:

  • (Hash<String, Object>)

    The attribute changes.

Since:

  • 2.4.0



54
55
56
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 54

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:

  • (Hash<String, Array<Object, Object> ] The changes.)

    Hash<String, Array<Object, Object> ] The changes.

Since:

  • 2.4.0



66
67
68
69
70
71
72
73
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 66

def changes
  _changes = {}
  changed.each do |attr|
    change = attribute_change(attr)
    _changes[attr] = change if change
  end
  _changes.with_indifferent_access
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



42
43
44
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 42

def children_changed?
  _children.any?(&:changed?)
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



84
85
86
87
88
89
90
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 84

def move_changes
  @previous_changes = changes
  Atomic::UPDATES.each do |update|
    send(update).clear
  end
  changed_attributes.clear
end

#post_persistObject

Things that need to execute after a document has been persisted.

Examples:

Handle post persistence.

document.post_persist

Since:

  • 3.0.0



98
99
100
101
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 98

def post_persist
  reset_persisted_children
  move_changes
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:

  • (Hash<String, Array<Object, Object> ] The previous changes.)

    Hash<String, Array<Object, Object> ] The previous changes.

Since:

  • 2.4.0



111
112
113
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 111

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:

  • name (Symbol, String)

    The name of the field.

Since:

  • 2.1.0



124
125
126
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 124

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.

Since:

  • 2.0.0



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'build/mongoid-7.0/lib/mongoid/changeable.rb', line 139

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