Module: Mongoid::Interceptable

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

Overview

This module contains all the callback hooks for Mongoid.

Since:

  • 4.0.0

Constant Summary collapse

CALLBACKS =

Since:

  • 4.0.0

[
  :after_build,
  :after_create,
  :after_destroy,
  :after_find,
  :after_initialize,
  :after_save,
  :after_touch,
  :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
].freeze

Instance Method Summary collapse

Instance Method Details

#callback_executable?(kind) ⇒ true, false

Is the provided type of callback executable by this document?

Examples:

Is the callback executable?

document.callback_executable?(:save)

Parameters:

  • kind (Symbol)

    The type of callback.

Returns:

  • (true, false)

    If the callback can be executed.

Since:

  • 3.0.6



56
57
58
# File 'build/mongoid-7.3/lib/mongoid/interceptable.rb', line 56

def callback_executable?(kind)
  respond_to?("_#{kind}_callbacks")
end

#in_callback_state?(kind) ⇒ true, false

Is the document currently in a state that could potentially require callbacks to be executed?

Examples:

Is the document in a callback state?

document.in_callback_state?(:update)

Parameters:

  • kind (Symbol)

    The callback kind.

Returns:

  • (true, false)

    If the document is in a callback state.

Since:

  • 3.1.0



71
72
73
# File 'build/mongoid-7.3/lib/mongoid/interceptable.rb', line 71

def in_callback_state?(kind)
  [ :create, :destroy ].include?(kind) || new_record? || flagged_for_destroy? || changed?
end

#run_after_callbacks(*kinds) ⇒ Object

Note:

ActiveSupport does not allow this type of behavior 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



88
89
90
91
92
# File 'build/mongoid-7.3/lib/mongoid/interceptable.rb', line 88

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 behavior 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



107
108
109
110
111
# File 'build/mongoid-7.3/lib/mongoid/interceptable.rb', line 107

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

#run_callbacksDocument

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



128
129
130
131
132
133
134
135
136
137
138
139
# File 'build/mongoid-7.3/lib/mongoid/interceptable.rb', line 128

ruby2_keywords def run_callbacks(kind, *args, &block)
  cascadable_children(kind).each do |child|
    if child.run_callbacks(child_callback_type(kind, child), *args) == false
      return false
    end
  end
  if callback_executable?(kind)
    super(kind, *args, &block)
  else
    true
  end
end