diff --git a/app/jobs/standing_order_confirm_job.rb b/app/jobs/standing_order_confirm_job.rb index eedd1d4a6f..f383b9c555 100644 --- a/app/jobs/standing_order_confirm_job.rb +++ b/app/jobs/standing_order_confirm_job.rb @@ -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 diff --git a/app/jobs/standing_order_placement_job.rb b/app/jobs/standing_order_placement_job.rb index 0e6ee85c9c..903c6dffe6 100644 --- a/app/jobs/standing_order_placement_job.rb +++ b/app/jobs/standing_order_placement_job.rb @@ -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) diff --git a/app/mailers/spree/order_mailer_decorator.rb b/app/mailers/spree/order_mailer_decorator.rb index afcb9de86e..a4b2db8b70 100644 --- a/app/mailers/spree/order_mailer_decorator.rb +++ b/app/mailers/spree/order_mailer_decorator.rb @@ -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 diff --git a/app/mailers/standing_order_mailer.rb b/app/mailers/standing_order_mailer.rb new file mode 100644 index 0000000000..11bbea53d0 --- /dev/null +++ b/app/mailers/standing_order_mailer.rb @@ -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 diff --git a/app/views/spree/order_mailer/standing_order_email.html.haml b/app/views/spree/order_mailer/standing_order_email.html.haml deleted file mode 100644 index 4f9d4dfe7a..0000000000 --- a/app/views/spree/order_mailer/standing_order_email.html.haml +++ /dev/null @@ -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   -%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' diff --git a/app/views/standing_order_mailer/_header.html.haml b/app/views/standing_order_mailer/_header.html.haml new file mode 100644 index 0000000000..9453260f2e --- /dev/null +++ b/app/views/standing_order_mailer/_header.html.haml @@ -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 diff --git a/app/views/standing_order_mailer/confirmation_email.html.haml b/app/views/standing_order_mailer/confirmation_email.html.haml new file mode 100644 index 0000000000..cf741e7542 --- /dev/null +++ b/app/views/standing_order_mailer/confirmation_email.html.haml @@ -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   +%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' diff --git a/app/views/standing_order_mailer/empty_email.html.haml b/app/views/standing_order_mailer/empty_email.html.haml new file mode 100644 index 0000000000..baab7689e6 --- /dev/null +++ b/app/views/standing_order_mailer/empty_email.html.haml @@ -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   +%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' diff --git a/app/views/standing_order_mailer/placement_email.html.haml b/app/views/standing_order_mailer/placement_email.html.haml new file mode 100644 index 0000000000..77ce8f9880 --- /dev/null +++ b/app/views/standing_order_mailer/placement_email.html.haml @@ -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   +%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' diff --git a/spec/jobs/standing_order_placement_job_spec.rb b/spec/jobs/standing_order_placement_job_spec.rb index 7c0cbad254..27141101f1 100644 --- a/spec/jobs/standing_order_placement_job_spec.rb +++ b/spec/jobs/standing_order_placement_job_spec.rb @@ -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 diff --git a/spec/mailers/order_mailer_spec.rb b/spec/mailers/order_mailer_spec.rb index f786dbcff4..49de985325 100644 --- a/spec/mailers/order_mailer_spec.rb +++ b/spec/mailers/order_mailer_spec.rb @@ -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 diff --git a/spec/mailers/standing_order_mailer_spec.rb b/spec/mailers/standing_order_mailer_spec.rb new file mode 100644 index 0000000000..3977765a5d --- /dev/null +++ b/spec/mailers/standing_order_mailer_spec.rb @@ -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