← Back to Upcase

ActionMailer - all in one or separate?

(Jon Seidel) #1

I’ve added a mailer/notifier to my app and now want to add more features which would work with more than one controller. Any suggestions on whether I should use a single Notifier for all mailers:

rails generate mailer Notifier

or use a separate mailer for each feature/controller that needs it:

rails generate mailer AccidentNotifier
rails generate mailer IncidentNotifier

I’m tempted to start with a single Notifier and only break them out if complexity warrants it; still, I’m curious to see what others would do in this situation.

(Sean Griffin) #2

We usually start with 1 mailer (creatively named Mailer), and break it out if it becomes unwieldly.

(Derek Prior) #3

I was just having this conversation with a co-worker last week. I’ve always done a single mailer, though I’ve generally had applications with anywhere from zero to two mailers. The app I’m on now has four and will likely have more. I’m now bugged by having all the mailers in the same class, and even more bugged by having all the tests for those mailers in the same spec (by convention, anyway). I think I’d prefer to split them out.

One counter argument to splitting them out is that it starts to pollute app/views with various mailer view directories, though i imagine you could namespace them to take care of that.

(Brian Dear) #4

I think starting with just one is key (per the Thoughtbot Style Guide)

That being said, there might be a use case where multiple mailers might be indicated, however, I would default to one mailer. Especially in your case where you’re doing “Accident” and “Incident” notifications. They’re seemingly related and it seems more logical to keep them together. One case that I broke out into a second mailer was when I had an app that had notifications as well as a fairly complex mailing list system. The mailing list related stuff, I had in a separate mailer, but that was more out of convenience (it was a pretty long mailer) and not necessarily a defined need.

(Jon Seidel) #5

Thanks; one it is for starters.