Skip to content

Commit 6085fab

Browse files
committed
1 parent 0ff2d2f commit 6085fab

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

lib/interactor/hooks.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def after(*hooks, &block)
143143
#
144144
# Returns an Array of Symbols and Procs.
145145
def around_hooks
146-
@around_hooks ||= []
146+
@around_hooks ||= superclass && superclass.respond_to?(:around_hooks) ? superclass.around_hooks.dup : []
147147
end
148148

149149
# Internal: An Array of declared hooks to run before Interactor
@@ -162,7 +162,7 @@ def around_hooks
162162
#
163163
# Returns an Array of Symbols and Procs.
164164
def before_hooks
165-
@before_hooks ||= []
165+
@before_hooks ||= superclass && superclass.respond_to?(:before_hooks) ? superclass.before_hooks.dup : []
166166
end
167167

168168
# Internal: An Array of declared hooks to run before Interactor
@@ -181,7 +181,7 @@ def before_hooks
181181
#
182182
# Returns an Array of Symbols and Procs.
183183
def after_hooks
184-
@after_hooks ||= []
184+
@after_hooks ||= superclass && superclass.respond_to?(:after_hooks) ? superclass.after_hooks.dup : []
185185
end
186186
end
187187

@@ -263,4 +263,4 @@ def run_hook(hook, *args)
263263
hook.is_a?(Symbol) ? send(hook, *args) : instance_exec(*args, &hook)
264264
end
265265
end
266-
end
266+
end

spec/interactor/hooks_spec.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,53 @@ def add_after2
353353
])
354354
end
355355
end
356+
357+
context "inheritance" do
358+
context "around_hooks" do
359+
let(:hooked) {
360+
build_hooked do
361+
around :add_around_before_and_around_after
362+
around { |hooked| hooked.call }
363+
end
364+
}
365+
366+
let(:inherited) { Class.new(hooked) }
367+
368+
it "inherites around hooks from parent class" do
369+
expect(inherited.around_hooks).to eq(hooked.around_hooks)
370+
end
371+
end
372+
373+
context "before_hooks" do
374+
let(:hooked) {
375+
build_hooked do
376+
before :add_before
377+
before {}
378+
end
379+
}
380+
381+
let(:inherited) { Class.new(hooked) }
382+
383+
it "inherites before hooks from parent class" do
384+
expect(inherited.before_hooks).to eq(hooked.before_hooks)
385+
end
386+
end
387+
388+
context "after_hooks" do
389+
let(:hooked) {
390+
build_hooked do
391+
after :add_after
392+
after {}
393+
end
394+
}
395+
396+
let(:inherited) { Class.new(hooked) }
397+
398+
it "inherites after hooks from parent class" do
399+
expect(inherited.after_hooks).to eq(hooked.after_hooks)
400+
end
401+
end
402+
end
356403
end
357404
end
358-
end
405+
end

0 commit comments

Comments
 (0)