我有一个应用程序,在其中我可以创建发票,渲染成pdf和发送给客户。我的邮件中有两个动作:
class InvoiceMailer < ActionMailer::Base
default from: "from@example.com"
def send_invoice_reminder(invoice)
@invoice = invoice
attach_invoice
mail :subject => "Invoice reminder", :to => invoice.customer.email
end
def send_invoice(invoice)
@invoice = invoice
attach_invoice
mail :subject => "Your Invoice", :to => invoice.customer.email
end
protected
def attach_invoice
attachments["invoice.pdf"] = WickedPdf.new.pdf_from_string(
render_to_string(:pdf => "invoice",:template => 'admin/invoices/show.pdf.erb')
)
end
end现在我想通过Sidkiq工人发送这个。但我有疑问。我是否需要两个帮手:
或者一个工人就够了?
发布于 2014-08-25 09:34:33
我想你可以用一个工人来做这两件事,因为在这两件事上,你的工作基本上是一样的。
它看起来像:
class InvoiceMailer < ActionMailer::Base
default from: "from@example.com"
def send_invoice(invoice, subject)
@invoice = invoice
attachments["invoice.pdf"] = pdf
mail subject: subject, to: invoice.customer.email
end
private
def pdf
WickedPdf.new.pdf_from_string render_to_string(
pdf: "invoice",
template: 'admin/invoices/show.pdf.erb'
)
end
end
class InvoceEmailSender
include Sidekiq::Worker
def perform(invoice, subject)
InvoiceMailer.send_invoice(invoice, subject).deliver
end
end
InvoiceEmailSender.perform_async invoice, 'Your Invoice'
InvoiceEmailSender.perform_async invoice, 'Invoice Reminder'https://stackoverflow.com/questions/25482797
复制相似问题