mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Improve User#email= behavior (#11338)
- Only apply the change after `save` is called on the record - Automatically remove matching secondary emails
This commit is contained in:
@@ -4,6 +4,7 @@ class UserEmail < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
|
||||
attr_accessor :skip_validate_email
|
||||
attr_accessor :skip_validate_unique_email
|
||||
|
||||
before_validation :strip_downcase_email
|
||||
|
||||
@@ -12,7 +13,7 @@ class UserEmail < ActiveRecord::Base
|
||||
|
||||
validates :primary, uniqueness: { scope: [:user_id] }, if: [:user_id, :primary]
|
||||
validate :user_id_not_changed, if: :primary
|
||||
validate :unique_email
|
||||
validate :unique_email, if: :validate_unique_email?
|
||||
|
||||
scope :secondary, -> { where(primary: false) }
|
||||
|
||||
@@ -30,8 +31,13 @@ class UserEmail < ActiveRecord::Base
|
||||
email_changed?
|
||||
end
|
||||
|
||||
def validate_unique_email?
|
||||
return false if self.skip_validate_unique_email
|
||||
will_save_change_to_email?
|
||||
end
|
||||
|
||||
def unique_email
|
||||
if self.will_save_change_to_email? && self.class.where("lower(email) = ?", email).exists?
|
||||
if self.class.where("lower(email) = ?", email).exists?
|
||||
self.errors.add(:email, :taken)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user