所以我已经构建了一个发送电子邮件的通知程序,这些电子邮件是从它所依赖的模型中触发的。一切正常。我只是想知道如何通过我设置的不同方法传递不同的主题(例如:友情提醒 - 发票到期,警告 - 您的帐户将被暂停)。
def send_notice(manual_invoice_id)
@invoice = Billing::ManualInvoice.find(manual_invoice_id)
@subscription = @invoice.subscription
filename = "Invoice - #{@invoice.due_on.strftime("%m-%d-%Y")}.pdf"
attachments[filename] = { mime_type: 'application/pdf',
content: @invoice.generate_pdf! }
mail(to: @invoice.subscription.owner.billing_emails.join("; "), subject: "These should all be different") do |format|
format.text
format.html
end
end
def suspended_email_1(manual_invoice_id)
send_notice
end
def suspended_email_2(manual_invoice_id)
send_notice
end
def suspended_email_3(manual_invoice_id)
send_notice
end
def suspended_email_4(manual_invoice_id)
send_notice
end
def suspended_email_5(manual_invoice_id)
send_notice
end
end
我尝试使用switch / case语句,但这似乎只会增加噪音。我想知道是否应该将另一个参数传递给send_notice
方法。
答案 0 :(得分:1)
为什么不在send_notice方法中添加额外的参数?像这样:
def send_notice(manual_invoice_id, subj)
@invoice = Billing::ManualInvoice.find(manual_invoice_id)
@subscription = @invoice.subscription
filename = "Invoice - #{@invoice.due_on.strftime("%m-%d-%Y")}.pdf"
attachments[filename] = { mime_type: 'application/pdf',
content: @invoice.generate_pdf! }
mail(to: @invoice.subscription.owner.billing_emails.join("; "), subject: subj) do |format|
format.text
format.html
end
end
然后你可以用不同的方法来调用它:
def suspended_email_1(manual_invoice_id)
send_notice(manual_invoice_id, "Friendly Reminder - Invoice Due, Warning - Your account will be suspended")
end