Skip to content

Commit

Permalink
Do not print warning for after_commit outside of transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Mar 29, 2018
1 parent d33ad98 commit 890a637
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/after_commit_everywhere.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def after_commit(connection: ActiveRecord::Base.connection, &callback)
connection: connection,
name: __callee__,
callback: callback,
no_tx_action: :warn,
no_tx_action: :execute,
)
end

Expand All @@ -41,7 +41,7 @@ def before_commit(connection: ActiveRecord::Base.connection, &callback)
connection: connection,
name: __callee__,
callback: callback,
no_tx_action: :warn,
no_tx_action: :warn_and_execute,
)
end

Expand Down Expand Up @@ -69,9 +69,11 @@ def register_callback(connection:, name:, no_tx_action:, callback:)
raise ArgumentError, "Provide callback to #{name}" unless callback
unless in_transaction?(connection)
case no_tx_action
when :warn
when :warn_and_execute
warn "#{name}: No transaction open. Executing callback immediately."
return callback.call
when :execute
return callback.call
when :exception
raise NotInTransaction, "#{name} is useless outside transaction"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/after_commit_everywhere/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module AfterCommitEverywhere
VERSION = '0.1.0'
VERSION = '0.1.1'
end
8 changes: 8 additions & 0 deletions spec/after_commit_everywhere_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
subject
expect(handler).to have_received(:call)
end

it "doesn't print any warnings as it is expected behaviour" do
expect { subject }.not_to output.to_stderr
end
end

context 'with nested transactions' do
Expand Down Expand Up @@ -138,6 +142,10 @@
subject
expect(handler).to have_received(:call)
end

it 'warns as it is unclear whether it is expected behaviour or not' do
expect { subject }.to output(anything).to_stderr
end
end

context 'with nested transactions' do
Expand Down

0 comments on commit 890a637

Please sign in to comment.