Class: PhoneValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/phone_validator.rb

Overview

PhoneValidator makes phone validation natural for ActiveModel object. Validation using global_phone.

Constant Summary collapse

@@default_options =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_optionsObject



5
6
7
# File 'lib/phone_validator.rb', line 5

def self.default_options
  @@default_options
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/phone_validator.rb', line 9

def validate_each(record, attribute, value)
  options = @@default_options.merge(self.options)

  unless GlobalPhone.validate(value)
    record.errors.add(attribute, options[:message] || :invalid)
  end
end