test_after_commit 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56c217f295482402f1b0b61450c5688abbe5cdbb
4
- data.tar.gz: 2089d55aa79c4110eafe4ce88d88b893bea0875a
3
+ metadata.gz: 8823861468a75a7bca2bb53477565b321df0b4d6
4
+ data.tar.gz: ca3d17b225ca68cb1f27c15b98b3af6263fc79ef
5
5
  SHA512:
6
- metadata.gz: 4647ddfa475ad62303dafa1a0d589ff454ce0c4db4188607fba5937b412775acfdae06167257cca2431556474715cedc22e62ce0a2b59272c662189a30bcd03f
7
- data.tar.gz: 13b4ce6d63d923ce30c050c01ed43524aaf8685bfd95ce093af8b2ef669874948b28c54bc72dd060a121824b008df053efa76f03d199bde3faa0f7daf825b3f2
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/>
@@ -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
- if @test_open_transactions == 1 && !rolled_back
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(false)
35
+ commit_transaction_records
33
36
  else
34
- transaction = @transaction || @transaction_manager.current_transaction
35
- begin
36
- transaction.commit_records
37
- ensure
38
- transaction.records.clear # prevent duplicate .commit!
39
- transaction.instance_variable_get(:@state).set_state(nil)
40
- end
41
- end
42
- end
43
-
44
- if ActiveRecord::VERSION::MAJOR == 3
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
@@ -1,3 +1,3 @@
1
1
  module TestAfterCommit
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  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.3.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-09-13 00:00:00.000000000 Z
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord