Class: EmailAddress::ActiveRecordValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- EmailAddress::ActiveRecordValidator
- Defined in:
- lib/email_address/active_record_validator.rb
Overview
ActiveRecord validator class for validating an email address with this library. Note the initialization happens once per process.
Usage:
validates_with EmailAddress::ActiveRecordValidator, field: :name
Options:
-
field: email,
-
fields: [:email1, :email2]
Default field: :email or :email_address (first found)
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ActiveRecordValidator
constructor
A new instance of ActiveRecordValidator.
- #validate(r) ⇒ Object
- #validate_email(r, f) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ActiveRecordValidator
Returns a new instance of ActiveRecordValidator.
18 19 20 |
# File 'lib/email_address/active_record_validator.rb', line 18 def initialize( = {}) @opt = end |
Instance Method Details
#validate(r) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/email_address/active_record_validator.rb', line 22 def validate(r) if @opt[:fields] @opt[:fields].each { |f| validate_email(r, f) } elsif @opt[:field] validate_email(r, @opt[:field]) elsif r.respond_to? :email validate_email(r, :email) elsif r.respond_to? :email_address validate_email(r, :email_address) end end |
#validate_email(r, f) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/email_address/active_record_validator.rb', line 34 def validate_email(r, f) return if r[f].nil? e = Address.new(r[f]) unless e.valid? = @opt[:message] || Config.(:invalid_address, I18n.locale.to_s) || "Invalid Email Address" r.errors.add(f, ) end end |