Skip to content

Commit fff8885

Browse files
authored
Preserve I18n.locale set when after_commit is called (#36)
1 parent 0db52e7 commit fff8885

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

lib/after_commit_everywhere/wrap.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Wrap
77
def initialize(connection: ActiveRecord::Base.connection, **handlers)
88
@connection = connection
99
@handlers = handlers
10+
@locale = I18n.locale
1011
end
1112

1213
# rubocop: disable Naming/PredicateName
@@ -16,21 +17,21 @@ def has_transactional_callbacks?
1617
# rubocop: enable Naming/PredicateName
1718

1819
def before_committed!(*)
19-
@handlers[:before_commit]&.call
20+
I18n.with_locale(@locale) { @handlers[:before_commit]&.call }
2021
end
2122

2223
def trigger_transactional_callbacks?
2324
true
2425
end
2526

2627
def committed!(should_run_callbacks: true)
27-
if should_run_callbacks
28-
@handlers[:after_commit]&.call
29-
end
28+
return unless should_run_callbacks
29+
30+
I18n.with_locale(@locale) { @handlers[:after_commit]&.call }
3031
end
3132

3233
def rolledback!(*)
33-
@handlers[:after_rollback]&.call
34+
I18n.with_locale(@locale) { @handlers[:after_rollback]&.call }
3435
end
3536

3637
# Required for +transaction(requires_new: true)+

spec/after_commit_everywhere_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@
108108
expect(handler_1).not_to have_received(:call)
109109
expect(handler_2).not_to have_received(:call)
110110
end
111+
112+
it "preserves the locale" do
113+
I18n.enforce_available_locales = false
114+
115+
ActiveRecord::Base.transaction do
116+
expect(I18n.locale).not_to eq(:de)
117+
I18n.with_locale(:de) { example_class.new.after_commit { handler.call(I18n.locale) } }
118+
expect(handler).not_to have_received(:call)
119+
end
120+
expect(handler).to have_received(:call).with(:de)
121+
122+
I18n.enforce_available_locales = true
123+
end
111124
end
112125

113126
context "without transaction" do
@@ -291,6 +304,19 @@
291304
end
292305
expect(handler).not_to have_received(:call)
293306
end
307+
308+
it "preserves the locale" do
309+
I18n.enforce_available_locales = false
310+
311+
ActiveRecord::Base.transaction do
312+
expect(I18n.locale).not_to eq(:de)
313+
I18n.with_locale(:de) { example_class.new.before_commit { handler.call(I18n.locale) } }
314+
expect(handler).not_to have_received(:call)
315+
end
316+
expect(handler).to have_received(:call).with(:de)
317+
318+
I18n.enforce_available_locales = true
319+
end
294320
end
295321

296322
context "without transaction" do
@@ -439,6 +465,20 @@
439465
end
440466
expect(handler).not_to have_received(:call)
441467
end
468+
469+
it "preserves the locale" do
470+
I18n.enforce_available_locales = false
471+
472+
ActiveRecord::Base.transaction do
473+
expect(I18n.locale).not_to eq(:de)
474+
I18n.with_locale(:de) { example_class.new.after_rollback { handler.call(I18n.locale) } }
475+
expect(handler).not_to have_received(:call)
476+
raise ActiveRecord::Rollback
477+
end
478+
expect(handler).to have_received(:call).with(:de)
479+
480+
I18n.enforce_available_locales = true
481+
end
442482
end
443483

444484
context "without transaction" do

0 commit comments

Comments
 (0)