Skip to content

Commit

Permalink
feature(user): improved email already taken message for more clarity …
Browse files Browse the repository at this point in the history
…message
  • Loading branch information
Raushan Kumar Raman committed Jan 3, 2025
1 parent 0c9d065 commit 9b413a2
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions app/validators/user_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 9b413a2

Please sign in to comment.