我正在使用SendGrid模块(require sendgrid-ruby),但是在任何地方放置这样的代码并不是很枯燥。
client = SendGrid::Client.new(api_key: SENDGRID_KEY)
mail = SendGrid::Mail.new do |m|
m.to = 'js@lso.com'
m.from = 'no-reply@gsdfdo.com'
m.subject = 'Boo'
m.html = " "
m.text = " "
end我的想法是创建一个模块MyModule,创建一个名为standardMail的方法。
module MyModule
require 'sendgrid-ruby'
def standardMail
mail = SendGrid::Mail.new do |m|
m.to = 'js@lso.com'
m.from = 'no-reply@gsdfdo.com'
m.subject = 'Boo'
m.html = " "
m.text = " "
end
return mail
end
end然后,我可以使用standardMail (通过include MyModule)返回邮件对象设置并准备就绪。我的问题是,您是否需要模块中的模块(,也就是在我的自定义模块中需要sendgrid)。
class Thing
include MyModule
def doMail
mail = Thing.standardMail
end
end发布于 2016-04-11 02:56:01
我不知道为什么在这种情况下需要一个模块--扩展Sendgrid的默认行为要容易得多:
class MyMailer < SendGrid::Mail
def initialize(params)
@to = 'js@lso.com'
@from = 'no-reply@gsdfdo.com'
@subject = 'Boo'
@html = " "
@text = " "
super
end
end或者您可以直接覆盖:
class SendGrid::Mail
def initialize(params)
@to = 'js@lso.com'
@from = 'no-reply@gsdfdo.com'
@subject = 'Boo'
@html = " "
@text = " "
super
end
endhttps://stackoverflow.com/questions/36538974
复制相似问题