File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -94,12 +94,14 @@ def after_rollback(&block)
94
94
@internal_transaction &.after_rollback ( &block )
95
95
end
96
96
97
+ # Returns true if the transaction exists and isn't finalized yet
97
98
def open?
98
- @internal_transaction &. open ?
99
+ ! closed ?
99
100
end
100
101
102
+ # Returns true if the transaction doesn't exists or is finalized (committed or rolled back)
101
103
def closed?
102
- ! open ?
104
+ @internal_transaction . nil? || @internal_transaction . state . finalized ?
103
105
end
104
106
105
107
alias_method :blank? , :closed?
Original file line number Diff line number Diff line change 12
12
require "models/cpk"
13
13
14
14
module TransactionCallbacksTests
15
+ SomeError = Class . new ( StandardError )
16
+
17
+ def test_transaction_open?
18
+ assert_predicate Topic . current_transaction , :closed?
19
+
20
+ committed_transaction = nil
21
+ Topic . transaction do
22
+ assert_predicate Topic . current_transaction , :open?
23
+ committed_transaction = Topic . current_transaction
24
+ end
25
+ assert_predicate committed_transaction , :closed?
26
+
27
+ rolledback_transaction = nil
28
+ assert_raises SomeError do
29
+ Topic . transaction do
30
+ assert_predicate Topic . current_transaction , :open?
31
+ rolledback_transaction = Topic . current_transaction
32
+ raise SomeError
33
+ end
34
+ end
35
+ assert_predicate rolledback_transaction , :closed?
36
+ end
37
+
15
38
def test_after_all_transactions_commit
16
39
called = 0
17
40
ActiveRecord . after_all_transactions_commit { called += 1 }
You can’t perform that action at this time.
0 commit comments