Rails Mailer最佳实践 - 控制器或after_create回调? [英] Rails Mailer Best Practice - controller or after_create callback?

查看:131
本文介绍了Rails Mailer最佳实践 - 控制器或after_create回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在普通Ruby on Rails 3/4应用程序中,创建模型后有两种方式可以传送邮件:



1)发送邮件直接从控制器的创建动作

 #listing_controller.rb 

def create
@ listing.create(params [:listing])
如果@ listing.save
ListingMailer.new_listing_notice(@listing).deliver
format.html {redirect_to @listing}
...
end
end

2)发送来自模特回调的邮件

 #listing.rb 


类列表
after_create:notify
...
def notify
ListingMailer.new_listing_notice(self).deliver
end
end

目前有关哪种方式更好的共识?从控制器发送更多的控制权,但是如果一个邮件总是被发送有什么理由不使用回调?这主要是一个风格的问题,还是有其他重要的问题?

解决方案

一般来说,维护代码更加困难在模型中的_ * 钩子后面有。当然有一些情况下,使用回调是非常合理的(计算校验和,例如,应该在某些应用程序中一直都这样做),但这些情况是规则的例外。



在您的电子邮件示例中,回调方法有一些缺点:



邮件接口和列表模型由于不同的原因而改变( 单一责任原则)。



例如,您希望使用特殊队列发送电子邮件。与队列通信的界面不应以任何方式影响列表模型的构建方式。



您的应用程序始终不需要电子邮件。 / h3>

电子邮件只是与外界互动的手段之一。并不是所有的业务逻辑都将永远需要与外界联系在一起。其中一个示例,如 apneadiving 所提到的是导入。另一个例子是控制台界面(当您正在使用rails控制台时,是否要发送电子邮件?)



测试很困难。



这更多是1和2的结果,但是在钩子之后测试随着时间的推移越来越困难。在测试列表模型时,需要mo the邮件程序,使得测试不清楚,并且更改难度。


In common Ruby on Rails 3/4 apps, there are two options to deliver mail after the creation of a model:

1) Send the mail directly from the controller's create action

#listings_controller.rb

def create
    @listing.create(params[:listing])
    if @listing.save
        ListingMailer.new_listing_notice(@listing).deliver
        format.html {redirect_to @listing}
        ...
    end
end

or 2) Send the mail from a Model Callback

#listing.rb


class Listing
    after_create :notify
    ...
    def notify
        ListingMailer.new_listing_notice(self).deliver
    end
end

Is there a current consensus about which way is better? Sending from the controller gives more control, but if a mail will always be sent is there any reason not to use the callback? Is this mostly a question of style, or are there other important concerns?

解决方案

Generally, it's much more difficult to maintain code that has after_* hooks in models. There are, of course some cases, when using a callback is very reasonable (computing a checksum, for instance, should be done all the time in some applications), but those cases are exceptions to the rule.

In your example with the email, here's some drawbacks of the callback approach:

The mailer interface and the Listing model change for different reasons (Single Responsibility Principle).

For instance, you would like your emails to be sent using a special queue. The interface of communicating with the queue should not, in any way, affect the way the Listing model is built.

Your application doesn't need emailing all the time.

Emailing is just one of the means of interacting with the outside world. Not all of the business logic will always require to be tied to the outside world. One of the examples, as apneadiving mentioned is import. Another example is the console interface (would you like to be sent an email when you're playing with the rails console?)

Testing is difficult.

This is more of a result of 1 and 2, but testing after hooks is increasingly difficult as time passes. The need of mocking the mailer while testing the Listing model makes the test unclear and more difficult to maintain whenever something changes.

这篇关于Rails Mailer最佳实践 - 控制器或after_create回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆