Module: TestAfterCommit::DatabaseStatements
- Defined in:
- lib/test_after_commit/database_statements.rb
Instance Method Summary collapse
Instance Method Details
#test_commit_records ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/test_after_commit/database_statements.rb', line 28 def test_commit_records if ActiveRecord::VERSION::MAJOR == 3 commit_transaction_records else # To avoid an infinite loop, we need to copy the transaction locally, and clear out # `records` on the copy that stays in the AR stack. Otherwise new # transactions inside a commit callback will cause an infinite loop. # # This is because we're re-using the transaction on the stack, before # it's been popped off and re-created by the AR code. original = @transaction || @transaction_manager.current_transaction transaction = original.dup transaction.instance_variable_set(:@records, transaction.records.dup) # deep clone of records array original.records.clear # so that this clear doesn't clear out both copies transaction.commit_records end end |
#transaction ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/test_after_commit/database_statements.rb', line 2 def transaction(*) @test_open_transactions ||= 0 super do begin @test_open_transactions += 1 if ActiveRecord::VERSION::MAJOR == 3 @_current_transaction_records.push([]) if @_current_transaction_records.empty? end result = yield rescue Exception rolled_back = true raise ensure begin @test_open_transactions -= 1 if TestAfterCommit.enabled && @test_open_transactions == 0 && !rolled_back test_commit_records end ensure result end end end end |