Re-add dynamic delivery_method for ApplicationMailer (#13994)

This commit is contained in:
Mac Siri 2021-06-23 15:17:27 -04:00 committed by GitHub
parent 0eaea33710
commit a8c75fb0e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 40 deletions

View file

@ -6,13 +6,10 @@ class ApplicationMailer < ActionMailer::Base
helper Rails.application.routes.url_helpers
helper ApplicationHelper
helper AuthenticationHelper
include Deliverable
before_action :use_custom_host
# [@SRE] temporarily disabled
# before_action :set_delivery_options
# before_action :set_perform_deliveries
default(
from: -> { email_from },
template_path: ->(mailer) { "mailers/#{mailer.class.name.underscore}" },
@ -40,14 +37,4 @@ class ApplicationMailer < ActionMailer::Base
def use_custom_host
ActionMailer::Base.default_url_options[:host] = Settings::General.app_domain
end
def set_perform_deliveries
self.perform_deliveries = ForemInstance.smtp_enabled?
end
protected
def set_delivery_options
self.smtp_settings = Settings::SMTP.settings
end
end

View file

@ -0,0 +1,16 @@
module Deliverable
extend ActiveSupport::Concern
included do
before_action :set_perform_deliveries
after_action :set_delivery_options
end
def set_perform_deliveries
self.perform_deliveries = ForemInstance.smtp_enabled?
end
def set_delivery_options
mail.delivery_method.settings.merge!(Settings::SMTP.settings)
end
end

View file

@ -1,4 +1,6 @@
class DeviseMailer < Devise::Mailer
include Deliverable
before_action :use_settings_general_values
def use_settings_general_values

View file

@ -9,7 +9,7 @@ module Follows
return if EmailMessage.where(user_id: follow.followable_id)
.where("sent_at > ?", rand(15..35).hours.ago)
.exists?(["subject LIKE ?", "%#{NotifyMailer.subjects[:new_follower_email]}"])
.exists?(["subject LIKE ?", "%#{NotifyMailer.new.subjects[:new_follower_email]}"])
mailer.constantize.with(follow: follow).new_follower_email.deliver_now
end

View file

@ -127,27 +127,8 @@ Rails.application.configure do
protocol = ENV["APP_PROTOCOL"] || "http://"
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.perform_deliveries = false
config.action_mailer.default_url_options = { host: protocol + ENV["APP_DOMAIN"].to_s }
ActionMailer::Base.smtp_settings = if ENV["SENDGRID_API_KEY"].present?
{
address: "smtp.sendgrid.net",
port: 587,
authentication: :plain,
user_name: "apikey",
password: ENV["SENDGRID_API_KEY"],
domain: ENV["APP_DOMAIN"]
}
else
{
address: ENV["SMTP_ADDRESS"],
port: ENV["SMTP_PORT"],
authentication: ENV["SMTP_AUTHENTICATION"],
user_name: ENV["SMTP_USER_NAME"],
password: ENV["SMTP_PASSWORD"],
domain: ENV["SMTP_DOMAIN"]
}
end
if ENV["HEROKU_APP_URL"].present? && ENV["HEROKU_APP_URL"] != ENV["APP_DOMAIN"]
config.middleware.use Rack::HostRedirect,

View file

@ -4,7 +4,7 @@ RSpec.describe ApplicationMailer, type: :mailer do
let(:user) { create(:user) }
let(:email) { VerificationMailer.with(user_id: user.id).account_ownership_verification_email }
xdescribe "#set_perform_deliveries" do
describe "#set_perform_deliveries" do
it "changes perform_deliveries from true to false if smtp is not enabled" do
expect { email.deliver_now }.to change(described_class, :perform_deliveries).from(true).to(false)
end
@ -17,11 +17,17 @@ RSpec.describe ApplicationMailer, type: :mailer do
end
end
xdescribe "#set_delivery_options" do
it "evoked Settings::SMTP.settings during callback" do
allow(Settings::SMTP).to receive(:settings)
describe "#set_delivery_options" do
after do
Settings::SMTP.clear_cache
end
it "sets proper SMTP credential during callback" do
Settings::SMTP.user_name = Faker::Internet.username
Settings::SMTP.password = Faker::Internet.password
email.deliver_now
expect(Settings::SMTP).to have_received(:settings)
expect(described_class.deliveries.last.delivery_method.settings).to eq(Settings::SMTP.settings)
end
end
end