From 9b413a2f638812569f9b18729c37d37c8fed2983 Mon Sep 17 00:00:00 2001 From: Raushan Kumar Raman Date: Sat, 4 Jan 2025 02:41:25 +0530 Subject: [PATCH] feature(user): improved email already taken message for more clarity message --- app/validators/user_validator.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/validators/user_validator.rb b/app/validators/user_validator.rb index e5e212fa26..d9b14cc020 100644 --- a/app/validators/user_validator.rb +++ b/app/validators/user_validator.rb @@ -8,7 +8,7 @@ def validate(record) valid_phone_number_if_receive_sms_notifications(record) validate_date_of_birth_in_past(record.date_of_birth, record) validate_date_of_birth_not_before_1920(record.date_of_birth, record) - validate_email_unique(record) + validate_uniqueness(:email, record, I18n.t('activerecord.errors.messages.email_uniqueness')) end private @@ -52,9 +52,14 @@ def validate_date_of_birth_not_before_1920(date_of_birth, record) record.errors.add(:base, " Date of birth must be on or after 1/1/1920.") unless date_of_birth >= "1920-01-01".to_date end - def validate_email_unique(record) - if User.exists?(email: record.email) - record.errors.add(:base, I18n.t("activerecord.errors.messages.email_uniqueness")) + def validate_uniqueness(attribute, record, message) + existing_record = record.class.find_by(attribute => record[attribute]) + + if existing_record && existing_record.id != record.id + record.errors.add(:base, message) + return false end + + true end end