Skip to content

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

@esquivalient

Description

@esquivalient

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions