test_after_commit 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Readme.md +2 -0
- data/lib/test_after_commit.rb +17 -39
- data/lib/test_after_commit/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8823861468a75a7bca2bb53477565b321df0b4d6
|
4
|
+
data.tar.gz: ca3d17b225ca68cb1f27c15b98b3af6263fc79ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ff5bd019f7c9dac53ea5e3548fe2a53cdb7fc20e22c026259dadb5b2a14cbdace0249a51f8860bd6a053bd048e9e3ba48c9b8942c941444acfe048502593bcd
|
7
|
+
data.tar.gz: abff3757960d644d0bbb414c29012e749d84dd5e01ca5d7fe8738755600de6a8fa19afe3428fd0d1f17dc414965206e3efd59e9608ac092b7fda5ea1f4101315
|
data/Readme.md
CHANGED
@@ -45,6 +45,8 @@ Inspired by https://gist.github.com/1305285
|
|
45
45
|
- [Brad Gessler](https://github.com/bradgessler)
|
46
46
|
- [Rohan McGovern](https://github.com/rohanpm)
|
47
47
|
- [lsylvester](https://github.com/lsylvester)
|
48
|
+
- [Tony Novak](https://github.com/afn)
|
49
|
+
- [Brian Palmer](https://github.com/codekitchen)
|
48
50
|
|
49
51
|
[Michael Grosser](http://grosser.it)<br/>
|
50
52
|
michael@grosser.it<br/>
|
data/lib/test_after_commit.rb
CHANGED
@@ -9,17 +9,20 @@ ActiveRecord::ConnectionAdapters::DatabaseStatements.class_eval do
|
|
9
9
|
transaction_without_transactional_fixtures(*args) do
|
10
10
|
begin
|
11
11
|
@test_open_transactions += 1
|
12
|
+
if ActiveRecord::VERSION::MAJOR == 3
|
13
|
+
@_current_transaction_records.push([]) if @_current_transaction_records.empty?
|
14
|
+
end
|
12
15
|
result = yield
|
13
16
|
rescue Exception => e
|
14
17
|
rolled_back = true
|
15
18
|
raise e
|
16
19
|
ensure
|
17
20
|
begin
|
18
|
-
|
21
|
+
@test_open_transactions -= 1
|
22
|
+
if @test_open_transactions == 0 && !rolled_back
|
19
23
|
test_commit_records
|
20
24
|
end
|
21
25
|
ensure
|
22
|
-
@test_open_transactions -= 1
|
23
26
|
result
|
24
27
|
end
|
25
28
|
end
|
@@ -29,44 +32,19 @@ ActiveRecord::ConnectionAdapters::DatabaseStatements.class_eval do
|
|
29
32
|
|
30
33
|
def test_commit_records
|
31
34
|
if ActiveRecord::VERSION::MAJOR == 3
|
32
|
-
commit_transaction_records
|
35
|
+
commit_transaction_records
|
33
36
|
else
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
# The @_current_transaction_records is a stack of arrays, each one
|
46
|
-
# containing the records associated with the corresponding transaction
|
47
|
-
# in the transaction stack. This is used by the
|
48
|
-
# `rollback_transaction_records` method (to only send a rollback hook to
|
49
|
-
# models attached to the transaction being rolled back) but is usually
|
50
|
-
# ignored by the `commit_transaction_records` method. Here we
|
51
|
-
# monkey-patch it to temporarily replace the array with only the records
|
52
|
-
# for the top-of-stack transaction, so the real
|
53
|
-
# `commit_transaction_records` method only sends callbacks to those.
|
54
|
-
#
|
55
|
-
def commit_transaction_records_with_transactional_fixtures(commit = true)
|
56
|
-
return commit_transaction_records_without_transactional_fixtures if commit
|
57
|
-
|
58
|
-
preserving_current_transaction_records do
|
59
|
-
@_current_transaction_records = @_current_transaction_records.pop || []
|
60
|
-
commit_transaction_records_without_transactional_fixtures
|
61
|
-
end
|
62
|
-
end
|
63
|
-
alias_method_chain :commit_transaction_records, :transactional_fixtures
|
64
|
-
|
65
|
-
def preserving_current_transaction_records
|
66
|
-
old_current_transaction_records = @_current_transaction_records.dup
|
67
|
-
yield
|
68
|
-
ensure
|
69
|
-
@_current_transaction_records = old_current_transaction_records
|
37
|
+
# To avoid an infinite loop, we need to copy the transaction locally, and clear out
|
38
|
+
# `records` on the copy that stays in the AR stack. Otherwise new
|
39
|
+
# transactions inside a commit callback will cause an infinite loop.
|
40
|
+
#
|
41
|
+
# This is because we're re-using the transaction on the stack, before
|
42
|
+
# it's been popped off and re-created by the AR code.
|
43
|
+
original = @transaction || @transaction_manager.current_transaction
|
44
|
+
transaction = original.dup
|
45
|
+
transaction.instance_variable_set(:@records, transaction.records.dup) # deep clone of records array
|
46
|
+
original.records.clear # so that this clear doesn't clear out both copies
|
47
|
+
transaction.commit_records
|
70
48
|
end
|
71
49
|
end
|
72
50
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_after_commit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Grosser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|