Skip to content

Commit

Permalink
WIP: v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mec committed Feb 20, 2024
1 parent 55113d3 commit a87c44a
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 57 deletions.
2 changes: 2 additions & 0 deletions lib/mail/notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require "mail/notify/personalisation"
require "mail/notify/mailer"
require "mail/notify/message"
require "mail/notify/mail_notify_preview_interceptor"
require "mail/notify/mail_notify_previews_controller"

Mail::Message.include Mail::Notify::Message

Expand Down
15 changes: 8 additions & 7 deletions lib/mail/notify/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ def initialize(settings)
@settings = settings
end

def deliver!(mail)
@mail = mail
@personalisation = Personalisation.new(mail)
def deliver!(message)
@mail = message
@personalisation = Personalisation.new(message)
send_email
end

def preview(mail)
personalisation = Personalisation.new(mail).to_h
template_id = mail[:template_id].to_s
def preview(message)
template_id = message.template_id
personalisation = message.personalisation || {}

client.generate_template_preview(template_id, personalisation: personalisation)
end

Expand All @@ -32,7 +33,7 @@ def client
def email_params
{
email_address: @mail.to.first,
template_id: @mail[:template_id].to_s,
template_id: @mail.template_id,
personalisation: @personalisation.to_h,
email_reply_to_id: optional_param(:reply_to_id),
reference: optional_param(:reference)
Expand Down
29 changes: 29 additions & 0 deletions lib/mail/notify/mail_notify_preview_interceptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class MailNotifyPreviewInterceptor
def self.previewing_email(message)
Rails.logger.info("[Mail Notify] intercepting preview")

new(message).transform!
end

def initialize(message)
@message = message
@template_id = @message.template_id
@personalisation = @message.personalisation
end

def transform!
preview = @message.delivery_method.preview(@message)

@message.subject = preview.subject
@message.html_part.body = renderer.render html: preview.html.html_safe, layout: "govuk_notify_layout"
@message.text_part.body = preview.body

@message
end

private

def renderer
MailNotifyPreviewsController.renderer
end
end
24 changes: 22 additions & 2 deletions lib/mail/notify/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@
module Mail
module Notify
class Mailer < ActionMailer::Base
default from: "[email protected]"

def view_mail(template_id, headers)
raise ArgumentError, "You must specify a template ID" if template_id.blank?
raise ArgumentError, "You must specify a subject in the headers" if headers[:subject].blank?

subject = headers[:subject]
body = mail(headers).body.raw_source

message.template_id = template_id

mail(headers.merge(template_id: template_id))
message.personalisation = {subject: subject, body: body}

mail(headers) do |format|
format.text { nil }
format.html { nil }
end
end

def template_mail(template_id, headers)
raise ArgumentError, "You must specify a template ID" if template_id.blank?

mail(headers.merge(body: "", subject: "", template_id: template_id))
message.personalisation = headers[:personalisation]

message.template_id = template_id

mail(headers) do |format|
format.text { nil }
format.html { nil }
end
end

def blank_allowed(value)
Expand Down
4 changes: 1 addition & 3 deletions lib/mail/notify/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
module Mail
module Notify
module Message
def preview
delivery_method.preview(self) if delivery_method.respond_to?(:preview)
end
attr_accessor :template_id, :personalisation
end
end
end
8 changes: 6 additions & 2 deletions lib/mail/notify/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# frozen_string_literal: true

require "rails/mailers_controller"

module Mail
module Notify
class Railtie < Rails::Engine
initializer "mail-notify.add_delivery_method", before: "action_mailer.set_configs" do
ActionMailer::Base.add_delivery_method(:notify, Mail::Notify::DeliveryMethod)

if config.action_mailer.preview_interceptors.present?
config.action_mailer.preview_interceptors << :mail_notify_preview_interceptor
else
config.action_mailer.preview_interceptors = [:mail_notify_preview_interceptor]
end
end
end
end
Expand Down
Binary file modified spec/dummy/db/test.sqlite3
Binary file not shown.
30 changes: 0 additions & 30 deletions spec/mail/message_spec.rb

This file was deleted.

72 changes: 72 additions & 0 deletions spec/mail/notify/mail_notify_preview_interceptor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe MailNotifyPreviewInterceptor do
describe "#transform!" do
it "sets the subject of the message" do
preview_message = double(
"preview message",
subject: "Preview subject",
html: "<p>Preview body</p>",
body: "Preview body"
)
delivery_method = double("delivery method", preview: preview_message)

message = Mail::Message.new(subject: "Original subject")
message.add_part(Mail::Part.new(content_type: "text/html"))
message.add_part(Mail::Part.new)

allow(message).to receive(:delivery_method).and_return(delivery_method)

described_class.new(message).transform!

expect(message.subject).to eql preview_message.subject
end

it "sets the plain text body of the message" do
preview_message = double(
"preview message",
subject: "Preview subject",
html: "<p>Preview body</p>",
body: "Preview body"
)
delivery_method = double("delivery method", preview: preview_message)

message = fake_message(delivery_method)

described_class.new(message).transform!

expect(message.text_part.body.raw_source).to eql preview_message.body
end

it "sets the html body of the message" do
preview_message = double(
"preview message",
subject: "Preview subject",
html: "<p>Preview body</p>",
body: "Preview body"
)
delivery_method = double("delivery method", preview: preview_message)

message = Mail::Message.new(subject: "Original subject")
message.add_part(Mail::Part.new(content_type: "text/html"))
message.add_part(Mail::Part.new)

allow(message).to receive(:delivery_method).and_return(delivery_method)

described_class.new(message).transform!

expect(message.html_part.body.raw_source).to include(preview_message.html)
end
end

def fake_message(delivery_method)
message = Mail::Message.new(subject: "Original subject")
message.add_part(Mail::Part.new(content_type: "text/html"))
message.add_part(Mail::Part.new)

allow(message).to receive(:delivery_method).and_return(delivery_method)
message
end
end
15 changes: 2 additions & 13 deletions spec/mail/notify/mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
let(:mailer) { Mail::Notify::Mailer.new }

context "with a view" do
it "sends the template" do
expect(mailer).to receive(:mail).with({template_id: "foo", bar: "baz"})
mailer.view_mail("foo", bar: "baz")
end
it "sends the template"

it "raises an error if the template ID is blank" do
expect { mailer.view_mail("", bar: "baz") }.to raise_error(
Expand All @@ -20,15 +17,7 @@
end

context "with a template" do
it "sends a blank body and subject" do
expect(mailer).to receive(:mail).with({
template_id: "foo",
bar: "baz",
body: "",
subject: ""
})
mailer.template_mail("foo", bar: "baz")
end
it "sends a blank body and subject"

it "raises an error if the template ID is blank" do
expect { mailer.template_mail("", bar: "baz") }.to raise_error(
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SimpleCov.start

require "bundler/setup"
require "action_controller"
require "action_mailer"
require "pry"
require "webmock/rspec"
Expand Down

0 comments on commit a87c44a

Please sign in to comment.