Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation to prevent sending emails that have no recipients should have a better error message #13

Open
esquivalient opened this issue Aug 22, 2023 · 0 comments

Comments

@esquivalient
Copy link
Member

Make an ActiveMailer email with subject and sender, but no recipient, and send! it. You'll see

Error:
InviteTeamMemberEmailTest#test_can_send_valid_invitation_email:
ArgumentError: SMTP To address may not be blank: []
    test/models/invite_team_member_email_test.rb:7:in `block in <class:InviteTeamMemberEmailTest>'

instead of the nice message from ActiveMailer::Base that should like like "You have to have at least one recipient in the to, cc, or bcc fields" from this code

  validate :must_have_at_least_one_recipient_of_some_kind

   def must_have_at_least_one_recipient_of_some_kind
     if self.recipients.blank? and self.cc.blank? and self.bcc.blank?
       self.errors[:base] << "You have to have at least one recipient in the to, cc, or bcc fields"
     end
   end

I think this is because the Rails errors interface has changed and ActiveMailer needs to be updated to add errors the correct way. I think something like this

class Comment
  include ActiveModel::Validations

  validate do |comment|
    comment.must_be_friends
  end

  def must_be_friends
    errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant