Minor EmailValidator refactor
This commit is contained in:
parent
e48391b813
commit
96e51a0304
1 changed files with 4 additions and 7 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
# EmailValidator
|
||||||
|
#
|
||||||
# Based on https://github.com/balexand/email_validator
|
# Based on https://github.com/balexand/email_validator
|
||||||
#
|
#
|
||||||
# Extended to use only strict mode with following allowed characters:
|
# Extended to use only strict mode with following allowed characters:
|
||||||
|
@ -6,15 +8,10 @@
|
||||||
# See http://www.remote.org/jochen/mail/info/chars.html
|
# See http://www.remote.org/jochen/mail/info/chars.html
|
||||||
#
|
#
|
||||||
class EmailValidator < ActiveModel::EachValidator
|
class EmailValidator < ActiveModel::EachValidator
|
||||||
@@default_options = {}
|
PATTERN = /\A\s*([-a-z0-9+._']{1,64})@((?:[-a-z0-9]+\.)+[a-z]{2,})\s*\z/i.freeze
|
||||||
|
|
||||||
def self.default_options
|
|
||||||
@@default_options
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_each(record, attribute, value)
|
def validate_each(record, attribute, value)
|
||||||
options = @@default_options.merge(self.options)
|
unless value =~ PATTERN
|
||||||
unless value =~ /\A\s*([-a-z0-9+._']{1,64})@((?:[-a-z0-9]+\.)+[a-z]{2,})\s*\z/i
|
|
||||||
record.errors.add(attribute, options[:message] || :invalid)
|
record.errors.add(attribute, options[:message] || :invalid)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue