2017-07-23 11:17:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-28 05:20:46 -05:00
|
|
|
module ActionMailer
|
2017-01-28 13:00:54 -05:00
|
|
|
# Provides the option to parameterize mailers in order to share instance variable
|
|
|
|
# setup, processing, and common headers.
|
2017-01-28 05:20:46 -05:00
|
|
|
#
|
|
|
|
# Consider this example that does not use parameterization:
|
|
|
|
#
|
|
|
|
# class InvitationsMailer < ApplicationMailer
|
|
|
|
# def account_invitation(inviter, invitee)
|
|
|
|
# @account = inviter.account
|
|
|
|
# @inviter = inviter
|
|
|
|
# @invitee = invitee
|
|
|
|
#
|
|
|
|
# subject = "#{@inviter.name} invited you to their Basecamp (#{@account.name})"
|
|
|
|
#
|
|
|
|
# mail \
|
|
|
|
# subject: subject,
|
|
|
|
# to: invitee.email_address,
|
|
|
|
# from: common_address(inviter),
|
|
|
|
# reply_to: inviter.email_address_with_name
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def project_invitation(project, inviter, invitee)
|
|
|
|
# @account = inviter.account
|
|
|
|
# @project = project
|
|
|
|
# @inviter = inviter
|
|
|
|
# @invitee = invitee
|
|
|
|
# @summarizer = ProjectInvitationSummarizer.new(@project.bucket)
|
|
|
|
#
|
|
|
|
# subject = "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})"
|
|
|
|
#
|
|
|
|
# mail \
|
|
|
|
# subject: subject,
|
|
|
|
# to: invitee.email_address,
|
|
|
|
# from: common_address(inviter),
|
|
|
|
# reply_to: inviter.email_address_with_name
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def bulk_project_invitation(projects, inviter, invitee)
|
|
|
|
# @account = inviter.account
|
|
|
|
# @projects = projects.sort_by(&:name)
|
|
|
|
# @inviter = inviter
|
|
|
|
# @invitee = invitee
|
|
|
|
#
|
|
|
|
# subject = "#{@inviter.name.familiar} added you to some new stuff in Basecamp (#{@account.name})"
|
|
|
|
#
|
|
|
|
# mail \
|
|
|
|
# subject: subject,
|
|
|
|
# to: invitee.email_address,
|
|
|
|
# from: common_address(inviter),
|
|
|
|
# reply_to: inviter.email_address_with_name
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# InvitationsMailer.account_invitation(person_a, person_b).deliver_later
|
|
|
|
#
|
|
|
|
# Using parameterized mailers, this can be rewritten as:
|
|
|
|
#
|
|
|
|
# class InvitationsMailer < ApplicationMailer
|
|
|
|
# before_action { @inviter, @invitee = params[:inviter], params[:invitee] }
|
|
|
|
# before_action { @account = params[:inviter].account }
|
|
|
|
#
|
|
|
|
# default to: -> { @invitee.email_address },
|
|
|
|
# from: -> { common_address(@inviter) },
|
|
|
|
# reply_to: -> { @inviter.email_address_with_name }
|
|
|
|
#
|
|
|
|
# def account_invitation
|
|
|
|
# mail subject: "#{@inviter.name} invited you to their Basecamp (#{@account.name})"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def project_invitation
|
|
|
|
# @project = params[:project]
|
|
|
|
# @summarizer = ProjectInvitationSummarizer.new(@project.bucket)
|
|
|
|
#
|
2017-01-30 13:38:06 -05:00
|
|
|
# mail subject: "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})"
|
2017-01-28 05:20:46 -05:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def bulk_project_invitation
|
|
|
|
# @projects = params[:projects].sort_by(&:name)
|
|
|
|
#
|
|
|
|
# mail subject: "#{@inviter.name.familiar} added you to some new stuff in Basecamp (#{@account.name})"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# InvitationsMailer.with(inviter: person_a, invitee: person_b).account_invitation.deliver_later
|
|
|
|
module Parameterized
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
attr_accessor :params
|
|
|
|
end
|
|
|
|
|
2017-01-30 13:38:12 -05:00
|
|
|
module ClassMethods
|
|
|
|
# Provide the parameters to the mailer in order to use them in the instance methods and callbacks.
|
|
|
|
#
|
|
|
|
# InvitationsMailer.with(inviter: person_a, invitee: person_b).account_invitation.deliver_later
|
|
|
|
#
|
|
|
|
# See Parameterized documentation for full example.
|
2017-01-28 05:20:46 -05:00
|
|
|
def with(params)
|
|
|
|
ActionMailer::Parameterized::Mailer.new(self, params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-30 13:02:11 -05:00
|
|
|
class Mailer # :nodoc:
|
2017-01-28 05:20:46 -05:00
|
|
|
def initialize(mailer, params)
|
|
|
|
@mailer, @params = mailer, params
|
|
|
|
end
|
|
|
|
|
2017-01-30 13:28:14 -05:00
|
|
|
private
|
2021-04-12 14:49:54 -04:00
|
|
|
def method_missing(method_name, *args)
|
2017-01-30 13:28:14 -05:00
|
|
|
if @mailer.action_methods.include?(method_name.to_s)
|
|
|
|
ActionMailer::Parameterized::MessageDelivery.new(@mailer, method_name, @params, *args)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2021-04-12 14:49:54 -04:00
|
|
|
ruby2_keywords(:method_missing)
|
2017-01-30 13:28:14 -05:00
|
|
|
|
|
|
|
def respond_to_missing?(method, include_all = false)
|
|
|
|
@mailer.respond_to?(method, include_all)
|
2017-01-28 05:20:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-12 15:20:07 -04:00
|
|
|
class DeliveryJob < ActionMailer::DeliveryJob # :nodoc:
|
2021-04-12 14:49:54 -04:00
|
|
|
def perform(mailer, mail_method, delivery_method, params, *args)
|
2020-05-12 15:20:07 -04:00
|
|
|
mailer.constantize.with(params).public_send(mail_method, *args).send(delivery_method)
|
|
|
|
end
|
2021-04-12 14:49:54 -04:00
|
|
|
ruby2_keywords(:perform)
|
2020-05-12 15:20:07 -04:00
|
|
|
end
|
|
|
|
|
2017-01-30 13:02:11 -05:00
|
|
|
class MessageDelivery < ActionMailer::MessageDelivery # :nodoc:
|
2021-04-12 14:49:54 -04:00
|
|
|
def initialize(mailer_class, action, params, *args)
|
2017-01-30 13:02:30 -05:00
|
|
|
super(mailer_class, action, *args)
|
|
|
|
@params = params
|
|
|
|
end
|
2021-04-12 14:49:54 -04:00
|
|
|
ruby2_keywords(:initialize)
|
2017-01-28 05:20:46 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
def processed_mailer
|
|
|
|
@processed_mailer ||= @mailer_class.new.tap do |mailer|
|
2017-01-30 13:02:30 -05:00
|
|
|
mailer.params = @params
|
2017-01-28 05:20:46 -05:00
|
|
|
mailer.process @action, *@args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_delivery(delivery_method, options = {})
|
|
|
|
if processed?
|
|
|
|
super
|
|
|
|
else
|
2020-05-12 15:20:07 -04:00
|
|
|
job = delivery_job_class
|
|
|
|
|
|
|
|
if job <= MailDeliveryJob
|
|
|
|
job.set(options).perform_later(
|
|
|
|
@mailer_class.name, @action.to_s, delivery_method.to_s, params: @params, args: @args)
|
|
|
|
else
|
|
|
|
job.set(options).perform_later(
|
|
|
|
@mailer_class.name, @action.to_s, delivery_method.to_s, @params, *@args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delivery_job_class
|
|
|
|
if @mailer_class.delivery_job <= MailDeliveryJob
|
|
|
|
@mailer_class.delivery_job
|
|
|
|
else
|
|
|
|
Parameterized::DeliveryJob
|
2017-01-28 05:20:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|