123456789_123456789_123456789_123456789_123456789_

Module: ActionMailer::Parameterized

Relationships & Source Files
Namespace Children
Modules:
Classes:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Defined in: actionmailer/lib/action_mailer/parameterized.rb

Overview

Provides the option to parameterize mailers in order to share instance variable setup, processing, and common headers.

Consider this example that does not use parameterization:

class InvitationsMailer < ApplicationMailer
  def (inviter, invitee)
    @account = inviter.
    @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.
    @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.
    @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.(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]. }

  default to:       -> { @invitee.email_address },
          from:     -> { common_address(@inviter) },
          reply_to: -> { @inviter.email_address_with_name }

  def 
    mail subject: "#{@inviter.name} invited you to their Basecamp (#{@account.name})"
  end

  def project_invitation
    @project    = params[:project]
    @summarizer = ProjectInvitationSummarizer.new(@project.bucket)

    mail subject: "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})"
  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)..deliver_later

Class Method Summary

::ActiveSupport::Concern - Extended

class_methods

Define class methods from given block.

included

Evaluate given block in context of base class, so that you can write class macros here.

prepended

Evaluate given block in context of base class, so that you can write class macros here.

append_features, prepend_features

DSL Calls

included

[ GitHub ]


92
93
94
95
96
97
98
# File 'actionmailer/lib/action_mailer/parameterized.rb', line 92

included do
  attr_writer :params

  def params
    @params ||= {}
  end
end