From 66da02f1de8ac3564a2229dd392fa3145197fc5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 6 Sep 2019 15:09:35 +0200 Subject: [PATCH] Evaluate mailer from address at runtime We're reading the value from the database, but the `ApplicationMailer.default` method is evaluated when the application is started. So if we don't use a Proc, we'll need to restart the server every time we change the value in the database, or else the old value will still be used. Using a Proc makes sure the mailer from address is evaluated at runtime, so emails are sent using the from address currently defined in the database. The same situation took place using the devise mailer. Now we don't need to check for the settings table being present because the Proc in the devise initializer won't be evaluated before the settings table is created and populated. --- app/mailers/application_mailer.rb | 2 +- config/initializers/devise.rb | 6 +----- spec/mailers/devise_mailer_spec.rb | 9 +++++++++ spec/mailers/mailer_spec.rb | 9 +++++++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index f002cedd6..278862070 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,6 +1,6 @@ class ApplicationMailer < ActionMailer::Base helper :settings helper :application - default from: "#{Setting["mailer_from_name"]} <#{Setting["mailer_from_address"]}>" + default from: Proc.new { "#{Setting["mailer_from_name"]} <#{Setting["mailer_from_address"]}>" } layout "mailer" end diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 4e7af7476..3f4a58076 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -12,11 +12,7 @@ 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. - if Rails.env.test? || !ActiveRecord::Base.connection.data_source_exists?("settings") - config.mailer_sender = "noreply@consul.dev" - else - config.mailer_sender = "'#{Setting["mailer_from_name"]}' <#{Setting["mailer_from_address"]}>" - end + config.mailer_sender = Proc.new { "'#{Setting["mailer_from_name"]}' <#{Setting["mailer_from_address"]}>" } # Configure the class responsible to send e-mails. config.mailer = "DeviseMailer" diff --git a/spec/mailers/devise_mailer_spec.rb b/spec/mailers/devise_mailer_spec.rb index ef4e0c38c..4e863a209 100644 --- a/spec/mailers/devise_mailer_spec.rb +++ b/spec/mailers/devise_mailer_spec.rb @@ -12,5 +12,14 @@ describe DeviseMailer do expect(email.subject).to include("confirmación") end + + it "reads the from address at runtime" do + Setting["mailer_from_name"] = "New organization" + Setting["mailer_from_address"] = "new@consul.dev" + + email = DeviseMailer.confirmation_instructions(create(:user), "ABC") + + expect(email).to deliver_from "'New organization' " + end end end diff --git a/spec/mailers/mailer_spec.rb b/spec/mailers/mailer_spec.rb index 6215a3c3a..786a06102 100644 --- a/spec/mailers/mailer_spec.rb +++ b/spec/mailers/mailer_spec.rb @@ -13,5 +13,14 @@ describe Mailer do expect(email.subject).to include("comentado") end + + it "reads the from address at runtime" do + Setting["mailer_from_name"] = "New organization" + Setting["mailer_from_address"] = "new@consul.dev" + + email = Mailer.comment(create(:comment)) + + expect(email).to deliver_from "New organization " + end end end