Module: ActiveRecord::Validations::ClassMethods

Defined in:
lib/validates_as_phone_number.rb

Instance Method Summary collapse

Instance Method Details

#validates_as_phone_number(*attr_names) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/validates_as_phone_number.rb', line 18

def validates_as_phone_number(*attr_names)
  regExpStr = '(([0-9]{3})|((\()([0-9]{3})(\))))?([\s\.-])?([0-9]{3})([\s\.-])?([0-9]{4})([\s\.-])?([x] ?[0-9]{1,7})?'
  configuration = {
    :message   => 'is an invalid phone number',
    :with      => /^#{regExpStr}$/,
    :allow_nil => false }
    
  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
  
  configuration[:with] = /^(#{regExpStr})?$/ if configuration[:allow_nil]

  validates_format_of attr_names, configuration
end