Merge pull request #5005 from consul/asset_host
Simplify setting the asset host in mailers
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
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
class DeviseMailer < Devise::Mailer
|
||||
helper :application, :settings, :mailer
|
||||
include Devise::Controllers::UrlHelpers
|
||||
default template_path: "devise/mailer"
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -14,11 +14,15 @@ Devise.setup do |config|
|
||||
# Configure the e-mail address which will be shown in Devise::Mailer,
|
||||
# note that it will be overwritten if you use your own mailer class
|
||||
# with default "from" parameter.
|
||||
config.mailer_sender = proc { "'#{Setting["mailer_from_name"]}' <#{Setting["mailer_from_address"]}>" }
|
||||
# We're not setting it here because it's set by the ApplicationMailer class
|
||||
# config.mailer_sender = "please-change-me-at-config-initializers-devise@example.com"
|
||||
|
||||
# Configure the class responsible to send e-mails.
|
||||
config.mailer = "DeviseMailer"
|
||||
|
||||
# Configure the parent class responsible to send e-mails.
|
||||
config.parent_mailer = "ApplicationMailer"
|
||||
|
||||
# ==> ORM configuration
|
||||
# Load and configure the ORM. Supports :active_record (default) and
|
||||
# :mongoid (bson_ext recommended) by default. Other ORMs may be
|
||||
|
||||
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
|
||||
@@ -18,7 +18,7 @@ describe DeviseMailer do
|
||||
|
||||
email = DeviseMailer.confirmation_instructions(create(:user), "ABC")
|
||||
|
||||
expect(email).to deliver_from "'New organization' <new@consul.dev>"
|
||||
expect(email).to deliver_from "New organization <new@consul.dev>"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user