Reputation: 23556
I need to save information about sending every e-mail to my client in database for further analysis. So I'm trying to do it in Observer but there I need info about invoices. So I've got Mailer:
class ClientMailer < ActionMailer::Base
default :from => "[email protected]"
def remind(client, invoices)
@client = client
@company = @client.company
@invoices = invoices.to_a
@template = t('message.template')
@text = liquid_parse @template
@html = markdown_parse @text
mail(:to => @client.email, :subject => t('message.title')) do |format|
format.html
format.text
end
end
private
def markdown_parse(text)
markdown = Redcarpet::Markdown.new Redcarpet::Render::HTML,
:autolink => true, :space_after_headers => true
markdown.render text
end
def liquid_parse(text)
renderer = Liquid::Template.parse text
renderer.render 'company' => @company, 'invoice' => @invoice, 'client' => @client
end
end
And question is: how to pass @invoices
to ActionMailer observer?
Upvotes: 3
Views: 614
Reputation: 23556
I've done it by myself by adding in mailer:
headers 'X-Invoice-IDs' => @invoices.map(&:id).join(';')
And then in observer
ids = message.header['X-Invoice-IDs'].to_s.split ';'
And there I've got all Invoice IDs in my observer.
EDIT
After thursday meeting of PRUG I came with idea of overloading delivery
method instead of using observer. It will be compatible with Rails 4 and easier to use (as I won't need passing additional header, I will use just instance variable).
Upvotes: 8