Skip to content

Commit

Permalink
Preserve I18n.locale set when after_commit is called (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilka2 authored Feb 7, 2025
1 parent 0db52e7 commit fff8885
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/after_commit_everywhere/wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Wrap
def initialize(connection: ActiveRecord::Base.connection, **handlers)
@connection = connection
@handlers = handlers
@locale = I18n.locale
end

# rubocop: disable Naming/PredicateName
Expand All @@ -16,21 +17,21 @@ def has_transactional_callbacks?
# rubocop: enable Naming/PredicateName

def before_committed!(*)
@handlers[:before_commit]&.call
I18n.with_locale(@locale) { @handlers[:before_commit]&.call }
end

def trigger_transactional_callbacks?
true
end

def committed!(should_run_callbacks: true)
if should_run_callbacks
@handlers[:after_commit]&.call
end
return unless should_run_callbacks

I18n.with_locale(@locale) { @handlers[:after_commit]&.call }
end

def rolledback!(*)
@handlers[:after_rollback]&.call
I18n.with_locale(@locale) { @handlers[:after_rollback]&.call }
end

# Required for +transaction(requires_new: true)+
Expand Down
40 changes: 40 additions & 0 deletions spec/after_commit_everywhere_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@
expect(handler_1).not_to have_received(:call)
expect(handler_2).not_to have_received(:call)
end

it "preserves the locale" do
I18n.enforce_available_locales = false

ActiveRecord::Base.transaction do
expect(I18n.locale).not_to eq(:de)
I18n.with_locale(:de) { example_class.new.after_commit { handler.call(I18n.locale) } }
expect(handler).not_to have_received(:call)
end
expect(handler).to have_received(:call).with(:de)

I18n.enforce_available_locales = true
end
end

context "without transaction" do
Expand Down Expand Up @@ -291,6 +304,19 @@
end
expect(handler).not_to have_received(:call)
end

it "preserves the locale" do
I18n.enforce_available_locales = false

ActiveRecord::Base.transaction do
expect(I18n.locale).not_to eq(:de)
I18n.with_locale(:de) { example_class.new.before_commit { handler.call(I18n.locale) } }
expect(handler).not_to have_received(:call)
end
expect(handler).to have_received(:call).with(:de)

I18n.enforce_available_locales = true
end
end

context "without transaction" do
Expand Down Expand Up @@ -439,6 +465,20 @@
end
expect(handler).not_to have_received(:call)
end

it "preserves the locale" do
I18n.enforce_available_locales = false

ActiveRecord::Base.transaction do
expect(I18n.locale).not_to eq(:de)
I18n.with_locale(:de) { example_class.new.after_rollback { handler.call(I18n.locale) } }
expect(handler).not_to have_received(:call)
raise ActiveRecord::Rollback
end
expect(handler).to have_received(:call).with(:de)

I18n.enforce_available_locales = true
end
end

context "without transaction" do
Expand Down

0 comments on commit fff8885

Please sign in to comment.