Merge pull request #7673 from Matt-Yorkley/spree-config-caching

Fix clashing loggers
This commit is contained in:
Matt-Yorkley
2021-05-25 11:09:33 +02:00
committed by GitHub
6 changed files with 40 additions and 40 deletions

View File

@@ -2,9 +2,8 @@
# by setting entries on the Spree Config
# and initializing Spree:MailSettings that uses the Spree::Config.
class MailConfiguration
# @param entries [Hash] Spree Config entries
def self.entries=(entries)
entries.each do |name, value|
def self.apply!
configuration.each do |name, value|
Spree::Config[name] = value
end
apply_mail_settings
@@ -12,6 +11,21 @@ class MailConfiguration
private
def self.configuration
{
mail_host: ENV.fetch('MAIL_HOST'),
mail_domain: ENV.fetch('MAIL_DOMAIN'),
mail_port: ENV.fetch('MAIL_PORT'),
mail_auth_type: ENV.fetch('MAIL_AUTH_TYPE', 'login'),
smtp_username: ENV.fetch('SMTP_USERNAME'),
smtp_password: ENV.fetch('SMTP_PASSWORD'),
secure_connection_type: ENV.fetch('MAIL_SECURE_CONNECTION', 'None'),
mails_from: ENV.fetch('MAILS_FROM', "no-reply@#{ENV.fetch('MAIL_DOMAIN')}"),
mail_bcc: ENV.fetch('MAIL_BCC', ''),
intercept_email: ''
}
end
def self.apply_mail_settings
Spree::Core::MailSettings.init
end

View File

@@ -1,4 +1,3 @@
Delayed::Worker.logger = Logger.new(Rails.root.join('log', 'delayed_job.log'))
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.max_run_time = 15.minutes

View File

@@ -18,6 +18,7 @@ Spree::Gateway.class_eval do
end
Spree.config do |config|
config.site_url = ENV['SITE_URL'] if ENV['SITE_URL']
config.shipping_instructions = true
config.address_requires_state = true
config.admin_interface_logo = '/default_images/ofn-logo.png'
@@ -31,6 +32,10 @@ Spree.config do |config|
config.s3_protocol = ENV.fetch('S3_PROTOCOL', 'https')
end
# Read mail configuration from ENV vars at boot time and ensure the values are
# applied correctly in Spree::Config.
MailConfiguration.apply!
# Attachments settings
Spree::Image.set_attachment_attribute(:path, ENV['ATTACHMENT_PATH']) if ENV['ATTACHMENT_PATH']
Spree::Image.set_attachment_attribute(:url, ENV['ATTACHMENT_URL']) if ENV['ATTACHMENT_URL']

View File

@@ -2,22 +2,8 @@
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
require 'yaml'
def set_mail_configuration
MailConfiguration.entries= {
mail_host: ENV.fetch('MAIL_HOST'),
mail_domain: ENV.fetch('MAIL_DOMAIN'),
mail_port: ENV.fetch('MAIL_PORT'),
mail_auth_type: ENV.fetch('MAIL_AUTH_TYPE', 'login'),
smtp_username: ENV.fetch('SMTP_USERNAME'),
smtp_password: ENV.fetch('SMTP_PASSWORD'),
secure_connection_type: ENV.fetch('MAIL_SECURE_CONNECTION', 'None'),
mails_from: ENV.fetch('MAILS_FROM', "no-reply@#{ENV.fetch('MAIL_DOMAIN')}"),
mail_bcc: ENV.fetch('MAIL_BCC', ''),
intercept_email: ''
}
end
# We need mail_configuration to create a user account, because it sends a confirmation email.
set_mail_configuration
MailConfiguration.apply!
puts "[db:seed] Seeding Roles"
Spree::Role.where(:name => "admin").first_or_create

View File

@@ -12,7 +12,7 @@ module Spree
end
def override!
ActionMailer::Base.default_url_options[:host] ||= Config.site_url
ActionMailer::Base.default_url_options[:host] ||= ENV.fetch("SITE_URL", Config.site_url)
ActionMailer::Base.smtp_settings = mail_server_settings
ActionMailer::Base.perform_deliveries = true
end

View File

@@ -3,33 +3,29 @@
require 'spec_helper'
describe MailConfiguration do
describe 'entries=' do
let(:mail_settings) { instance_double(Spree::Core::MailSettings) }
let(:entries) do
{ smtp_username: "smtp_username", mail_auth_type: "login" }
end
describe 'apply!' do
before do
allow(Spree::Core::MailSettings).to receive(:init) { mail_settings }
end
# keeps spree_config unchanged
around do |example|
original_smtp_username = Spree::Config[:smtp_username]
original_mail_auth_type = Spree::Config[:mail_auth_type]
example.run
Spree::Config[:smtp_username] = original_smtp_username
Spree::Config[:mail_auth_type] = original_mail_auth_type
allow(Spree::Core::MailSettings).to receive(:init) { true }
end
it 'sets config entries in the Spree Config' do
described_class.entries = entries
expect(Spree::Config[:smtp_username]).to eq("smtp_username")
expect(Spree::Config[:mail_auth_type]).to eq("login")
allow(Spree::Config).to receive(:[]=)
described_class.apply!
expect(Spree::Config).to have_received(:[]=).with(:mail_host, "example.com")
expect(Spree::Config).to have_received(:[]=).with(:mail_domain, "example.com")
expect(Spree::Config).to have_received(:[]=).with(:mail_port, "25")
expect(Spree::Config).to have_received(:[]=).with(:mail_auth_type, "login")
expect(Spree::Config).to have_received(:[]=).with(:smtp_username, "ofn")
expect(Spree::Config).to have_received(:[]=).with(:smtp_password, "f00d")
expect(Spree::Config).to have_received(:[]=).with(:secure_connection_type, "None")
expect(Spree::Config).to have_received(:[]=).with(:mails_from, "no-reply@example.com")
expect(Spree::Config).to have_received(:[]=).with(:mail_bcc, "")
expect(Spree::Config).to have_received(:[]=).with(:intercept_email, "")
end
it 'initializes the mail settings' do
described_class.entries = entries
described_class.apply!
expect(Spree::Core::MailSettings).to have_received(:init)
end
end