Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/validators/user_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +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_uniqueness(:email, record, I18n.t("activerecord.errors.messages.email_uniqueness"))
end

private
Expand Down Expand Up @@ -50,4 +51,15 @@ 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_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
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ en:
cant_be_future: can't be in the future
must_be_selected: must be selected
must_be_true_or_false: must be true or false
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.
time:
formats:
day_and_date: "%A, %b %d, %Y"
Expand Down
7 changes: 7 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
expect(user.errors[:base]).to eq([" Date of birth must be on or after 1/1/1920."])
end

it "shows custom email uniqueness error message" do
create(:user, email: "volunteer1@example.com")
user = build(:user, email: "volunteer1@example.com")
expect(user.valid?).to be false
expect(user.errors[:base]).to eq([I18n.t("activerecord.errors.messages.email_uniqueness")])
end

it "has an empty old_emails array when initialized" do
user = build(:user)
expect(user.old_emails).to eq([])
Expand Down
Loading