Class: Heya::CampaignMailer

Inherits:
ApplicationMailer show all
Defined in:
app/mailers/heya/campaign_mailer.rb

Constant Summary collapse

DEFAULT_LAYOUT =
"heya/campaign_mailer"

Instance Method Summary collapse

Instance Method Details

#buildObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/mailers/heya/campaign_mailer.rb', line 7

def build
  user = params.fetch(:user)
  step = params.fetch(:step)

  campaign_name = step.campaign_name.underscore
  step_name = step.name.underscore

  from = step.params.fetch("from")
  from = from.call(user) if from.respond_to?(:call)

  bcc = step.params.fetch("bcc", nil)
  bcc = bcc.call(user) if bcc.respond_to?(:call)

  reply_to = step.params.fetch("reply_to", nil)
  reply_to = reply_to.call(user) if reply_to.respond_to?(:call)

  subject = step.params.fetch("subject") {
    I18n.t("#{campaign_name}.#{step_name}.subject", **attributes_for(user))
  }
  subject = subject.call(user) if subject.respond_to?(:call)

  instance_variable_set(:"@#{user.model_name.element}", user)
  instance_variable_set(:@campaign_name, campaign_name)

  headers_params = step.params.fetch("headers", nil)
  headers(headers_params) if headers_params

  mail(
    from: from,
    bcc: bcc,
    reply_to: reply_to,
    to: to_address(user, step),
    subject: subject,
    template_path: "heya/campaign_mailer/#{campaign_name}",
    template_name: step_name
  )
end