Skip to content

Commit ba313fc

Browse files
author
Raushan Kumar Raman
committed
feature(user): improved email already taken message for more clarity message
1 parent c9cc5e8 commit ba313fc

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

app/validators/user_validator.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def validate(record)
88
valid_phone_number_if_receive_sms_notifications(record)
99
validate_date_of_birth_in_past(record.date_of_birth, record)
1010
validate_date_of_birth_not_before_1920(record.date_of_birth, record)
11+
validate_unique_email(record)
1112
end
1213

1314
private
@@ -50,4 +51,10 @@ def validate_date_of_birth_not_before_1920(date_of_birth, record)
5051

5152
record.errors.add(:base, " Date of birth must be on or after 1/1/1920.") unless date_of_birth >= "1920-01-01".to_date
5253
end
54+
55+
def validate_unique_email(record)
56+
if User.exists?(email: record.email)
57+
record.errors.add(:base, I18n.t('activerecord.errors.messages.email_uniqueness'))
58+
end
59+
end
5360
end

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ en:
6060
cant_be_future: can't be in the future
6161
must_be_selected: must be selected
6262
must_be_true_or_false: must be true or false
63+
email_uniqueness: This email is already in use. If it does not appear on your roster, it may be associated with another CASA organization. Please use a different email address.
6364
time:
6465
formats:
6566
day_and_date: "%A, %b %d, %Y"

spec/models/user_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555
expect(user.errors[:base]).to eq([" Date of birth must be on or after 1/1/1920."])
5656
end
5757

58+
it 'shows custom email uniqueness error message' do
59+
create(:user, email: "[email protected]")
60+
user = build(:user, email: '[email protected]')
61+
expect(user.valid?).to be false
62+
expect(user.errors[:base]).to eq ([I18n.t('activerecord.errors.messages.email_uniqueness')])
63+
end
64+
5865
it "has an empty old_emails array when initialized" do
5966
user = build(:user)
6067
expect(user.old_emails).to eq([])

0 commit comments

Comments
 (0)