Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring Mailers from Spree and make order shipped email translatable #5763

Merged
merged 10 commits into from
Aug 5, 2020
26 changes: 26 additions & 0 deletions app/mailers/spree/base_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Spree
class BaseMailer < ActionMailer::Base
# Inline stylesheets
include Roadie::Rails::Automatic

layout 'mailer'

def from_address
Spree::Config[:mails_from]
end

def money(amount)
Spree::Money.new(amount).to_s
end
helper_method :money

protected

def roadie_options
# This lets us specify assets using relative paths in email templates
super.merge(url_options: { host: URI(main_app.root_url).host })
end
end
end
14 changes: 0 additions & 14 deletions app/mailers/spree/base_mailer_decorator.rb

This file was deleted.

68 changes: 68 additions & 0 deletions app/mailers/spree/order_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

module Spree
class OrderMailer < BaseMailer
helper HtmlHelper
helper ::CheckoutHelper
helper SpreeCurrencyHelper
helper OrderHelper
include I18nHelper

def cancel_email(order_or_order_id, resend = false)
@order = find_order(order_or_order_id)
I18n.with_locale valid_locale(@order.user) do
mail(to: @order.email,
from: from_address,
subject: mail_subject(t('spree.order_mailer.cancel_email.subject'), resend))
end
end

def confirm_email_for_customer(order_or_order_id, resend = false)
@order = find_order(order_or_order_id)
I18n.with_locale valid_locale(@order.user) do
subject = mail_subject(t('spree.order_mailer.confirm_email.subject'), resend)
mail(to: @order.email,
from: from_address,
subject: subject,
reply_to: @order.distributor.contact.email)
end
end

def confirm_email_for_shop(order_or_order_id, resend = false)
@order = find_order(order_or_order_id)
I18n.with_locale valid_locale(@order.user) do
subject = mail_subject(t('spree.order_mailer.confirm_email.subject'), resend)
mail(to: @order.distributor.contact.email,
from: from_address,
subject: subject)
end
end

def invoice_email(order_or_order_id, pdf)
@order = find_order(order_or_order_id)
attach_file("invoice-#{@order.number}.pdf", pdf)
I18n.with_locale valid_locale(@order.user) do
mail(to: @order.email,
from: from_address,
subject: mail_subject(t(:invoice), false),
reply_to: @order.distributor.contact.email)
end
end

private

# Finds an order instance from an order or from an order id
def find_order(order_or_order_id)
order_or_order_id.respond_to?(:id) ? order_or_order_id : Spree::Order.find(order_or_order_id)
end

def mail_subject(base_subject, resend)
resend_prefix = (resend ? "[#{t(:resend).upcase}] " : '')
"#{resend_prefix}#{Spree::Config[:site_name]} #{base_subject} ##{@order.number}"
end

def attach_file(filename, file)
attachments[filename] = file if file.present?
end
end
end
64 changes: 0 additions & 64 deletions app/mailers/spree/order_mailer_decorator.rb

This file was deleted.

13 changes: 13 additions & 0 deletions app/mailers/spree/shipment_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Spree
class ShipmentMailer < BaseMailer
def shipped_email(shipment, resend = false)
@shipment = shipment.respond_to?(:id) ? shipment : Spree::Shipment.find(shipment)
subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')
base_subject = t('spree.shipment_mailer.shipped_email.subject')
subject += "#{Spree::Config[:site_name]} #{base_subject} ##{@shipment.order.number}"
mail(to: @shipment.order.email, from: from_address, subject: subject)
end
end
end
11 changes: 11 additions & 0 deletions app/mailers/spree/test_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Spree
class TestMailer < BaseMailer
def test_email(user)
recipient = user.respond_to?(:id) ? user : Spree.user_class.find(user)
subject = "#{Spree::Config[:site_name]} #{t('spree.test_mailer.test_email.subject')}"
mail(to: recipient.email, from: from_address, subject: subject)
end
end
end
30 changes: 30 additions & 0 deletions app/views/spree/shipment_mailer/shipped_email.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%p
= t('.dear_customer')
%p
= t('.instructions')

%p
= "============================================================"
%br
= t('.shipment_summary')
%br
= "============================================================"

%p
- @shipment.manifest.each do |item|
= item.variant.sku
= item.variant.product.name
= item.variant.options_text
%br
= "============================================================"

- if @shipment.tracking
%p
= t('.track_information', tracking: @shipment.tracking)

- if @shipment.tracking_url
%p
= t('.track_link', url: @shipment.tracking_url)

%p
= t('.thanks')
4 changes: 4 additions & 0 deletions app/views/spree/test_mailer/test_email.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= t('.greeting')
= "================"

= t('.message')
38 changes: 26 additions & 12 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3508,6 +3508,32 @@ See the %{link} to find out more about %{sitename}'s features and to start using
invoice_email:
hi: "Hi %{name}"
invoice_attached_text: Please find attached an invoice for your recent order from
user_mailer:
reset_password_instructions:
request_sent_text: |
A request to reset your password has been made.
If you did not make this request, simply ignore this email.
link_text: >
If you did make this request just click the link below:
issue_text: |
If the above URL does not work try copying and pasting it into your browser.
If you continue to have problems please feel free to contact us.
confirmation_instructions:
subject: "Please confirm your OFN account"
shipment_mailer:
shipped_email:
dear_customer: "Dear Customer,"
instructions: "Your order has been shipped"
shipment_summary: "Shipment Summary"
subject: "Shipment Notification"
thanks: "Thank you for your business."
track_information: ! "Tracking Information: %{tracking}"
track_link: ! "Tracking Link: %{url}"
test_mailer:
test_email:
greeting: "Congratulations!"
message: "If you have received this email, then your email settings are correct."
subject: "Test Mail"
order_state:
address: address
adjustments: adjustments
Expand All @@ -3529,18 +3555,6 @@ See the %{link} to find out more about %{sitename}'s features and to start using
ended: ended
paused: paused
canceled: cancelled
user_mailer:
reset_password_instructions:
request_sent_text: |
A request to reset your password has been made.
If you did not make this request, simply ignore this email.
link_text: >
If you did make this request just click the link below:
issue_text: |
If the above URL does not work try copying and pasting it into your browser.
If you continue to have problems please feel free to contact us.
confirmation_instructions:
subject: Please confirm your OFN account
users:
form:
account_settings: Account Settings
Expand Down
74 changes: 74 additions & 0 deletions spec/mailers/order_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,82 @@
# frozen_string_literal: true

require 'spec_helper'

describe Spree::OrderMailer do
include OpenFoodNetwork::EmailHelper

context "basic behaviour" do
let(:order) { build(:order_with_totals_and_distribution) }

context ":from not set explicitly" do
it "falls back to spree config" do
message = Spree::OrderMailer.confirm_email_for_customer(order)
expect(message.from).to eq [Spree::Config[:mails_from]]
end
end

it "doesn't aggressively escape double quotes in confirmation body" do
confirmation_email = Spree::OrderMailer.confirm_email_for_customer(order)
expect(confirmation_email.body).to_not include("&quot;")
end

it "confirm_email_for_customer accepts an order id as an alternative to an Order object" do
expect(Spree::Order).to receive(:find).with(order.id).and_return(order)
expect {
confirmation_email = Spree::OrderMailer.confirm_email_for_customer(order.id)
}.to_not raise_error
end

it "cancel_email accepts an order id as an alternative to an Order object" do
expect(Spree::Order).to receive(:find).with(order.id).and_return(order)
expect {
cancel_email = Spree::OrderMailer.cancel_email(order.id)
}.to_not raise_error
end
end

context "only shows eligible adjustments in emails" do
let(:order) { create(:order_with_totals_and_distribution) }

before do
order.adjustments.create(
label: "Eligible Adjustment",
amount: 10,
eligible: true
)

order.adjustments.create!(
label: "Ineligible Adjustment",
amount: 0,
)
end

let!(:confirmation_email) { Spree::OrderMailer.confirm_email_for_customer(order) }
let!(:cancel_email) { Spree::OrderMailer.cancel_email(order) }

specify do
expect(confirmation_email.body).to_not include("Ineligible Adjustment")
end

specify do
expect(cancel_email.body).to_not include("Ineligible Adjustment")
end
end

context "displays line item price" do
let(:order) { create(:order_with_totals_and_distribution) }

specify do
confirmation_email = Spree::OrderMailer.confirm_email_for_customer(order)
expect(confirmation_email.body).to include("3.00")
end

specify do
cancel_email = Spree::OrderMailer.cancel_email(order)
expect(cancel_email.body).to include("3.00")
end
end

describe "order confimation" do
let(:bill_address) { create(:address) }
let(:distributor_address) { create(:address, address1: "distributor address", city: 'The Shire', zipcode: "1234") }
Expand Down
Loading