Skip to content

Commit

Permalink
Merge pull request #2590 from luisramos0/spree2_mail_method_create
Browse files Browse the repository at this point in the history
Spree 2 - MailMethod - CreateMailMethod is now MailConfiguration
  • Loading branch information
sauloperez authored Sep 10, 2018
2 parents a906ad1 + 44d8162 commit cde2f98
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 101 deletions.
34 changes: 0 additions & 34 deletions app/services/create_mail_method.rb

This file was deleted.

18 changes: 18 additions & 0 deletions app/services/mail_configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Configures Rails to use the specified mail configuration
# 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 |
Spree::Config[name] = value
end
apply_mail_settings
end

private

def self.apply_mail_settings
Spree::Core::MailSettings.init
end
end
33 changes: 15 additions & 18 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,23 @@
end
end

def create_mail_method
Spree::MailMethod.destroy_all

CreateMailMethod.new(
environment: Rails.env,
preferred_enable_mail_delivery: true,
preferred_mail_host: ENV.fetch('MAIL_HOST'),
preferred_mail_domain: ENV.fetch('MAIL_DOMAIN'),
preferred_mail_port: ENV.fetch('MAIL_PORT'),
preferred_mail_auth_type: 'login',
preferred_smtp_username: ENV.fetch('SMTP_USERNAME'),
preferred_smtp_password: ENV.fetch('SMTP_PASSWORD'),
preferred_secure_connection_type: ENV.fetch('MAIL_SECURE_CONNECTION', 'None'),
preferred_mails_from: ENV.fetch('MAILS_FROM', "no-reply@#{ENV.fetch('MAIL_DOMAIN')}"),
preferred_mail_bcc: ENV.fetch('MAIL_BCC', ''),
preferred_intercept_email: ''
).call
def set_mail_configuration
MailConfiguration.entries= {
enable_mail_delivery: true,
mail_host: ENV.fetch('MAIL_HOST'),
mail_domain: ENV.fetch('MAIL_DOMAIN'),
mail_port: ENV.fetch('MAIL_PORT'),
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

create_mail_method
set_mail_configuration

spree_user = Spree::User.first
spree_user && spree_user.confirm!
49 changes: 0 additions & 49 deletions spec/services/create_mail_method_spec.rb

This file was deleted.

34 changes: 34 additions & 0 deletions spec/services/mail_configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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

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
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")
end

it 'initializes the mail settings' do
described_class.entries= entries
expect(Spree::Core::MailSettings).to have_received(:init)
end
end
end

0 comments on commit cde2f98

Please sign in to comment.