-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MONGOID-3795 Clarify/improve order of callback invocation on embedded…
… associations vs host documents
- Loading branch information
Showing
3 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module InterceptableSpec | ||
class CallbackRegistry | ||
def initialize | ||
@calls = [] | ||
end | ||
|
||
def record_call(cls, cb) | ||
@calls << [cls, cb] | ||
end | ||
|
||
attr_reader :calls | ||
end | ||
|
||
module CallbackTracking | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
%i( | ||
validation save create update | ||
).each do |what| | ||
%i(before after).each do |whn| | ||
send("#{whn}_#{what}", "#{whn}_#{what}_stub".to_sym) | ||
define_method("#{whn}_#{what}_stub") do | ||
callback_registry.record_call(self.class, "#{whn}_#{what}".to_sym) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
class CbParent | ||
include Mongoid::Document | ||
|
||
def initialize(callback_registry) | ||
@callback_registry = callback_registry | ||
super() | ||
end | ||
|
||
attr_reader :callback_registry | ||
|
||
embeds_many :cb_children | ||
embeds_many :cb_cascaded_children, cascade_callbacks: true | ||
|
||
include CallbackTracking | ||
end | ||
|
||
class CbChild | ||
include Mongoid::Document | ||
|
||
embedded_in :cb_parent | ||
|
||
def initialize(callback_registry, options) | ||
@callback_registry = callback_registry | ||
super(options) | ||
end | ||
|
||
attr_reader :callback_registry | ||
|
||
include CallbackTracking | ||
end | ||
|
||
class CbCascadedChild | ||
include Mongoid::Document | ||
|
||
embedded_in :cb_parent | ||
|
||
def initialize(callback_registry, options) | ||
@callback_registry = callback_registry | ||
super(options) | ||
end | ||
|
||
attr_reader :callback_registry | ||
|
||
include CallbackTracking | ||
end | ||
end |