module Parameterized

Ruby on Rails edge

Since v5.1.7

Available in: v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

Action Mailer Parameterized

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 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)

  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).account_invitation.deliver_later “‘

Namespace

Modules

Extends

Attributes

Methods (defined here)

Used by

Included by (1)

Methods (inherited)

From ActiveSupport::Concern (3)

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close