Skip to content

Commit 81d9922

Browse files
authored
Propagatable exception after committed DB transaction (#35)
See discussion at #34 for details
1 parent fbb1e36 commit 81d9922

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

CHANGELOG.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## 1.5.0 (2024-12-09)
8+
9+
### Added
10+
11+
- Ability to propagate exception for multiple `after_commit` callbacks within transaction. Should handle exception inside callback to avoid stopping other callbacks.
12+
13+
[Pull request #35](https://github.com/Envek/after_commit_everywhere/pull/35) by [@kevink1103](). Also see discussion at [#34](https://github.com/Envek/after_commit_everywhere/issues/34).
14+
715
## 1.4.0 (2024-02-07)
816

917
### Added
1018

11-
- Ability to prepend callbacks to the head of callback queue using `prepend: true` option.
19+
- Ability to prepend callbacks to the head of callback queue using `prepend: true` option.
1220

13-
```ruby
14-
AfterCommitEverywhere.after_commit { puts "I'm second!" }
15-
AfterCommitEverywhere.after_commit(prepend: true) { puts "I'm first!" }
16-
```
21+
```ruby
22+
AfterCommitEverywhere.after_commit { puts "I'm second!" }
23+
AfterCommitEverywhere.after_commit(prepend: true) { puts "I'm first!" }
24+
```
1725

18-
See [Pull request #30](https://github.com/Envek/after_commit_everywhere/pull/30) by [@quentindemetz][] and [@A1090][].
26+
See [Pull request #30](https://github.com/Envek/after_commit_everywhere/pull/30) by [@quentindemetz][] and [@A1090][].
1927

2028
## 1.3.1 (2023-06-21)
2129

@@ -147,3 +155,4 @@ See [#11](https://github.com/Envek/after_commit_everywhere/issues/11) for discus
147155
[@jpcamara]: https://github.com/jpcamara "JP Camara"
148156
[@quentindemetz]: https://github.com/quentindemetz "Quentin de Metz"
149157
[@A1090]: https://github.com/A1090 "Tabac Andreina"
158+
[@kevink1103]: https://github.com/kevink1103 "Kevin (bum)"

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
after_commit_everywhere (1.4.0)
4+
after_commit_everywhere (1.5.0)
55
activerecord (>= 4.2)
66
activesupport
77

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module AfterCommitEverywhere
4-
VERSION = "1.4.0"
4+
VERSION = "1.5.0"
55
end

lib/after_commit_everywhere/wrap.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def trigger_transactional_callbacks?
2323
true
2424
end
2525

26-
def committed!(*)
27-
@handlers[:after_commit]&.call
26+
def committed!(should_run_callbacks: true)
27+
if should_run_callbacks
28+
@handlers[:after_commit]&.call
29+
end
2830
end
2931

3032
def rolledback!(*)

spec/after_commit_everywhere_spec.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
end
2929

3030
context "within transaction" do
31-
context 'when prepend is true' do
32-
let(:handler_1) { spy("handler_1") }
33-
let(:handler_2) { spy("handler_2") }
31+
let(:handler_1) { spy("handler_1") }
32+
let(:handler_2) { spy("handler_2") }
3433

34+
context 'when prepend is true' do
3535
it 'executes prepended callback first' do
3636
ActiveRecord::Base.transaction do
3737
example_class.new.after_commit { handler_1.call }
@@ -54,9 +54,6 @@
5454
end
5555

5656
context 'when prepend is not specified' do
57-
let(:handler_1) { spy("handler_1") }
58-
let(:handler_2) { spy("handler_2") }
59-
6057
it 'executes callbacks in the order they were defined' do
6158
ActiveRecord::Base.transaction do
6259
example_class.new.after_commit { handler_1.call }
@@ -99,6 +96,18 @@
9996
expect(handler).to have_received(:call)
10097
end
10198
end
99+
100+
it 'propagates an error raised in one of multiple callbacks' do
101+
expect do
102+
ActiveRecord::Base.transaction do
103+
example_class.new.after_commit { raise 'this should prevent other callbacks being executed' }
104+
example_class.new.after_commit { handler_1.call }
105+
example_class.new.after_commit { handler_2.call }
106+
end
107+
end.to raise_error('this should prevent other callbacks being executed')
108+
expect(handler_1).not_to have_received(:call)
109+
expect(handler_2).not_to have_received(:call)
110+
end
102111
end
103112

104113
context "without transaction" do

0 commit comments

Comments
 (0)