after_commit_everywhere 1.3.1 → 1.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
  SHA256:
3
- metadata.gz: 6b63aab224d26cf40ba71632ebc055bd458bd3c72c87234715d1d8020f661247
4
- data.tar.gz: b558770a87644ee346a8361419ea9d0e616062da4873427d260dc8e428d545b4
3
+ metadata.gz: 749225d1d1a6000f08b986f6c2704767339fc576648811f4421fb7344b864761
4
+ data.tar.gz: af075362eeaa19f6e893d5fcefadd751f8cf7817bd310ddea0a024e49482100e
5
5
  SHA512:
6
- metadata.gz: 99d19cdc3ba272ad5a7c6a47b232d2cd6d5fa012889cd270763d154cf559d2b504b34ba650917a8618f073897a46e4e4fec79293092d05e66aa435d21179037a
7
- data.tar.gz: 7169e814084435d36a231cc5b2a3c7b11268a3e24913826b00fefe87ceaddea2c2299208616938d51ee245eca60136a654f8ec02664e30c3466f59b43f82f62d
6
+ metadata.gz: c816318efea508b86763b07f69b6164eab40edb52f5c95953e05c6dd7df1b2432b1a933e0bfa68605b7b67c3e99dbee6a3e7c4c30389214c7ed9bab5d6afa704
7
+ data.tar.gz: 4b20e061dfbd60ca6caef4da43ba1866c92a4c3be3418016da1f1f768f01321c99d19f024020d48139dd4320b0c0d21854554e15ee67bf0e9a6a1f3b072e21ad
data/CHANGELOG.md CHANGED
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 1.4.0 (2024-02-07)
8
+
9
+ ### Added
10
+
11
+ - Ability to prepend callbacks to the head of callback queue using `prepend: true` option.
12
+
13
+ ```ruby
14
+ AfterCommitEverywhere.after_commit { puts "I'm second!" }
15
+ AfterCommitEverywhere.after_commit(prepend: true) { puts "I'm first!" }
16
+ ```
17
+
18
+ See [Pull request #30](https://github.com/Envek/after_commit_everywhere/pull/30) by [@quentindemetz][] and [@A1090][].
19
+
7
20
  ## 1.3.1 (2023-06-21)
8
21
 
9
22
  ### Fixed
@@ -132,3 +145,5 @@ See [#11](https://github.com/Envek/after_commit_everywhere/issues/11) for discus
132
145
  [@stokarenko]: https://github.com/stokarenko "Sergey Tokarenko"
133
146
  [@lolripgg]: https://github.com/lolripgg "James Brewer"
134
147
  [@jpcamara]: https://github.com/jpcamara "JP Camara"
148
+ [@quentindemetz]: https://github.com/quentindemetz "Quentin de Metz"
149
+ [@A1090]: https://github.com/A1090 "Tabac Andreina"
data/README.md CHANGED
@@ -111,7 +111,7 @@ Will be executed right after transaction in which it have been declared was roll
111
111
 
112
112
  If called outside transaction will raise an exception!
113
113
 
114
- Please keep in mind ActiveRecord's [limitations for rolling back nested transactions](http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#module-ActiveRecord::Transactions::ClassMethods-label-Nested+transactions). See [`in_transaction`](#in_transaction) for a workaround to this limitation.
114
+ Please keep in mind ActiveRecord's [limitations for rolling back nested transactions](http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#module-ActiveRecord::Transactions::ClassMethods-label-Nested+transactions). See [`in_transaction`](#in_transaction) for a workaround to this limitation.
115
115
 
116
116
  ### Available helper methods
117
117
 
@@ -139,7 +139,7 @@ class ServiceObjectBtw
139
139
  end
140
140
  ```
141
141
 
142
- Our service object can run its database operations safely when run in isolation.
142
+ Our service object can run its database operations safely when run in isolation.
143
143
 
144
144
  ```rb
145
145
  ServiceObjectBtw.new.call # This opens a new #transaction block
@@ -195,6 +195,8 @@ end
195
195
  - `:warn_and_execute` to print warning and execute immediately
196
196
  - `:raise` to raise an exception instead of executing
197
197
 
198
+ - `prepend` puts the callback at the head of the callback chain, instead of at the end.
199
+
198
200
  ### FAQ
199
201
 
200
202
  #### Does it works with transactional_test or DatabaseCleaner
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AfterCommitEverywhere
4
- VERSION = "1.3.1"
4
+ VERSION = "1.4.0"
5
5
  end
@@ -39,11 +39,13 @@ module AfterCommitEverywhere
39
39
  # @param callback [#call] Callback to be executed
40
40
  # @return void
41
41
  def after_commit(
42
+ prepend: false,
42
43
  connection: nil,
43
44
  without_tx: EXECUTE,
44
45
  &callback
45
46
  )
46
47
  register_callback(
48
+ prepend: prepend,
47
49
  connection: connection,
48
50
  name: __method__,
49
51
  callback: callback,
@@ -64,6 +66,7 @@ module AfterCommitEverywhere
64
66
  # @param callback [#call] Callback to be executed
65
67
  # @return void
66
68
  def before_commit(
69
+ prepend: false,
67
70
  connection: nil,
68
71
  without_tx: WARN_AND_EXECUTE,
69
72
  &callback
@@ -73,6 +76,7 @@ module AfterCommitEverywhere
73
76
  end
74
77
 
75
78
  register_callback(
79
+ prepend: prepend,
76
80
  connection: connection,
77
81
  name: __method__,
78
82
  callback: callback,
@@ -90,8 +94,9 @@ module AfterCommitEverywhere
90
94
  # @param callback [#call] Callback to be executed
91
95
  # @return void
92
96
  # @raise [NotInTransaction] if called outside transaction.
93
- def after_rollback(connection: nil, &callback)
97
+ def after_rollback(prepend: false, connection: nil, &callback)
94
98
  register_callback(
99
+ prepend: prepend,
95
100
  connection: connection,
96
101
  name: __method__,
97
102
  callback: callback,
@@ -100,7 +105,7 @@ module AfterCommitEverywhere
100
105
  end
101
106
 
102
107
  # @api private
103
- def register_callback(connection: nil, name:, without_tx:, callback:)
108
+ def register_callback(prepend:, connection: nil, name:, without_tx:, callback:)
104
109
  raise ArgumentError, "Provide callback to #{name}" unless callback
105
110
 
106
111
  unless in_transaction?(connection)
@@ -119,7 +124,16 @@ module AfterCommitEverywhere
119
124
 
120
125
  connection ||= default_connection
121
126
  wrap = Wrap.new(connection: connection, "#{name}": callback)
122
- connection.add_transaction_record(wrap)
127
+
128
+ if prepend
129
+ # Hacking ActiveRecord's transaction internals to prepend our callback
130
+ # See https://github.com/rails/rails/blob/f0d433bb46ac233ec7fd7fae48f458978908d905/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb#L148-L156
131
+ records = connection.current_transaction.instance_variable_get(:@records)
132
+ records = connection.current_transaction.instance_variable_set(:@records, []) if records.nil?
133
+ records.unshift(wrap)
134
+ else
135
+ connection.add_transaction_record(wrap)
136
+ end
123
137
  end
124
138
 
125
139
  # Helper method to determine whether we're currently in transaction or not
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: after_commit_everywhere
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Novikov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-21 00:00:00.000000000 Z
11
+ date: 2024-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -246,7 +246,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
246
246
  - !ruby/object:Gem::Version
247
247
  version: '0'
248
248
  requirements: []
249
- rubygems_version: 3.1.6
249
+ rubygems_version: 3.5.3
250
250
  signing_key:
251
251
  specification_version: 4
252
252
  summary: Executes code after database commit wherever you want in your application