ar_after_transaction 0.4.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b424a149e55b8cbc4975249856cae410c9618533
4
- data.tar.gz: c2c6cced324cdddab51ad689e64cb5722212fc64
2
+ SHA256:
3
+ metadata.gz: 260f2ee697d717537bd14d2fa30261d9270092be1efce392b10baea6c604f996
4
+ data.tar.gz: 96c6489521d2f4224d5a9544978c106f4ef698340900b20b60112fbda5465236
5
5
  SHA512:
6
- metadata.gz: 2395d07fa007b42f1f3ad6d241eba4cf013800925ae2acfa3e31a53597b57550264e16d1ccbb01b85b429576a78458cb0ed28077f4f1ca341340331b8cdffbc3
7
- data.tar.gz: ef988f3834fd49816f06903636de29f54fee94ce4b2587393aac2993201ca71b8f53611c540a49097daba2af701f20a0173b1f93fc745b04abaa2c26f0f3d5a2
6
+ metadata.gz: 1a03f6148f86af85473b2f709152234a07e38c04e22f366e79582880411e7455ede71a49f454870cbce1d1ce833f410d925fa29a19effc08e77ceec48c9cf539
7
+ data.tar.gz: 96f2a7bc147a02f58c8b10597bd559dc86574195f465f9a22db136685951fe6f65def1c273bfb759452c1f07339e67aaaf3f5b90b04f1f44a18fd23fa4fbe530
data/Readme.md CHANGED
@@ -1,6 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/ar_after_transaction.png)](http://badge.fury.io/rb/ar_after_transaction)
2
- [![Build Status](https://travis-ci.org/grosser/ar_after_transaction.png)](https://travis-ci.org/grosser/ar_after_transaction)
3
- [![Dependency Status](https://gemnasium.com/grosser/ar_after_transaction.png)](https://gemnasium.com/grosser/ar_after_transaction)
2
+ ![CI](https://github.com/grosser/ar_after_transaction/workflows/CI/badge.svg)
4
3
 
5
4
  Do something only after the currently open transactions have finished.
6
5
 
@@ -9,36 +8,44 @@ Normally everything gets rolled back when a transaction fails, but you cannot ro
9
8
  Install
10
9
  =======
11
10
 
12
- gem install ar_after_transaction
11
+ ```bash
12
+ gem install ar_after_transaction
13
+ ```
13
14
 
14
15
 
15
16
  Usage
16
17
  =====
18
+
17
19
  ### just-in-time callbacks
18
- class User
19
- after_create :do_stuff, :oops
20
-
21
- def do_stuff
22
- after_transaction do
23
- send_an_email # cannot be rolled back
24
- end
25
- comments.create(...) # will be rolled back
26
- end
27
-
28
- def oops
29
- raise "do the rolback!"
30
- end
20
+
21
+ ```ruby
22
+ class User
23
+ after_create :do_stuff, :oops
24
+
25
+ def do_stuff
26
+ after_transaction do
27
+ send_an_email # cannot be rolled back
31
28
  end
29
+ comments.create(...) # will be rolled back
30
+ end
31
+
32
+ def oops
33
+ raise "do the rolback!"
34
+ end
35
+ end
36
+ ```
32
37
 
33
38
  ### General 'this should be rolled back when in a transaction' code like jobs
34
39
 
35
- class Resque
36
- def revertable_enqueue(*args)
37
- ActiveRecord::Base.after_transaction do
38
- enqueue(*args)
39
- end
40
- end
40
+ ```ruby
41
+ class Resque
42
+ def revertable_enqueue(*args)
43
+ ActiveRecord::Base.after_transaction do
44
+ enqueue(*args)
41
45
  end
46
+ end
47
+ end
48
+ ```
42
49
 
43
50
  ### When not in a transaction
44
51
  after_transaction will perform the given block immediately
@@ -47,25 +54,32 @@ after_transaction will perform the given block immediately
47
54
  after_transaction assumes zero open transactions.<br/>
48
55
  If you use transactional fixtures you should change it in test mode.
49
56
 
50
- # config/environments/test.rb
51
- config.after_initialize do
52
- ActiveRecord::Base.normally_open_transactions = 1
53
- end
57
+ ```ruby
58
+ # config/environments/test.rb
59
+ config.after_initialize do
60
+ ActiveRecord::Base.normally_open_transactions = 1
61
+ end
62
+ ```
54
63
 
55
64
  ### Rails 3: after_commit hook can replace the first usage example:
56
65
 
57
- class User
58
- after_commit :send_an_email :on=>:create
59
- after_create :do_stuff, :oops
60
- ...
61
- end
66
+ ```ruby
67
+ class User
68
+ after_commit :send_an_email on: :create
69
+ after_create :do_stuff, :oops
70
+ ...
71
+ end
72
+ ```
62
73
 
63
74
  Alternative
64
75
  ===========
76
+
65
77
  Rails 3+
66
78
  - basic support is built in, use it if you can!
67
79
  - `after_commit :foo`
68
- - `after_commit :bar, :on => :create / :update`
80
+ - `after_commit :bar, on: :create / :update`
81
+ - [after_commit everywhere](https://dev.to/evilmartians/rails-aftercommit-everywhere--4j9g)
82
+
69
83
 
70
84
  [after_commit](https://github.com/pat/after_commit)<br/>
71
85
  - pro: threadsafe<br/>
@@ -83,6 +97,8 @@ Authors
83
97
  - [Tyler Rick](https://github.com/TylerRick)
84
98
  - [Michael Wu](https://github.com/michaelmwu)
85
99
  - [C.W.](https://github.com/compwron)
100
+ - [Ben Weintraub](https://github.com/benweint)
101
+ - [Vladimir Temnikov](https://github.com/vladimirtemnikov)
86
102
 
87
103
  [Michael Grosser](http://grosser.it)<br/>
88
104
  michael@grosser.it<br/>
@@ -1,72 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_record'
2
4
  require 'ar_after_transaction/version'
3
5
 
4
6
  module ARAfterTransaction
5
7
  module ClassMethods
6
- def self.extended( base )
7
- base.class_eval do
8
- class << self
9
- alias_method :transaction_without_after, :transaction
10
- alias_method :transaction, :transaction_with_after
11
- end
12
- end
13
- end
14
-
15
- def transaction_with_after(*args)
16
- clean = true
17
- transaction_without_after(*args) do
18
- begin
19
- yield
20
- rescue ActiveRecord::Rollback
21
- clean = false
22
- raise
23
- end
24
- end
25
- rescue Exception
26
- clean = false
27
- raise
28
- ensure
29
- unless transactions_open?
30
- callbacks = delete_after_transaction_callbacks
31
- callbacks.each(&:call) if clean
32
- end
33
- end
34
-
35
8
  def after_transaction(&block)
36
- if transactions_open?
37
- connection.after_transaction_callbacks ||= []
38
- connection.after_transaction_callbacks << block
39
- else
40
- yield
41
- end
9
+ connection.after_transaction(&block)
42
10
  end
43
11
 
44
12
  def normally_open_transactions
45
- @@normally_open_transactions ||= 0
13
+ connection.normally_open_transactions ||= 0
46
14
  end
47
15
 
48
- def normally_open_transactions=(value)
49
- @@normally_open_transactions = value
50
- end
16
+ delegate :normally_open_transactions=, to: :connection
51
17
 
52
18
  private
53
19
 
54
20
  def transactions_open?
55
- pool = connection_pool
56
- return false unless pool && pool.active_connection?
57
- connection.open_transactions > normally_open_transactions
58
- end
59
-
60
- def delete_after_transaction_callbacks
61
- result = connection.after_transaction_callbacks || []
62
- connection.after_transaction_callbacks = []
63
- result
21
+ connection.send :transactions_open?
64
22
  end
65
23
  end
66
24
 
67
25
  module InstanceMethods
68
26
  def after_transaction(&block)
69
- self.class.after_transaction(&block)
27
+ self.class.connection.after_transaction(&block)
70
28
  end
71
29
  end
72
30
  end
@@ -76,10 +34,57 @@ module ARAfterTransactionConnection
76
34
  base.class_eval do
77
35
  attr_accessor :normally_open_transactions
78
36
  attr_accessor :after_transaction_callbacks
37
+
38
+ alias_method :transaction_without_after, :transaction
39
+ alias_method :transaction, :transaction_with_after
79
40
  end
80
41
  end
42
+
43
+ def transaction_with_after(**args)
44
+ clean = true
45
+ transaction_without_after(**args) do
46
+ begin
47
+ yield
48
+ rescue ActiveRecord::Rollback
49
+ clean = false
50
+ raise
51
+ end
52
+ end
53
+ rescue StandardError
54
+ clean = false
55
+ raise
56
+ ensure
57
+ unless transactions_open?
58
+ callbacks = delete_after_transaction_callbacks
59
+ callbacks.each(&:call) if clean
60
+ end
61
+ end
62
+
63
+ def after_transaction(&block)
64
+ if transactions_open?
65
+ self.after_transaction_callbacks ||= []
66
+ self.after_transaction_callbacks << block
67
+ else
68
+ yield
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def transactions_open?
75
+ return false unless active?
76
+
77
+ self.normally_open_transactions ||= 0
78
+ open_transactions > normally_open_transactions
79
+ end
80
+
81
+ def delete_after_transaction_callbacks
82
+ result = after_transaction_callbacks || []
83
+ self.after_transaction_callbacks = []
84
+ result
85
+ end
81
86
  end
82
87
 
83
- ActiveRecord::Base.send(:extend, ARAfterTransaction::ClassMethods)
84
- ActiveRecord::Base.send(:include, ARAfterTransaction::InstanceMethods)
85
- ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, ARAfterTransactionConnection)
88
+ ActiveRecord::Base.extend ARAfterTransaction::ClassMethods
89
+ ActiveRecord::Base.include ARAfterTransaction::InstanceMethods
90
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.include ARAfterTransactionConnection
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ARAfterTransaction
2
- VERSION = Version = '0.4.1'
3
- end
3
+ VERSION = Version = '0.7.0'
4
+ end
metadata CHANGED
@@ -1,37 +1,37 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_after_transaction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.7.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: 2016-11-29 00:00:00.000000000 Z
11
+ date: 2020-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.2.0
19
+ version: 4.2.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5.0'
22
+ version: '6.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">"
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 3.2.0
29
+ version: 4.2.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5.0'
32
+ version: '6.2'
33
33
  - !ruby/object:Gem::Dependency
34
- name: wwtd
34
+ name: bump
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
@@ -45,7 +45,7 @@ dependencies:
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
47
  - !ruby/object:Gem::Dependency
48
- name: bump
48
+ name: rails
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">="
@@ -78,14 +78,14 @@ dependencies:
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: '2'
81
+ version: '3'
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: '2'
88
+ version: '3'
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: sqlite3
91
91
  requirement: !ruby/object:Gem::Requirement
@@ -101,7 +101,7 @@ dependencies:
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
103
  - !ruby/object:Gem::Dependency
104
- name: rails
104
+ name: wwtd
105
105
  requirement: !ruby/object:Gem::Requirement
106
106
  requirements:
107
107
  - - ">="
@@ -135,15 +135,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: '0'
138
+ version: 2.4.0
139
139
  required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  requirements:
141
141
  - - ">="
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  requirements: []
145
- rubyforge_project:
146
- rubygems_version: 2.4.5.1
145
+ rubygems_version: 3.1.3
147
146
  signing_key:
148
147
  specification_version: 4
149
148
  summary: Execute irreversible actions only when transactions are not rolled back