Skip to content

Commit

Permalink
Fix callback names when DSL methods are aliased (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
stokarenko committed Mar 22, 2020
1 parent d5f0112 commit 0d46662
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
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

0 comments on commit 0d46662

Please sign in to comment.