From 039231eb43a2cb3a290450bd1fffcaa43a8582eb Mon Sep 17 00:00:00 2001 From: Viktor Sheiko Date: Sat, 22 Apr 2017 17:29:01 +0300 Subject: [PATCH 1/2] Add inheritance for hooks --- lib/interactor/hooks.rb | 6 ++--- spec/interactor/hooks_spec.rb | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) 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..66b704b 100644 --- a/spec/interactor/hooks_spec.rb +++ b/spec/interactor/hooks_spec.rb @@ -353,6 +353,53 @@ 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 + 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 + 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 + end + end end end end From 6178392b0cd2cd3d8f3d10e4066c4c117553b7ce Mon Sep 17 00:00:00 2001 From: Viktor Sheiko Date: Fri, 15 Jun 2018 14:30:24 +0300 Subject: [PATCH 2/2] Added more specs for hook inheritance --- spec/interactor/hooks_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/interactor/hooks_spec.rb b/spec/interactor/hooks_spec.rb index 66b704b..df1b245 100644 --- a/spec/interactor/hooks_spec.rb +++ b/spec/interactor/hooks_spec.rb @@ -368,6 +368,11 @@ def add_after2 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 @@ -383,6 +388,11 @@ def add_after2 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 @@ -398,6 +408,11 @@ def add_after2 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