get rid of with_deliveries method

It was setting ActionMailer::Base.perform_deliveries for the duration of
the block, but perform_deliveries is a class attribute, so not thread
local and doesn't work as expected in combination with setting
perform_deliveries to true when calling
Setting::MailSettings#reload_mailer_settings! at the start of every
request and every job run
This commit is contained in:
Ivan Kuchin
2025-09-30 16:14:53 +02:00
parent 6e7388a32a
commit d7aec46d9f
3 changed files with 9 additions and 56 deletions
-9
View File
@@ -45,15 +45,6 @@ class ApplicationMailer < ActionMailer::Base
default from: Proc.new { Setting.mail_from }
class << self
# Activates/deactivates email deliveries during +block+
def with_deliveries(temporary_state = true, &)
old_state = ActionMailer::Base.perform_deliveries
ActionMailer::Base.perform_deliveries = temporary_state
yield
ensure
ActionMailer::Base.perform_deliveries = old_state
end
def host
if OpenProject::Configuration.rails_relative_url_root.blank?
Setting.host_name
+9 -13
View File
@@ -112,20 +112,16 @@ class CopyProjectJob < ApplicationJob
# rubocop:disable Metrics/AbcSize
# Most of the cost is from handling errors, we need to check what can be moved around / removed
def create_project_copy(target_project_params, associations_to_copy, send_mails)
errors = []
service_result = copy_project(target_project_params, associations_to_copy, send_mails)
target_project = service_result.result
errors = service_result.errors.full_messages
ProjectMailer.with_deliveries(send_mails) do
service_result = copy_project(target_project_params, associations_to_copy, send_mails)
target_project = service_result.result
errors = service_result.errors.full_messages
# We assume the copying worked "successfully" if the project was saved
if target_project&.persisted?
return target_project, errors
else
logger.error("Copying project fails with validation errors: #{errors.join("\n")}")
return nil, errors
end
# We assume the copying worked "successfully" if the project was saved
if target_project&.persisted?
[target_project, errors]
else
logger.error("Copying project fails with validation errors: #{errors.join("\n")}")
[nil, errors]
end
rescue ActiveRecord::RecordNotFound => e
logger.error("Entity missing: #{e.message} #{e.backtrace.join("\n")}")
-34
View File
@@ -69,40 +69,6 @@ RSpec.describe UserMailer do
allow(Setting).to receive(:default_language).and_return("en")
end
describe "#with_deliveries" do
context "with false" do
before do
described_class.with_deliveries(false) do
described_class.test_mail(recipient).deliver_now
end
end
it_behaves_like "mail is not sent"
end
context "with true" do
before do
described_class.with_deliveries(true) do
described_class.test_mail(recipient).deliver_now
end
end
it_behaves_like "mail is sent"
end
context "with true but user is locked" do
let(:recipient) { build_stubbed(:user, status: Principal.statuses[:locked]) }
before do
described_class.with_deliveries(true) do
described_class.test_mail(recipient).deliver_now
end
end
it_behaves_like "mail is not sent"
end
end
describe "#test_mail" do
let(:test_email) { "bob.bobbi@example.com" }
let(:recipient) { build_stubbed(:user, firstname: "Bob", lastname: "Bobbi", mail: test_email) }