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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 returnnil
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.