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-5836 Don't repeat callbacks for embedded grandchildren. #5933

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/mongoid/interceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,6 @@ def _mongoid_run_child_before_callbacks(kind, children: [], callback_list: [])
return false if env.halted
env.value = !env.halted
callback_list << [next_sequence, env]
if (grandchildren = child.send(:cascadable_children, kind))
_mongoid_run_child_before_callbacks(kind, children: grandchildren, callback_list: callback_list)
end
end
callback_list
end
Expand Down
19 changes: 19 additions & 0 deletions spec/mongoid/interceptable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,25 @@ class TestClass
end
end
end

context 'with embedded grandchildren' do
DCFCG = DuplicateCallbacksForCascadableGrandchildren

let(:parent) do
parent = DCFCG::Parent.new
child = DCFCG::Child.new
grandchild = DCFCG::Child.new

child.children = [ grandchild ]
parent.children = [ child ]

parent.tap(&:save)
end

it 'should not repeat any before callbacks' do
expect { parent }.not_to raise_exception(DCFCG::DuplicateCallbackDetected)
end
end
end

describe ".before_destroy" do
Expand Down
27 changes: 27 additions & 0 deletions spec/mongoid/interceptable_spec_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,30 @@ def break_mongoid
end
end

module DuplicateCallbacksForCascadableGrandchildren
class DuplicateCallbackDetected < RuntimeError
end

class Parent
include Mongoid::Document

embeds_many :children, cascade_callbacks: true, as: :parent
end

class Child
include Mongoid::Document

embedded_in :parent, polymorphic: true
embeds_many :children, cascade_callbacks: true, as: :parent

before_save :check_for_duplicate_callback

private

def check_for_duplicate_callback
raise DuplicateCallbackDetected if @callback_invoked
@callback_invoked = true
end
end
end

Loading