2012-02-26 90 views
3

我需要保存有關將數據庫中的每封電子郵件發送到我的客戶端以進一步分析的信息。所以我試圖在Observer中做到這一點,但我需要關於發票的信息。 所以我有梅勒:將其他信息傳遞給ActionMailer Observer

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 

而問題是:如何通過@invoices到的ActionMailer觀察員?

回答

8

我已經通過自己在郵件中加入了這事

headers 'X-Invoice-IDs' => @invoices.map(&:id).join(';') 

然後在觀察者

ids = message.header['X-Invoice-IDs'].to_s.split ';' 

在那裏我已經得到了所有發票的ID在我的觀察。

編輯

PRUG週四會議結束後我帶着超載delivery方法,而不是使用觀察者的想法。它將與Rails 4兼容並且更易於使用(因爲我不需要傳遞額外的頭文件,我將只使用實例變量)。