mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 18:12:46 +08:00
Improve UserEmail#email
validation to use the index.
This commit is contained in:
parent
42e49f6c7b
commit
69a53210d3
|
@ -7,14 +7,13 @@ class UserEmail < ActiveRecord::Base
|
|||
|
||||
before_validation :strip_downcase_email
|
||||
|
||||
validates :email, presence: true, uniqueness: true
|
||||
|
||||
validates :email, presence: true
|
||||
validates :email, email: true, format: { with: EmailValidator.email_regex },
|
||||
if: :validate_email?
|
||||
|
||||
validates :primary, uniqueness: { scope: [:user_id] }, if: :user_id
|
||||
validate :user_id_not_changed, if: :primary
|
||||
|
||||
validates :primary, uniqueness: { scope: [:user_id] }
|
||||
validate :unique_email
|
||||
|
||||
private
|
||||
|
||||
|
@ -30,6 +29,12 @@ class UserEmail < ActiveRecord::Base
|
|||
email_changed?
|
||||
end
|
||||
|
||||
def unique_email
|
||||
if self.will_save_change_to_email? && self.class.where("lower(email) = ?", email).exists?
|
||||
self.errors.add(:email, :taken)
|
||||
end
|
||||
end
|
||||
|
||||
def user_id_not_changed
|
||||
if self.will_save_change_to_user_id? && self.persisted?
|
||||
self.errors.add(:user_id, I18n.t(
|
||||
|
|
|
@ -28,6 +28,19 @@ describe User do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'when record has an email that as already been taken' do
|
||||
it 'should not be valid' do
|
||||
user2 = Fabricate(:user)
|
||||
user.email = user2.email.upcase
|
||||
|
||||
expect(user).to_not be_valid
|
||||
|
||||
expect(user.errors.messages[:primary_email]).to include(I18n.t(
|
||||
'activerecord.errors.messages.taken'
|
||||
))
|
||||
end
|
||||
end
|
||||
|
||||
describe 'when user is staged' do
|
||||
it 'should still validate presence of primary_email' do
|
||||
user.staged = true
|
||||
|
|
Loading…
Reference in New Issue
Block a user