-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
143 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters