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 =
[
  :before_validation, :after_validation,
  :after_initialize, :after_build,
  :before_create, :around_create, :after_create,
  :before_destroy, :around_destroy, :after_destroy,
  :before_save, :around_save, :after_save,
  :before_update, :around_update, :after_update,
]

Instance Method Summary collapse

Instance Method Details

#callback_executable?(kind) ⇒ true, false

Is the provided type of callback executable by this document?

document.callback_executable?(:save)

Examples:

Is the callback executable?

Parameters:

  • kin (Symbol)

    The type of callback.

Returns:

  • (true, false)

    If the callback can be executed.

Since:

  • 2.5.1



36
37
38
# File 'lib/mongoid/callbacks.rb', line 36

def callback_executable?(kind)
  respond_to?("_#{kind}_callbacks")
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



55
56
57
58
59
60
61
62
# File 'lib/mongoid/callbacks.rb', line 55

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
  callback_executable?(kind) ? super(kind, *args, &block) : true
end