Module: Mongoid::Callbacks

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

Overview

This module contains all the callback hooks for Mongoid.

Constant Summary collapse

CALLBACKS =
[
  :after_build,
  :after_create,
  :after_destroy,
  :after_initialize,
  :after_save,
  :after_update,
  :after_upsert,
  :after_validation,
  :around_create,
  :around_destroy,
  :around_save,
  :around_update,
  :around_upsert,
  :before_create,
  :before_destroy,
  :before_save,
  :before_update,
  :before_upsert,
  :before_validation
]

Instance Method Summary collapse

Instance Method Details

#run_after_callbacks(*kinds) ⇒ Object

Note:

ActiveSupport does not allow this type of behaviour by default, so Mongoid has to get around it and implement itself.

Run only the after callbacks for the specific event.

Examples:

Run only the after save callbacks.

model.run_after_callbacks(:save)

Parameters:

  • kinds (Array<Symbol>)

    The events that are occurring.

Returns:

  • (Object)

    The result of the chain executing.

Since:

  • 3.0.0



52
53
54
55
56
# File 'lib/mongoid/callbacks.rb', line 52

def run_after_callbacks(*kinds)
  kinds.each do |kind|
    run_targeted_callbacks(:after, kind)
  end
end

#run_before_callbacks(*kinds) ⇒ Object

Note:

ActiveSupport does not allow this type of behaviour by default, so Mongoid has to get around it and implement itself.

Run only the before callbacks for the specific event.

Examples:

Run only the before save callbacks.

model.run_before_callbacks(:save, :create)

Parameters:

  • kinds (Array<Symbol>)

    The events that are occurring.

Returns:

  • (Object)

    The result of the chain executing.

Since:

  • 3.0.0



71
72
73
74
75
# File 'lib/mongoid/callbacks.rb', line 71

def run_before_callbacks(*kinds)
  kinds.each do |kind|
    run_targeted_callbacks(:before, kind)
  end
end

#run_callbacks(kind, *args, &block) ⇒ Document

Run the callbacks for the document. This overrides active support’s functionality to cascade callbacks to embedded documents that have been flagged as such.

Examples:

Run the callbacks.

run_callbacks :save do
  save!
end

Parameters:

  • kind (Symbol)

    The type of callback to execute.

  • *args (Array)

    Any options.

Returns:

Since:

  • 2.3.0



92
93
94
95
96
97
98
99
# File 'lib/mongoid/callbacks.rb', line 92

def run_callbacks(kind, *args, &block)
  cascadable_children(kind).each do |child|
    unless child.run_callbacks(child_callback_type(kind, child), *args)
      return false
    end
  end
  super(kind, *args, &block)
end