Skip to content

Instantly share code, notes, and snippets.

@dnagir

dnagir/eample.rb

Last active Aug 29, 2015
Embed
What would you like to do?
nil, nil, nil
# 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
@alg

This comment has been minimized.

Copy link

@alg alg commented Jun 20, 2014

You want to raise an exception in invitation_email to be caught by smthng like Airbrake. Then, if an invitation can legitimately be w/o name, you need to check this and not even call that mailer.

@dnagir

This comment has been minimized.

Copy link
Owner Author

@dnagir dnagir commented Jun 20, 2014

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.

@alg

This comment has been minimized.

Copy link

@alg alg commented Jun 20, 2014

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.

@alg

This comment has been minimized.

Copy link

@alg alg commented Jun 20, 2014

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