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

Fix callback names for aliased DSL methods #8

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/after_commit_everywhere.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NotInTransaction < RuntimeError; end
def after_commit(connection: ActiveRecord::Base.connection, &callback)
AfterCommitEverywhere.register_callback(
connection: connection,
name: __callee__,
name: __method__,
callback: callback,
no_tx_action: :execute,
)
Expand All @@ -40,12 +40,12 @@ def after_commit(connection: ActiveRecord::Base.connection, &callback)
# @return void
def before_commit(connection: ActiveRecord::Base.connection, &callback)
if ActiveRecord::VERSION::MAJOR < 5
raise NotImplementedError, "#{__callee__} works only with Rails 5.0+"
raise NotImplementedError, "#{__method__} works only with Rails 5.0+"
end

AfterCommitEverywhere.register_callback(
connection: connection,
name: __callee__,
name: __method__,
callback: callback,
no_tx_action: :warn_and_execute,
)
Expand All @@ -64,7 +64,7 @@ def before_commit(connection: ActiveRecord::Base.connection, &callback)
def after_rollback(connection: ActiveRecord::Base.connection, &callback)
AfterCommitEverywhere.register_callback(
connection: connection,
name: __callee__,
name: __method__,
callback: callback,
no_tx_action: :exception,
)
Expand Down
24 changes: 23 additions & 1 deletion spec/after_commit_everywhere_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
expect(AfterCommitEverywhere::VERSION).not_to be nil
end

let(:example_class) { Class.new.include(described_class) }
let(:example_class) do
Class.new do
include ::AfterCommitEverywhere
alias_method :aliased_after_commit, :after_commit
end
end
let(:handler) { spy("handler") }

describe "#after_commit" do
Expand All @@ -38,6 +43,23 @@
end
expect(handler).not_to have_received(:call)
end

context 'aliased DSL method' do
subject do
example_class.new.aliased_after_commit do
handler.call
expect(ActiveRecord::Base.connection.transaction_open?).to be_falsey
end
end

it "executes code only after commit" do
ActiveRecord::Base.transaction do
subject
expect(handler).not_to have_received(:call)
end
expect(handler).to have_received(:call)
end
end
end

context "without transaction" do
Expand Down