refactor(mailer): extract dolist logic into its concern

This commit is contained in:
Colin Darie 2023-01-16 11:34:36 +01:00
parent ae7e3cf887
commit 6615165b2e
4 changed files with 17 additions and 10 deletions

View file

@ -1,4 +1,5 @@
class ApplicationMailer < ActionMailer::Base class ApplicationMailer < ActionMailer::Base
include MailerDolistConcern
include MailerMonitoringConcern include MailerMonitoringConcern
helper :application # gives access to all helpers defined within `application_helper`. helper :application # gives access to all helpers defined within `application_helper`.

View file

@ -0,0 +1,15 @@
module MailerDolistConcern
extend ActiveSupport::Concern
included do
before_action :add_dolist_header
# mandatory for dolist
# used for tracking in Dolist UI
# the delivery_method is yet unknown (:balancer)
# so we add the dolist header for everyone
def add_dolist_header
headers['X-Dolist-Message-Name'] = action_name
end
end
end

View file

@ -2,8 +2,6 @@ module MailerMonitoringConcern
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
before_action :add_dolist_header
# Intercept & log any error, then re-raise so job will retry. # Intercept & log any error, then re-raise so job will retry.
# NOTE: rescue_from order matters, later matchers are tried first. # NOTE: rescue_from order matters, later matchers are tried first.
rescue_from StandardError, with: :log_and_raise_delivery_error rescue_from StandardError, with: :log_and_raise_delivery_error
@ -21,14 +19,6 @@ module MailerMonitoringConcern
end end
end end
# mandatory for dolist
# used for tracking in Dolist UI
# the delivery_method is yet unknown (:balancer)
# so we add the dolist header for everyone
def add_dolist_header
headers['X-Dolist-Message-Name'] = action_name
end
def log_and_raise_delivery_error(exception) def log_and_raise_delivery_error(exception)
EmailEvent.create_from_message!(message, status: "dispatch_error") EmailEvent.create_from_message!(message, status: "dispatch_error")
Sentry.capture_exception(exception, extra: { to: message.to, subject: message.subject }) Sentry.capture_exception(exception, extra: { to: message.to, subject: message.subject })

View file

@ -3,6 +3,7 @@ class DeviseUserMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`. helper :application # gives access to all helpers defined within `application_helper`.
helper MailerHelper helper MailerHelper
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url` include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
include MailerDolistConcern
include MailerMonitoringConcern include MailerMonitoringConcern
layout 'mailers/layout' layout 'mailers/layout'
before_action :add_delivery_method, if: :forced_delivery? before_action :add_delivery_method, if: :forced_delivery?