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

Commit cb005cd

Browse files
Refactoring email_validator as per the recommendations of code climate
1 parent d033d2d commit cb005cd

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

app/validators/email_validator.rb

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
class EmailValidator < ActiveModel::EachValidator
22
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)
64

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)
106

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)
148
end
159

1610
private
11+
1712
def email_invalid_error_message
1813
# options[:message] || I18n.t('errors.messages.email')
1914
(options[:message] || "must be a valid email address")
@@ -37,19 +32,13 @@ def handle_contains_number_of_dots(email_address)
3732
return true if email_address.blank?
3833

3934
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?)
4436
end
4537

4638
def handle_last_character_is_not_dot(email_address)
4739
return true if email_address.blank?
4840

4941
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?)
5443
end
55-
end
44+
end

0 commit comments

Comments
 (0)