1
1
class EmailValidator < ActiveModel ::EachValidator
2
2
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
8
6
end
9
7
10
8
private
11
9
12
10
def email_invalid_error_message
13
11
# 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' )
23
13
end
24
14
25
15
def email_valid? ( email_address )
@@ -32,13 +22,13 @@ def handle_contains_number_of_dots(email_address)
32
22
return true if email_address . blank?
33
23
34
24
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?
36
26
end
37
27
38
28
def handle_last_character_is_not_dot ( email_address )
39
29
return true if email_address . blank?
40
30
41
31
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?
43
33
end
44
34
end
0 commit comments