Skip to content

Commit

Permalink
WIP: Part 2
Browse files Browse the repository at this point in the history
Continuing to delve into this.

Collected notes in the wiki on GH.

I think I have a better or at least different way to prepend the
mailer_controller preview method for our purposes.

We want to check if the email is going to be delivered by Notify and
render a different preview when that is the case.

The main blocker right now is not having a real Notify account to test
with - we call the api to generate a preview, which is what we want to
show in the Rails preview, but without a real API key I can't get a
handle on that.
  • Loading branch information
mec committed Dec 22, 2023
1 parent bf7a2b2 commit 230dfa1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/mail/notify/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ def deliver!(mail)
end

def preview(mail)
debugger
personalisation = Personalisation.new(mail).to_h
template_id = mail[:template_id].to_s
client.generate_template_preview(template_id, personalisation: personalisation)
#client.generate_template_preview(template_id, personalisation: personalisation)
OpenStruct.new(html: "<h1>Hej!</h1>")
end

private
Expand Down
26 changes: 17 additions & 9 deletions lib/mail/notify/mailers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ module Mail
module Notify
module MailersController
def preview
@email_action = File.basename(params[:path])
return super unless @preview.email_exists?(@email_action)

# this methods overides the built in one
# https://www.rubydoc.info/docs/rails/Rails/MailersController#preview-instance_method
#
# what kind of mailer is trying to be previewed
# if it is a Notify one, we need to do something different
@email_action = File.basename(params[:path])
@email = @preview.call(@email_action, params)

return super unless notify?

return render_part if params[:part]

render_preview_wrapper
if @email.delivery_method.is_a?(Mail::Notify::DeliveryMethod)
# because we have two types, a view or template, we have to do different
# things
debugger
response.content_type = "text/html"
debugger
@part = @email.find_first_mime_type(:html)
render action: "email", layout: false, formats: %i[html]
else
super
end
end

private
Expand All @@ -23,8 +33,6 @@ def render_part
# the `govuk_notify_layout` layout
append_view_path(__dir__)

response.content_type = "text/html"
render html: @email.preview.html.html_safe, layout: "govuk_notify_layout"
end

def render_preview_wrapper
Expand Down
1 change: 1 addition & 0 deletions lib/mail/notify/rails/mailers/notify_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My Email
6 changes: 6 additions & 0 deletions lib/mail/notify/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ 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)
end

initializer "mail-notify.action_controller" do
ActiveSupport.on_load(:action_controller, run_once: true) do
Rails::MailersController.prepend(Mail::Notify::MailersController)
end
end
end
end
end

0 comments on commit 230dfa1

Please sign in to comment.