-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version 2
- Loading branch information
Showing
30 changed files
with
764 additions
and
478 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
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mail | ||
module Notify | ||
class Engine < Rails::Engine | ||
initializer "mail-notify.add_delivery_method", before: "action_mailer.set_configs" do | ||
ActionMailer::Base.add_delivery_method(:notify, Mail::Notify::DeliveryMethod) | ||
|
||
config.action_mailer.preview_interceptors = [:mail_notify_preview_interceptor] | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
## | ||
# This is a ActionMailer interceptor class for previews from the Notifications API | ||
# | ||
# See ActionMailer::Base: | ||
# | ||
# https://github.com/rails/rails/blob/v5.2.8.1/actionmailer/lib/action_mailer/base.rb#L367 | ||
|
||
class MailNotifyPreviewInterceptor | ||
## | ||
# ActionMailer call back when a preview is being generated. | ||
# | ||
# Transforms the content of the passed in message. | ||
|
||
def self.previewing_email(message) | ||
new(message).transform! | ||
end | ||
|
||
## | ||
# Creates a new MailNotifyPreviewInterceptor ready for use. | ||
|
||
def initialize(message) | ||
@message = message | ||
end | ||
|
||
## | ||
# Transforms the content of the message to that from the Notifications API preview. | ||
# | ||
# The html is wrapped in a layout and rendered by the MailNotifyPreviewsControllers renderer. | ||
|
||
def transform! | ||
# the messages delivery method will be our Mail::Notify::DeliveryMethod and have the `preview` method. | ||
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 | ||
# rendering in Rails without a controller gets far too complicated, instead | ||
# we rely on this empty controller to do it for us. | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
## | ||
# Empty controller used by MailNotifyPreviewInterceptor to render html. | ||
|
||
class MailNotifyPreviewsController < ActionController::Base; end |
Oops, something went wrong.