我正在使用邮件gem,为了发送电子邮件,您需要执行以下操作:
Mail.deliver do
from Config.mail['user_name']
to Config.mail['recipients']
subject "Process has completed"
body "Process has completed an import at #{Time.now}."
end
我创建了许多包含该块的不同方法,以便发送不同的电子邮件类型。在重新审视之后,虽然看起来对于每种电子邮件类型,唯一改变的部分是主题和正文。从和保持不变。换句话说,我似乎经常重复自己。
我希望我能有这样的方法:
def send_notification(notification)
Mail.deliver do
from Config.mail['user_name']
to Config.mail['recipients']
<some way I pass in the subject and body here from another method>
end
end
对于只需要包含主题和正文信息的通知类型,我会有一系列其他方法:
def job_start_notification
subject "Started"
body "Stuff Started"
end
我正在考虑将job_start_notification
方法作为lambda传递到send_notification
方法但由于主题和正文是方法而无法工作,并且由于某种原因它们不是&#39 ;在Mail.deliver
块的上下文中调用。
有关如何实现这一目标的任何想法?
答案 0 :(得分:1)
最简单的方法是:
def send_notification(subject, body)
Mail.deliver do
from Config.mail['user_name']
to Config.mail['recipients']
subject subject
body body
end
end
send_notification "foo", "your foo is here"
另一个(未经测试,不在我的Ruby附近):
def send_notification(subject, body)
Mail.deliver do
from Config.mail['user_name']
to Config.mail['recipients']
yield
end
end
send_notification do
subject "foo"
body "your foo is here"
end