1
1
class EmailValidator < ActiveModel ::EachValidator
2
2
def validate_each ( record , attribute , value )
3
- unless email_valid? ( value )
4
- record . errors [ attribute ] << email_invalid_error_message
5
- end
3
+ record . errors [ attribute ] << email_invalid_error_message unless email_valid? ( value )
6
4
7
- unless handle_contains_number_of_dots ( value )
8
- record . errors [ attribute ] << email_contains_dots_error_message
9
- end
5
+ record . errors [ attribute ] << email_contains_dots_error_message unless handle_contains_number_of_dots ( value )
10
6
11
- unless handle_last_character_is_not_dot ( value )
12
- record . errors [ attribute ] << email_ends_with_dot_error_message
13
- end
7
+ record . errors [ attribute ] << email_ends_with_dot_error_message unless handle_last_character_is_not_dot ( value )
14
8
end
15
9
16
10
private
11
+
17
12
def email_invalid_error_message
18
13
# options[:message] || I18n.t('errors.messages.email')
19
14
( options [ :message ] || "must be a valid email address" )
@@ -37,19 +32,13 @@ def handle_contains_number_of_dots(email_address)
37
32
return true if email_address . blank?
38
33
39
34
email_handle = email_address . split ( '@' )
40
-
41
- if ( email_handle && !email_handle . blank? )
42
- email_handle [ 0 ] . count ( '.' ) < 3
43
- end
35
+ email_handle [ 0 ] . count ( '.' ) < 3 if ( email_handle && !email_handle . blank? )
44
36
end
45
37
46
38
def handle_last_character_is_not_dot ( email_address )
47
39
return true if email_address . blank?
48
40
49
41
email_handle = email_address . split ( '@' )
50
-
51
- if ( email_handle && !email_handle . blank? )
52
- email_handle [ 0 ] [ -1 ] != "."
53
- end
42
+ email_handle [ 0 ] [ -1 ] != "." if ( email_handle && !email_handle . blank? )
54
43
end
55
- end
44
+ end
0 commit comments