Module: Mongoid::Interceptable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Composable
- Defined in:
- lib/mongoid/interceptable.rb
Overview
This module contains all the callback hooks for Mongoid.
Constant Summary collapse
- CALLBACKS =
[ :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
-
#callback_executable?(kind) ⇒ true, false
Is the provided type of callback executable by this document?.
-
#in_callback_state?(kind) ⇒ true, false
Is the document currently in a state that could potentially require callbacks to be executed?.
-
#run_after_callbacks(*kinds) ⇒ Object
Run only the after callbacks for the specific event.
-
#run_before_callbacks(*kinds) ⇒ Object
Run only the before callbacks for the specific event.
-
#run_callbacks ⇒ Document
Run the callbacks for the document.
Instance Method Details
#callback_executable?(kind) ⇒ true, false
Is the provided type of callback executable by this document?
51 52 53 |
# File 'lib/mongoid/interceptable.rb', line 51 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?
64 65 66 |
# File 'lib/mongoid/interceptable.rb', line 64 def in_callback_state?(kind) [ :create, :destroy ].include?(kind) || new_record? || flagged_for_destroy? || changed? end |
#run_after_callbacks(*kinds) ⇒ Object
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.
79 80 81 82 83 |
# File 'lib/mongoid/interceptable.rb', line 79 def run_after_callbacks(*kinds) kinds.each do |kind| run_targeted_callbacks(:after, kind) end end |
#run_before_callbacks(*kinds) ⇒ Object
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.
96 97 98 99 100 |
# File 'lib/mongoid/interceptable.rb', line 96 def run_before_callbacks(*kinds) kinds.each do |kind| run_targeted_callbacks(:before, kind) end end |
#run_callbacks ⇒ 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.
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mongoid/interceptable.rb', line 115 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 |