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
-
#_mongoid_run_child_callbacks(kind, children: nil, &block) ⇒ Object
private
Run the callbacks for embedded documents.
-
#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?.
-
#pending_callbacks ⇒ Array<Symbol>
private
This is used to store callbacks to be executed later.
- #pending_callbacks=(value) ⇒ Object private
-
#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(kind, with_children: true, &block) ⇒ Object
Run the callbacks for the document.
-
#run_pending_callbacks ⇒ Object
private
Run the pending callbacks.
Instance Method Details
#_mongoid_run_child_callbacks(kind, children: nil, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Run the callbacks for embedded documents.
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/mongoid/interceptable.rb', line 142 def _mongoid_run_child_callbacks(kind, children: nil, &block) child, *tail = (children || cascadable_children(kind)) if child.nil? return block&.call elsif tail.empty? return child.run_callbacks(child_callback_type(kind, child), &block) else return child.run_callbacks(child_callback_type(kind, child)) do _mongoid_run_child_callbacks(kind, children: tail, &block) end end end |
#callback_executable?(kind) ⇒ true | false
Is the provided type of callback executable by this document?
57 58 59 |
# File 'lib/mongoid/interceptable.rb', line 57 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?
70 71 72 |
# File 'lib/mongoid/interceptable.rb', line 70 def in_callback_state?(kind) [ :create, :destroy ].include?(kind) || new_record? || flagged_for_destroy? || changed? end |
#pending_callbacks ⇒ Array<Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This is used to store callbacks to be executed later. A good use case for this is delaying the after_find and after_initialize callbacks until the associations are set on the document. This can also be used to delay applying the defaults on a document.
163 164 165 |
# File 'lib/mongoid/interceptable.rb', line 163 def pending_callbacks @pending_callbacks ||= [].to_set end |
#pending_callbacks=(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
168 169 170 |
# File 'lib/mongoid/interceptable.rb', line 168 def pending_callbacks=(value) @pending_callbacks = value 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.
85 86 87 88 89 |
# File 'lib/mongoid/interceptable.rb', line 85 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.
102 103 104 105 106 |
# File 'lib/mongoid/interceptable.rb', line 102 def run_before_callbacks(*kinds) kinds.each do |kind| run_targeted_callbacks(:before, kind) end end |
#run_callbacks(kind, with_children: true, &block) ⇒ Object
Run the callbacks for the document. This overrides active support’s functionality to cascade callbacks to embedded documents that have been flagged as such.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mongoid/interceptable.rb', line 119 def run_callbacks(kind, with_children: true, &block) if with_children cascadable_children(kind).each do |child| if child.run_callbacks(child_callback_type(kind, child), with_children: with_children) == false return false end end end if callback_executable?(kind) super(kind, &block) else true end end |
#run_pending_callbacks ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Run the pending callbacks. If the callback is :apply_defaults, we will apply the defaults for this document. Otherwise, the callback is passed to the run_callbacks function.
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/mongoid/interceptable.rb', line 177 def run_pending_callbacks pending_callbacks.each do |cb| if [:apply_defaults, :apply_post_processed_defaults].include?(cb) send(cb) else self.run_callbacks(cb, with_children: false) end end pending_callbacks.clear end |