|
| 1 | +module ActionMailer |
| 2 | + # Provides the option to parameterize mailers in other to share ivar setup, processing, and common headers. |
| 3 | + # |
| 4 | + # Consider this example that does not use parameterization: |
| 5 | + # |
| 6 | + # class InvitationsMailer < ApplicationMailer |
| 7 | + # def account_invitation(inviter, invitee) |
| 8 | + # @account = inviter.account |
| 9 | + # @inviter = inviter |
| 10 | + # @invitee = invitee |
| 11 | + # |
| 12 | + # subject = "#{@inviter.name} invited you to their Basecamp (#{@account.name})" |
| 13 | + # |
| 14 | + # mail \ |
| 15 | + # subject: subject, |
| 16 | + # to: invitee.email_address, |
| 17 | + # from: common_address(inviter), |
| 18 | + # reply_to: inviter.email_address_with_name |
| 19 | + # end |
| 20 | + # |
| 21 | + # def project_invitation(project, inviter, invitee) |
| 22 | + # @account = inviter.account |
| 23 | + # @project = project |
| 24 | + # @inviter = inviter |
| 25 | + # @invitee = invitee |
| 26 | + # @summarizer = ProjectInvitationSummarizer.new(@project.bucket) |
| 27 | + # |
| 28 | + # subject = "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})" |
| 29 | + # |
| 30 | + # mail \ |
| 31 | + # subject: subject, |
| 32 | + # to: invitee.email_address, |
| 33 | + # from: common_address(inviter), |
| 34 | + # reply_to: inviter.email_address_with_name |
| 35 | + # end |
| 36 | + # |
| 37 | + # def bulk_project_invitation(projects, inviter, invitee) |
| 38 | + # @account = inviter.account |
| 39 | + # @projects = projects.sort_by(&:name) |
| 40 | + # @inviter = inviter |
| 41 | + # @invitee = invitee |
| 42 | + # |
| 43 | + # subject = "#{@inviter.name.familiar} added you to some new stuff in Basecamp (#{@account.name})" |
| 44 | + # |
| 45 | + # mail \ |
| 46 | + # subject: subject, |
| 47 | + # to: invitee.email_address, |
| 48 | + # from: common_address(inviter), |
| 49 | + # reply_to: inviter.email_address_with_name |
| 50 | + # end |
| 51 | + # end |
| 52 | + # |
| 53 | + # InvitationsMailer.account_invitation(person_a, person_b).deliver_later |
| 54 | + # |
| 55 | + # Using parameterized mailers, this can be rewritten as: |
| 56 | + # |
| 57 | + # class InvitationsMailer < ApplicationMailer |
| 58 | + # before_action { @inviter, @invitee = params[:inviter], params[:invitee] } |
| 59 | + # before_action { @account = params[:inviter].account } |
| 60 | + # |
| 61 | + # default to: -> { @invitee.email_address }, |
| 62 | + # from: -> { common_address(@inviter) }, |
| 63 | + # reply_to: -> { @inviter.email_address_with_name } |
| 64 | + # |
| 65 | + # def account_invitation |
| 66 | + # mail subject: "#{@inviter.name} invited you to their Basecamp (#{@account.name})" |
| 67 | + # end |
| 68 | + # |
| 69 | + # def project_invitation |
| 70 | + # @project = params[:project] |
| 71 | + # @summarizer = ProjectInvitationSummarizer.new(@project.bucket) |
| 72 | + # |
| 73 | + # mail subject: "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})" |
| 74 | + # end |
| 75 | + # |
| 76 | + # def bulk_project_invitation |
| 77 | + # @projects = params[:projects].sort_by(&:name) |
| 78 | + # |
| 79 | + # mail subject: "#{@inviter.name.familiar} added you to some new stuff in Basecamp (#{@account.name})" |
| 80 | + # end |
| 81 | + # end |
| 82 | + # |
| 83 | + # InvitationsMailer.with(inviter: person_a, invitee: person_b).account_invitation.deliver_later |
| 84 | + # |
| 85 | + # That's a big improvement! It's also fully backwards compatible. So you can start to gradually transition |
| 86 | + # mailers that stand to benefit the most from parameterization one by one and leave the others behind. |
| 87 | + module Parameterized |
| 88 | + extend ActiveSupport::Concern |
| 89 | + |
| 90 | + included do |
| 91 | + attr_accessor :params |
| 92 | + end |
| 93 | + |
| 94 | + class_methods do |
| 95 | + def with(params) |
| 96 | + ActionMailer::Parameterized::Mailer.new(self, params) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + class Mailer |
| 101 | + def initialize(mailer, params) |
| 102 | + @mailer, @params = mailer, params |
| 103 | + end |
| 104 | + |
| 105 | + def method_missing(method_name, *args) |
| 106 | + if @mailer.action_methods.include?(method_name.to_s) |
| 107 | + ActionMailer::Parameterized::MessageDelivery.new(@mailer, method_name, *args).tap { |pmd| pmd.params = @params } |
| 108 | + else |
| 109 | + super |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + class MessageDelivery < ActionMailer::MessageDelivery |
| 115 | + attr_accessor :params |
| 116 | + |
| 117 | + private |
| 118 | + def processed_mailer |
| 119 | + @processed_mailer ||= @mailer_class.new.tap do |mailer| |
| 120 | + mailer.params = params |
| 121 | + mailer.process @action, *@args |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + def enqueue_delivery(delivery_method, options = {}) |
| 126 | + if processed? |
| 127 | + super |
| 128 | + else |
| 129 | + args = @mailer_class.name, @action.to_s, delivery_method.to_s, @params, *@args |
| 130 | + ActionMailer::Parameterized::DeliveryJob.set(options).perform_later(*args) |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + class DeliveryJob < ActionMailer::DeliveryJob # :nodoc: |
| 136 | + def perform(mailer, mail_method, delivery_method, params, *args) #:nodoc: |
| 137 | + mailer.constantize.with(params).public_send(mail_method, *args).send(delivery_method) |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | +end |
0 commit comments