Move standing order emails to a dedicated StandingOrderMailer

This commit is contained in:
Rob Harrington
2017-11-16 12:53:25 +11:00
parent d6a9d63de5
commit 6e83c6dc86
12 changed files with 200 additions and 137 deletions

View File

@@ -25,7 +25,7 @@ class StandingOrderConfirmJob
end
def send_confirm_email(order)
Spree::OrderMailer.standing_order_email(order.id, 'confirmation', {}).deliver
StandingOrderMailer.confirmation_email(order).deliver
end
def recently_closed_order_cycles

View File

@@ -57,11 +57,11 @@ class StandingOrderPlacementJob
def send_placement_email(order, changes)
return unless order.completed?
Spree::OrderMailer.standing_order_email(order.id, 'placement', changes).deliver
StandingOrderMailer.placement_email(order, changes).deliver
end
def send_empty_email(order, changes)
Spree::OrderMailer.standing_order_email(order.id, 'empty', changes).deliver
StandingOrderMailer.empty_email(order, changes).deliver
end
def log_completion_issue(order)

View File

@@ -39,17 +39,6 @@ Spree::OrderMailer.class_eval do
:reply_to => @order.distributor.contact.email)
end
def standing_order_email(order, type, changes)
@type = type
@changes = changes
find_order(order) # Finds an order instance from an id
subject = "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{@order.number}"
mail(:to => @order.email,
:from => from_address,
:subject => subject,
:reply_to => @order.distributor.email)
end
def find_order(order)
@order = order.respond_to?(:id) ? order : Spree::Order.find(order)
end

View File

@@ -0,0 +1,33 @@
class StandingOrderMailer < Spree::BaseMailer
helper CheckoutHelper
def confirmation_email(order)
@type = 'confirmation'
@order = order
send_mail(order)
end
def empty_email(order, changes)
@type = 'empty'
@changes = changes
@order = order
send_mail(order)
end
def placement_email(order, changes)
@type = 'placement'
@changes = changes
@order = order
send_mail(order)
end
private
def send_mail(order)
subject = "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{order.number}"
mail(:to => order.email,
:from => from_address,
:subject => subject,
:reply_to => order.distributor.email)
end
end

View File

@@ -1,45 +0,0 @@
%table.social.white-bg{:width => "100%"}
%tr
%td
%table.column{:align => "left"}
%tr
%td
%h3
= t :email_confirm_customer_greeting, name: @order.bill_address.firstname
%h4
- if @order.paid? && @type == 'confirmation'
= t :email_so_payment_success_intro_html, distributor: @order.distributor.name
- else
= t("email_so_#{@type}_intro_html", distributor: @order.distributor.name)
%table.column{:align => "left"}
%tr
%td{:align => "right"}
%img.float-right{:src => "#{@order.distributor.logo.url(:medium)}"}/
%span.clear
%p.callout
= t("email_so_#{@type}_explainer_html")
- if @type != 'empty'
= t("email_so_edit_false_html",
orders_close_at: l(@order.order_cycle.orders_close_at, format: "%a %b %d @ %l:%M%p"),
order_url: spree.order_url(@order))
= t("email_so_contact_distributor_html", distributor: @order.distributor.name, email: @order.distributor.email)
%p &nbsp;
%h4
= t :email_confirm_customer_number_html, number: @order.number
%p
= t("email_so_#{@type}_details_html", distributor: @order.distributor.name)
- if @changes.any? && @type == 'placement'
%p.callout
= t("email_so_placement_changes")
= render 'order_summary'
- unless @type == 'empty'
= render 'payment'
= render 'shipping'
= render 'special_instructions'
= render 'signoff'

View File

@@ -0,0 +1,18 @@
%table.social.white-bg{:width => "100%"}
%tr
%td
%table.column{:align => "left"}
%tr
%td
%h3
= t :email_confirm_customer_greeting, name: @order.bill_address.firstname
%h4
- if @order.paid? && type == 'confirmation'
= t :email_so_payment_success_intro_html, distributor: @order.distributor.name
- else
= t("email_so_#{type}_intro_html", distributor: @order.distributor.name)
%table.column{:align => "left"}
%tr
%td{:align => "right"}
%img.float-right{:src => "#{@order.distributor.logo.url(:medium)}"}/
%span.clear

View File

@@ -0,0 +1,22 @@
= render 'header', type: @type
%p.callout
= t("email_so_confirmation_explainer_html")
= t("email_so_edit_false_html",
orders_close_at: l(@order.order_cycle.orders_close_at, format: "%a %b %d @ %l:%M%p"),
order_url: spree.order_url(@order))
= t("email_so_contact_distributor_html", distributor: @order.distributor.name, email: @order.distributor.email)
%p &nbsp;
%h4
= t :email_confirm_customer_number_html, number: @order.number
%p
= t("email_so_confirmation_details_html", distributor: @order.distributor.name)
= render 'spree/order_mailer/order_summary'
= render 'spree/order_mailer/payment'
= render 'spree/order_mailer/shipping'
= render 'spree/order_mailer/special_instructions'
= render 'spree/order_mailer/signoff'

View File

@@ -0,0 +1,15 @@
= render 'header', type: @type
%p.callout
= t("email_so_empty_explainer_html")
= t("email_so_contact_distributor_html", distributor: @order.distributor.name, email: @order.distributor.email)
%p &nbsp;
%h4
= t :email_confirm_customer_number_html, number: @order.number
%p
= t("email_so_empty_details_html", distributor: @order.distributor.name)
= render 'spree/order_mailer/order_summary'
= render 'spree/order_mailer/signoff'

View File

@@ -0,0 +1,26 @@
= render 'header', type: @type
%p.callout
= t("email_so_placement_explainer_html")
= t("email_so_edit_false_html",
orders_close_at: l(@order.order_cycle.orders_close_at, format: "%a %b %d @ %l:%M%p"),
order_url: spree.order_url(@order))
= t("email_so_contact_distributor_html", distributor: @order.distributor.name, email: @order.distributor.email)
%p &nbsp;
%h4
= t :email_confirm_customer_number_html, number: @order.number
%p
= t("email_so_placement_details_html", distributor: @order.distributor.name)
- if @changes.any?
%p.callout
= t("email_so_placement_changes")
= render 'spree/order_mailer/order_summary'
= render 'spree/order_mailer/payment'
= render 'spree/order_mailer/shipping'
= render 'spree/order_mailer/special_instructions'
= render 'spree/order_mailer/signoff'

View File

@@ -187,7 +187,7 @@ describe StandingOrderPlacementJob do
let(:changes) { double(:changes) }
before do
allow(Spree::OrderMailer).to receive(:standing_order_email) { mail_mock }
allow(StandingOrderMailer).to receive(:placement_email) { mail_mock }
allow(mail_mock).to receive(:deliver)
end
@@ -196,7 +196,7 @@ describe StandingOrderPlacementJob do
it "sends the email" do
job.send(:send_placement_email, order, changes)
expect(Spree::OrderMailer).to have_received(:standing_order_email).with(order.id, 'placement', changes)
expect(StandingOrderMailer).to have_received(:placement_email).with(order, changes)
expect(mail_mock).to have_received(:deliver)
end
end
@@ -204,7 +204,7 @@ describe StandingOrderPlacementJob do
context "when the order is incomplete" do
it "does not send the email" do
job.send(:send_placement_email, order, changes)
expect(Spree::OrderMailer).to_not have_received(:standing_order_email)
expect(StandingOrderMailer).to_not have_received(:placement_email)
expect(mail_mock).to_not have_received(:deliver)
end
end

View File

@@ -52,79 +52,4 @@ describe Spree::OrderMailer do
end
end
end
describe "order placement for standing orders" do
let(:standing_order) { create(:standing_order, with_items: true) }
let(:proxy_order) { create(:proxy_order, standing_order: standing_order) }
let!(:order) { proxy_order.initialise_order! }
context "when changes have been made to the order" do
let(:changes) { {} }
before do
changes[order.line_items.first.id] = 2
expect do
Spree::OrderMailer.standing_order_email(order.id, 'placement', changes).deliver
end.to change{Spree::OrderMailer.deliveries.count}.by(1)
end
it "sends the email, which notifies the customer of changes made" do
body = Spree::OrderMailer.deliveries.last.body.encoded
expect(body).to include "This order was automatically created for you."
expect(body).to include "Unfortunately, not all products that you requested were available."
expect(body).to include "href=\"#{spree.order_url(order)}\""
end
end
context "and changes have not been made to the order" do
before do
expect do
Spree::OrderMailer.standing_order_email(order.id, 'placement', {}).deliver
end.to change{Spree::OrderMailer.deliveries.count}.by(1)
end
it "sends the email" do
body = Spree::OrderMailer.deliveries.last.body.encoded
expect(body).to include "This order was automatically created for you."
expect(body).to_not include "Unfortunately, not all products that you requested were available."
expect(body).to include "href=\"#{spree.order_url(order)}\""
end
end
end
describe "order confirmation for standing orders" do
let(:standing_order) { create(:standing_order, with_items: true) }
let(:proxy_order) { create(:proxy_order, standing_order: standing_order) }
let!(:order) { proxy_order.initialise_order! }
before do
expect do
Spree::OrderMailer.standing_order_email(order.id, 'confirmation', {}).deliver
end.to change{Spree::OrderMailer.deliveries.count}.by(1)
end
it "sends the email" do
body = Spree::OrderMailer.deliveries.last.body.encoded
expect(body).to include "This order was automatically placed for you"
expect(body).to include "href=\"#{spree.order_url(order)}\""
end
end
describe "empty order notification for standing orders" do
let(:standing_order) { create(:standing_order, with_items: true) }
let(:proxy_order) { create(:proxy_order, standing_order: standing_order) }
let!(:order) { proxy_order.initialise_order! }
before do
expect do
Spree::OrderMailer.standing_order_email(order.id, 'empty', {}).deliver
end.to change{Spree::OrderMailer.deliveries.count}.by(1)
end
it "sends the email" do
body = Spree::OrderMailer.deliveries.last.body.encoded
expect(body).to include "We tried to place a new order with"
expect(body).to include "Unfortunately, none of products that you ordered were available"
end
end
end

View File

@@ -0,0 +1,80 @@
require 'spec_helper'
describe StandingOrderMailer do
let!(:mail_method) { create(:mail_method, preferred_mails_from: 'spree@example.com') }
describe "order placement" do
let(:standing_order) { create(:standing_order, with_items: true) }
let(:proxy_order) { create(:proxy_order, standing_order: standing_order) }
let!(:order) { proxy_order.initialise_order! }
context "when changes have been made to the order" do
let(:changes) { {} }
before do
changes[order.line_items.first.id] = 2
expect do
StandingOrderMailer.placement_email(order, changes).deliver
end.to change{StandingOrderMailer.deliveries.count}.by(1)
end
it "sends the email, which notifies the customer of changes made" do
body = StandingOrderMailer.deliveries.last.body.encoded
expect(body).to include "This order was automatically created for you."
expect(body).to include "Unfortunately, not all products that you requested were available."
expect(body).to include "href=\"#{spree.order_url(order)}\""
end
end
context "and changes have not been made to the order" do
before do
expect do
StandingOrderMailer.placement_email(order, {}).deliver
end.to change{StandingOrderMailer.deliveries.count}.by(1)
end
it "sends the email" do
body = StandingOrderMailer.deliveries.last.body.encoded
expect(body).to include "This order was automatically created for you."
expect(body).to_not include "Unfortunately, not all products that you requested were available."
expect(body).to include "href=\"#{spree.order_url(order)}\""
end
end
end
describe "order confirmation" do
let(:standing_order) { create(:standing_order, with_items: true) }
let(:proxy_order) { create(:proxy_order, standing_order: standing_order) }
let!(:order) { proxy_order.initialise_order! }
before do
expect do
StandingOrderMailer.confirmation_email(order).deliver
end.to change{StandingOrderMailer.deliveries.count}.by(1)
end
it "sends the email" do
body = StandingOrderMailer.deliveries.last.body.encoded
expect(body).to include "This order was automatically placed for you"
expect(body).to include "href=\"#{spree.order_url(order)}\""
end
end
describe "empty order notification" do
let(:standing_order) { create(:standing_order, with_items: true) }
let(:proxy_order) { create(:proxy_order, standing_order: standing_order) }
let!(:order) { proxy_order.initialise_order! }
before do
expect do
StandingOrderMailer.empty_email(order, {}).deliver
end.to change{StandingOrderMailer.deliveries.count}.by(1)
end
it "sends the email" do
body = StandingOrderMailer.deliveries.last.body.encoded
expect(body).to include "We tried to place a new order with"
expect(body).to include "Unfortunately, none of products that you ordered were available"
end
end
end