diff --git a/lib/interactor/hooks.rb b/lib/interactor/hooks.rb index d82e7a5..71698a7 100644 --- a/lib/interactor/hooks.rb +++ b/lib/interactor/hooks.rb @@ -143,7 +143,7 @@ def after(*hooks, &block) # # Returns an Array of Symbols and Procs. def around_hooks - @around_hooks ||= [] + @around_hooks ||= superclass && superclass.respond_to?(:around_hooks) ? superclass.around_hooks.dup : [] end # Internal: An Array of declared hooks to run before Interactor @@ -162,7 +162,7 @@ def around_hooks # # Returns an Array of Symbols and Procs. def before_hooks - @before_hooks ||= [] + @before_hooks ||= superclass && superclass.respond_to?(:before_hooks) ? superclass.before_hooks.dup : [] end # Internal: An Array of declared hooks to run before Interactor @@ -181,7 +181,7 @@ def before_hooks # # Returns an Array of Symbols and Procs. def after_hooks - @after_hooks ||= [] + @after_hooks ||= superclass && superclass.respond_to?(:after_hooks) ? superclass.after_hooks.dup : [] end end diff --git a/spec/interactor/hooks_spec.rb b/spec/interactor/hooks_spec.rb index 7e40f63..df1b245 100644 --- a/spec/interactor/hooks_spec.rb +++ b/spec/interactor/hooks_spec.rb @@ -353,6 +353,68 @@ def add_after2 ]) end end + + context "inheritance" do + context "around_hooks" do + let(:hooked) { + build_hooked do + around :add_around_before_and_around_after + around { |hooked| hooked.call } + end + } + + let(:inherited) { Class.new(hooked) } + + it "inherites around hooks from parent class" do + expect(inherited.around_hooks).to eq(hooked.around_hooks) + end + + it "does not add hook to parent class" do + inherited.class_eval { around { |hooked| hooked.call } } + expect(inherited.around_hooks.size).not_to eq(hooked.around_hooks.size) + end + end + + context "before_hooks" do + let(:hooked) { + build_hooked do + before :add_before + before {} + end + } + + let(:inherited) { Class.new(hooked) } + + it "inherites before hooks from parent class" do + expect(inherited.before_hooks).to eq(hooked.before_hooks) + end + + it "does not add hook to parent class" do + inherited.class_eval { before {} } + expect(inherited.before_hooks).not_to eq(hooked.before_hooks) + end + end + + context "after_hooks" do + let(:hooked) { + build_hooked do + after :add_after + after {} + end + } + + let(:inherited) { Class.new(hooked) } + + it "inherites after hooks from parent class" do + expect(inherited.after_hooks).to eq(hooked.after_hooks) + end + + it "does not add hook to parent class" do + inherited.class_eval { after {} } + expect(inherited.after_hooks).not_to eq(hooked.after_hooks) + end + end + end end end end