Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions gemfiles/activerecord_6_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
1 change: 1 addition & 0 deletions gemfiles/activerecord_6_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
1 change: 1 addition & 0 deletions gemfiles/activerecord_7_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../"
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