11class 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
4434end
0 commit comments