开发者

Action Mailer Design Patterns: Is it better to have a single mailer (eg. Notifier) or a mailer for each Model (eg UserMailer, ProductMailer)?

开发者 https://www.devze.com 2023-03-24 20:24 出处:网络
Im trying to better understand good design patterns for when I use ActionMailer in Rails3 (3.1 if it makes a 开发者_如何学Cdifference).

Im trying to better understand good design patterns for when I use ActionMailer in Rails3 (3.1 if it makes a 开发者_如何学Cdifference).

Generally for a Rails app is it better that generally I create a single mailer for the whole app (eg calling it something like Notifier) OR do I create mailers to reflect models which need mail. eg UserMailer, ProductMailer etc.

Do I default to one or many, which approach is better or when do I use which approach?

Thanks

Evolve2k


TL;DR

Background Info

In past projects, we generally have a single mailer for the app called Notifier and we use this for all the mail we send out. I think comes from here: http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3

eg.

class Notifier < ActionMailer::Base
  default :from => "from@example.com"
end

However the RailsGuides seem to imply to me that instead I should make a new mailer each time I have a model that needs to do some mail related activities. How Im seeing it is that just as a model has controllers and views of the same name, so to may a model have a ActionMailer of its namesake.

As illustrated here: http://guides.rubyonrails.org/action_mailer_basics.html

eg.

class UserMailer < ActionMailer::Base
  default :from => "from@example.com"
end

As asked above above, which approach is better and when?


I think this is a specific instance of a more general problem, namely, "Into how many discrete files/directories/packages should I place my code?" Unless your mailers are different somehow (eg, have different headers/include files, use different mail servers, etc.), this is largely a matter of taste.

Personally, I lean toward breaking things into related chunks, but if you see no benefit in doing so, don't!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号