-
-
Save dnagir/29830aab80559e243473 to your computer and use it in GitHub Desktop.
# In the context of Rails app (mailers) | |
# Somewhere in email helper | |
def from_address_by(person_name) | |
return nil unless person_name | |
address = Mail::Address.new AppConfig.email.from # ex: "john@example.com" | |
address.display_name = person_name # ex: "John Doe" | |
address.format # returns "John Doe <john@example.com>" | |
end | |
# being used in the mailer as: | |
def invitation_email(invitation) | |
from_name = invitation.invited_by.try(:name) | |
mail from: from_address_by(from_name) | |
end | |
# Reminds my about https://www.destroyallsoftware.com/screencasts/catalog/how-and-why-to-avoid-nil |
Yea, thos are some of the options.
The invitation can legitimately be without name
and the email should still be sent out.
Where "no name" state should be checked? Who is responsible for that?
Should from_address_by
raise an error when there's no name? Should it return nil and force the "clients" to deal with it?
Too many questions, that nil
brings IMO.
By "raise an exception" I don't mean raising it manually (which may be an option). Just do invitation.invited_by.name
. Also, I believe you aren't sending in background. That's something I don't do these days. Sidekiq gives you a great option to replace Mailer.invitation_email(...).deliver
with Mailer.delay.invitation_email(...)
to send your stuff in bg.
The invitation can legitimately be without name and the email should still be sent out.
Where "no name" state should be checked? Who is responsible for that?
Should from_address_by raise an error when there's no name? Should it return nil and force the "clients" to deal with it?
If you can send w/o name, you should have "from-address" anyway. In this case, from_address_by
has wrong implementation. It shouldn't return nil
but should use some predefined config value for the name if it's not in the invitation.
Should you not send an invitation w/o name, you'd need a InvitationsManager
object to encapsulate the logic, and call the Mailer only if the name is present.
You want to raise an exception in
invitation_email
to be caught by smthng like Airbrake. Then, if an invitation can legitimately be w/oname
, you need to check this and not even call that mailer.