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

Add inheritance for hooks #138

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 deletions lib/interactor/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
62 changes: 62 additions & 0 deletions spec/interactor/hooks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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