Module: Mongoid::Persistence::Modification

Included in:
Operations::Update
Defined in:
lib/mongoid/persistence/modification.rb

Overview

Contains common logic for modification operations.

Instance Method Summary collapse

Instance Method Details

#prepare(&block) ⇒ true, false

Wrap all the common modification logic for both root and embedded documents and then yield to the block.

Examples:

Execute common modification logic.

prepare do |doc|
  collection.update({ :_id => 1 }, { :field => "value })
end

Parameters:

  • block (Proc)

    The block to call.

Returns:

  • (true, false)

    If the save passed or not.

Since:

  • 2.1.0



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongoid/persistence/modification.rb', line 21

def prepare(&block)
  return false if validating? && document.invalid?(:update)
  document.run_callbacks(:save) do
    document.run_callbacks(:update) do
      yield(document); true
    end
  end.tap do |result|
    unless result == false
      document.reset_persisted_children
      document.move_changes
      Threaded.clear_options!
    end
  end
end