Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 31ddcc2

Browse files
Refactor email_validator
1 parent 5d855fc commit 31ddcc2

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

app/validators/email_validator.rb

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
class EmailValidator < ActiveModel::EachValidator
22
def validate_each(record, attribute, value)
3-
record.errors[attribute] << email_invalid_error_message unless email_valid?(value)
4-
5-
record.errors[attribute] << email_contains_dots_error_message unless handle_contains_number_of_dots(value)
6-
7-
record.errors[attribute] << email_ends_with_dot_error_message unless handle_last_character_is_not_dot(value)
3+
unless (email_valid?(value) && handle_contains_number_of_dots(value) && handle_last_character_is_not_dot(value))
4+
record.errors[attribute] << email_invalid_error_message
5+
end
86
end
97

108
private
119

1210
def email_invalid_error_message
1311
# options[:message] || I18n.t('errors.messages.email')
14-
(options[:message] || "must be a valid email address")
15-
end
16-
17-
def email_contains_dots_error_message
18-
(options[:message] || "must contain fewer dots")
19-
end
20-
21-
def email_ends_with_dot_error_message
22-
(options[:message] || "must not end with a dot")
12+
(options[:message] || 'must be a valid email address')
2313
end
2414

2515
def email_valid?(email_address)
@@ -32,13 +22,13 @@ def handle_contains_number_of_dots(email_address)
3222
return true if email_address.blank?
3323

3424
email_handle = email_address.split('@')
35-
email_handle[0].count('.') < 3 if (email_handle && !email_handle.blank?)
25+
email_handle[0].count('.') < 3 if email_handle && !email_handle.blank?
3626
end
3727

3828
def handle_last_character_is_not_dot(email_address)
3929
return true if email_address.blank?
4030

4131
email_handle = email_address.split('@')
42-
email_handle[0][-1] != "." if (email_handle && !email_handle.blank?)
32+
email_handle[0][-1] != '.' if email_handle && !email_handle.blank?
4333
end
4434
end

spec/requests/registrations_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
click_on('Join us')
6464

6565
expect(page).to have_content("2 errors prohibited this user from being saved")
66-
expect(page).to have_content("Email is invalid")
66+
expect(page).to have_content("must be a valid email address")
6767
end
6868

6969
it "email address contains uncommon number of characters" do
@@ -74,7 +74,7 @@
7474
click_on('Join us')
7575

7676
expect(page).to have_content("1 error prohibited this user from being saved")
77-
expect(page).to have_content("must contain fewer dots")
77+
expect(page).to have_content("must be a valid email address")
7878
end
7979

8080
it "email address handle ends with a dot" do
@@ -85,7 +85,7 @@
8585
click_on('Join us')
8686

8787
expect(page).to have_content("1 error prohibited this user from being saved")
88-
expect(page).to have_content("must not end with a dot")
88+
expect(page).to have_content("must be a valid email address")
8989
end
9090
end
9191
end

0 commit comments

Comments
 (0)