Skip to content

Commit 5028442

Browse files
Return value from transaction yield to caller instead of internal state (mongodb#6005)
Co-authored-by: Dmitry Rybakov <[email protected]>
1 parent 9bea977 commit 5028442

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

lib/mongoid/clients/sessions.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ def transaction(options = {}, session_options: {})
9292
begin
9393
session.with_transaction(options) do
9494
yield
95-
end
96-
run_commit_callbacks(session)
95+
end.tap { run_commit_callbacks(session) }
9796
rescue *transactions_not_supported_exceptions
9897
raise Mongoid::Errors::TransactionsNotSupported
9998
rescue Mongoid::Errors::Rollback

spec/mongoid/clients/transactions_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ def capture_exception
716716
require_transaction_support
717717

718718
context 'when no error raised' do
719-
before do
719+
let!(:person) do
720720
Mongoid.transaction do
721721
Person.create!
722722
end
@@ -727,6 +727,10 @@ def capture_exception
727727
expect(other_events.count { |e| e.command_name == 'commitTransaction'}).to be(1)
728728
end
729729

730+
it 'returns the value from the block' do
731+
expect(person).to be_a(Person)
732+
end
733+
730734
it 'executes the commands inside the transaction' do
731735
expect(Person.count).to be(1)
732736
end

0 commit comments

Comments
 (0)