Class: MailNotifyPreviewInterceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/mail/notify/mail_notify_preview_interceptor.rb

Overview

This is a ActionMailer interceptor class for previews from the Notifications API

See ActionMailer::Base:

github.com/rails/rails/blob/v5.2.8.1/actionmailer/lib/action_mailer/base.rb#L367

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ MailNotifyPreviewInterceptor

Creates a new MailNotifyPreviewInterceptor ready for use.



23
24
25
# File 'lib/mail/notify/mail_notify_preview_interceptor.rb', line 23

def initialize(message)
  @message = message
end

Class Method Details

.previewing_email(message) ⇒ Object

ActionMailer call back when a preview is being generated.

Transforms the content of the passed in message.



16
17
18
# File 'lib/mail/notify/mail_notify_preview_interceptor.rb', line 16

def self.previewing_email(message)
  new(message).transform!
end

Instance Method Details

#transform!Object

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.



32
33
34
35
36
37
38
39
40
41
# File 'lib/mail/notify/mail_notify_preview_interceptor.rb', line 32

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