Class: ActiveModel::Validations::PhoneValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/active_validators/active_model/validations/phone_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_validators/active_model/validations/phone_validator.rb', line 6

def validate_each(record, attribute, value)
  plausible = case (country = options[:country])
              when ->(s) { s.blank? }
                # Without a country, try to figure out if it sounds like
                # a plausible phone number.
                Phony.plausible?(value)
              else
                # In the presence of the country option, provide Phony the country
                # code associated with it.
                country_code = ISO3166::Country.new(country.to_s.upcase).country_code
                Phony.plausible?(value, :cc => country_code)
              end

  if !plausible
    record.errors.add(attribute, :invalid)
  end
end