Skip to content
Snippets Groups Projects

Use the same email validation for User and Email

All threads resolved!
5 files
+ 6
10
Compare changes
  • Side-by-side
  • Inline
Files
5
  • Otherwise, it is possible that a `User` primary email is successfully
    saved, but it cannot be added to the emails table due to being invalid
    for the `Email` model.
    
    The `Email#email` validation is now more relaxed, and the same as the
    `User#email` validation: it is the Devise email validation, that
    basically only checks that there is a single @ sign separating local
    part and domain. The previous stricter Email#email validation was
    enforcing compliance with RFC3696, but many mail servers allow addresses
    that are not fully compliant with the RFC. Therefore, the relaxed
    validation is preferable, to avoid blocking working email addresses.
+ 2
6
@@ -6,8 +6,8 @@ class Email < ApplicationRecord
@@ -6,8 +6,8 @@ class Email < ApplicationRecord
belongs_to :user, optional: false
belongs_to :user, optional: false
validates :email, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true, devise_email: true
validate :validate_email_format
validate :unique_email, if: ->(email) { email.email_changed? }
validate :unique_email, if: ->(email) { email.email_changed? }
scope :confirmed, -> { where.not(confirmed_at: nil) }
scope :confirmed, -> { where.not(confirmed_at: nil) }
@@ -33,10 +33,6 @@ def unique_email
@@ -33,10 +33,6 @@ def unique_email
self.errors.add(:email, 'has already been taken') if primary_email_of_another_user?
self.errors.add(:email, 'has already been taken') if primary_email_of_another_user?
end
end
def validate_email_format
self.errors.add(:email, I18n.t(:invalid, scope: 'valid_email.validations.email')) unless ValidateEmail.valid?(self.email)
end
# once email is confirmed, update the gpg signatures
# once email is confirmed, update the gpg signatures
def update_invalid_gpg_signatures
def update_invalid_gpg_signatures
user.update_invalid_gpg_signatures if confirmed?
user.update_invalid_gpg_signatures if confirmed?
Loading