Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-3795 Add a test of callback invocation order for cascading and non-cascading callbacks #4566

Merged
merged 2 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/tutorials/mongoid-relations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,9 @@ that a bundle may contain other bundles or products:
Cascading Callbacks
*******************

If you want the embedded document callbacks to fire when calling a persistence operation on
its parent, you will need to provide the cascade callbacks option to the association.
If you want the embedded document callbacks to fire when calling a persistence
operation on its parent, you will need to provide the cascade callbacks option
to the association.

.. code-block:: ruby

Expand Down
6 changes: 5 additions & 1 deletion lib/mongoid/interceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ def run_callbacks(kind, *args, &block)
return false
end
end
callback_executable?(kind) ? super(kind, *args, &block) : true
if callback_executable?(kind)
super(kind, *args, &block)
else
true
end
end

private
Expand Down
62 changes: 62 additions & 0 deletions spec/mongoid/interceptable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# encoding: utf-8

require "spec_helper"
require_relative './interceptable_spec_models'

describe Mongoid::Interceptable do

Expand Down Expand Up @@ -1722,4 +1723,65 @@ class TestClass
end
end
end

context 'when creating a parent and embedded child' do
let(:registry) { InterceptableSpec::CallbackRegistry.new }
let(:parent) do
InterceptableSpec::CbParent.new(registry).tap do |parent|
parent.cb_children << InterceptableSpec::CbChild.new(registry, cb_parent: parent)
end
end

let(:expected) do
[
[InterceptableSpec::CbParent, :before_validation],
[InterceptableSpec::CbChild, :before_validation],
[InterceptableSpec::CbChild, :after_validation],
[InterceptableSpec::CbParent, :after_validation],
[InterceptableSpec::CbParent, :before_save],
[InterceptableSpec::CbParent, :before_create],
[InterceptableSpec::CbParent, :after_create],
[InterceptableSpec::CbParent, :after_save],
]
end

it 'calls callbacks in the right order' do
parent.save!
expect(registry.calls).to eq expected
end
end

context 'when creating a parent and embedded child with cascading callbacks' do
let(:registry) { InterceptableSpec::CallbackRegistry.new }
let(:parent) do
InterceptableSpec::CbParent.new(registry).tap do |parent|
parent.cb_cascaded_children <<
InterceptableSpec::CbCascadedChild.new(registry, cb_parent: parent)
end
end

let(:expected) do
[
[InterceptableSpec::CbParent, :before_validation],
[InterceptableSpec::CbCascadedChild, :before_validation],
[InterceptableSpec::CbCascadedChild, :after_validation],
[InterceptableSpec::CbParent, :after_validation],
[InterceptableSpec::CbParent, :before_save],
[InterceptableSpec::CbCascadedChild, :before_save],
[InterceptableSpec::CbParent, :before_create],
[InterceptableSpec::CbCascadedChild, :before_create],
[InterceptableSpec::CbParent, :after_create],
[InterceptableSpec::CbCascadedChild, :after_create],
[InterceptableSpec::CbParent, :after_save],
[InterceptableSpec::CbCascadedChild, :after_save],
]
end

it 'calls callbacks in the right order' do
pending 'MONGOID-3795'

parent.save!
expect(registry.calls).to eq expected
end
end
end
76 changes: 76 additions & 0 deletions spec/mongoid/interceptable_spec_models.rb
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