Last active
August 29, 2015 14:02
-
-
Save dnagir/29830aab80559e243473 to your computer and use it in GitHub Desktop.
nil, nil, nil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 replaceMailer.invitation_email(...).deliver
withMailer.delay.invitation_email(...)
to send your stuff in bg.