Set the asset host based on default_url_options
We were duplicating the asset host and the URL host in all environments, but we can make it so the asset host uses the URL host unless we specifically set it. Note that, inside the `ApplicationMailer`, the `root_url` method already uses `default_url_options` to generate the URL. In the rare case of CONSUL installations who have changed the asset host, this change has no effect since they'll get a conflict in the environment files when upgrading and they'll choose to use their own asset host.
This commit is contained in:
@@ -4,4 +4,9 @@ class ApplicationMailer < ActionMailer::Base
|
||||
helper :mailer
|
||||
default from: proc { "#{Setting["mailer_from_name"]} <#{Setting["mailer_from_address"]}>" }
|
||||
layout "mailer"
|
||||
before_action :set_asset_host
|
||||
|
||||
def set_asset_host
|
||||
self.asset_host ||= root_url.delete_suffix("/")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -31,7 +31,6 @@ Rails.application.configure do
|
||||
# Don't care if the mailer can't send.
|
||||
config.action_mailer.raise_delivery_errors = false
|
||||
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
|
||||
config.action_mailer.asset_host = "http://localhost:3000"
|
||||
|
||||
# Deliver emails to a development mailbox at /letter_opener
|
||||
config.action_mailer.delivery_method = :letter_opener
|
||||
|
||||
@@ -70,7 +70,6 @@ Rails.application.configure do
|
||||
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
|
||||
config.action_mailer.raise_delivery_errors = true
|
||||
config.action_mailer.default_url_options = { host: Rails.application.secrets.server_name }
|
||||
config.action_mailer.asset_host = "https://#{Rails.application.secrets.server_name}"
|
||||
|
||||
# Configure your SMTP service credentials in secrets.yml
|
||||
if Rails.application.secrets.smtp_settings
|
||||
|
||||
@@ -69,7 +69,6 @@ Rails.application.configure do
|
||||
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
|
||||
config.action_mailer.raise_delivery_errors = true
|
||||
config.action_mailer.default_url_options = { host: Rails.application.secrets.server_name }
|
||||
config.action_mailer.asset_host = "https://#{Rails.application.secrets.server_name}"
|
||||
|
||||
# Configure your SMTP service credentials in secrets.yml
|
||||
if Rails.application.secrets.smtp_settings
|
||||
|
||||
@@ -45,7 +45,6 @@ Rails.application.configure do
|
||||
# ActionMailer::Base.deliveries array.
|
||||
config.action_mailer.delivery_method = :test
|
||||
config.action_mailer.default_url_options = { host: "test" }
|
||||
config.action_mailer.asset_host = "http://consul.test"
|
||||
|
||||
# Print deprecation notices to the stderr.
|
||||
config.active_support.deprecation = :stderr
|
||||
|
||||
41
spec/mailers/application_mailer_spec.rb
Normal file
41
spec/mailers/application_mailer_spec.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe ApplicationMailer do
|
||||
describe "#set_asset_host" do
|
||||
let(:mailer) { ApplicationMailer.new }
|
||||
|
||||
it "returns a host based on the default_url_options by default" do
|
||||
allow(ActionMailer::Base).to receive(:default_url_options).and_return(host: "consul.dev")
|
||||
|
||||
mailer.set_asset_host
|
||||
|
||||
expect(mailer.asset_host).to eq "http://consul.dev"
|
||||
end
|
||||
|
||||
it "considers options like port and protocol" do
|
||||
allow(ActionMailer::Base).to receive(:default_url_options).and_return(
|
||||
host: "localhost",
|
||||
protocol: "https",
|
||||
port: 3000
|
||||
)
|
||||
|
||||
mailer.set_asset_host
|
||||
|
||||
expect(mailer.asset_host).to eq "https://localhost:3000"
|
||||
end
|
||||
|
||||
it "returns the asset host when set manually" do
|
||||
default_asset_host = ActionMailer::Base.asset_host
|
||||
|
||||
begin
|
||||
ActionMailer::Base.asset_host = "http://consulassets.dev"
|
||||
|
||||
mailer.set_asset_host
|
||||
|
||||
expect(mailer.asset_host).to eq "http://consulassets.dev"
|
||||
ensure
|
||||
ActionMailer::Base.asset_host = default_asset_host
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user