From a78ba5de063f1ad084f02c0093347035b345238a Mon Sep 17 00:00:00 2001 From: Bilka Date: Thu, 6 Feb 2025 15:10:09 +0100 Subject: [PATCH 1/2] Preserve I18n.locale set when after_commit is called --- lib/after_commit_everywhere/wrap.rb | 11 ++++---- spec/after_commit_everywhere_spec.rb | 40 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/after_commit_everywhere/wrap.rb b/lib/after_commit_everywhere/wrap.rb index 66f19af..a898c0d 100644 --- a/lib/after_commit_everywhere/wrap.rb +++ b/lib/after_commit_everywhere/wrap.rb @@ -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 @@ -16,7 +17,7 @@ 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? @@ -24,13 +25,13 @@ def trigger_transactional_callbacks? 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)+ diff --git a/spec/after_commit_everywhere_spec.rb b/spec/after_commit_everywhere_spec.rb index 18f6088..118f18d 100644 --- a/spec/after_commit_everywhere_spec.rb +++ b/spec/after_commit_everywhere_spec.rb @@ -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 @@ -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 @@ -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 From 3a9ca7ecd32fa3e6999083ef3bcf7a181a51a4a2 Mon Sep 17 00:00:00 2001 From: Bilka Date: Thu, 6 Feb 2025 16:09:10 +0100 Subject: [PATCH 2/2] Fix pre Rails 7.1 tests Refer to https://github.com/rails/rails/pull/54264#issuecomment-2596149819 --- Appraisals | 3 +++ gemfiles/activerecord_6_0.gemfile | 1 + gemfiles/activerecord_6_1.gemfile | 1 + gemfiles/activerecord_7_0.gemfile | 1 + 4 files changed, 6 insertions(+) diff --git a/Appraisals b/Appraisals index 32fc913..b1bcaaf 100644 --- a/Appraisals +++ b/Appraisals @@ -23,18 +23,21 @@ end appraise "activerecord-6-0" do gem "activerecord", "~> 6.0.0" gem "sqlite3", "~> 1.4" + gem "concurrent-ruby", "< 1.3.5" end appraise "activerecord-6-1" do gem "activerecord", "~> 6.1.0" gem "sqlite3", "~> 1.4" gem "rspec-rails", "~> 4.0" + gem "concurrent-ruby", "< 1.3.5" end appraise "activerecord-7-0" do gem "activerecord", "~> 7.0.0" gem "sqlite3", "~> 1.4" gem "rspec-rails", "~> 5.0" + gem "concurrent-ruby", "< 1.3.5" end appraise "activerecord-7-1" do diff --git a/gemfiles/activerecord_6_0.gemfile b/gemfiles/activerecord_6_0.gemfile index 0cecfa6..ec1f56e 100644 --- a/gemfiles/activerecord_6_0.gemfile +++ b/gemfiles/activerecord_6_0.gemfile @@ -4,5 +4,6 @@ source "https://rubygems.org" gem "activerecord", "~> 6.0.0" gem "sqlite3", "~> 1.4" +gem "concurrent-ruby", "< 1.3.5" gemspec path: "../" diff --git a/gemfiles/activerecord_6_1.gemfile b/gemfiles/activerecord_6_1.gemfile index 87bf5da..eb55795 100644 --- a/gemfiles/activerecord_6_1.gemfile +++ b/gemfiles/activerecord_6_1.gemfile @@ -5,5 +5,6 @@ source "https://rubygems.org" gem "activerecord", "~> 6.1.0" gem "sqlite3", "~> 1.4" gem "rspec-rails", "~> 6.0" +gem "concurrent-ruby", "< 1.3.5" gemspec path: "../" diff --git a/gemfiles/activerecord_7_0.gemfile b/gemfiles/activerecord_7_0.gemfile index 6d33f41..552498b 100644 --- a/gemfiles/activerecord_7_0.gemfile +++ b/gemfiles/activerecord_7_0.gemfile @@ -5,5 +5,6 @@ source "https://rubygems.org" gem "activerecord", "~> 7.0.0" gem "sqlite3", "~> 1.4" gem "rspec-rails", "~> 6.0" +gem "concurrent-ruby", "< 1.3.5" gemspec path: "../"