Class: PhonyPlausibleValidator

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

Overview

global namespace so you can use the rails 3 “validate :field, phony_plausible: [options]”

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/phony_attribute/phony_plausible_validator.rb', line 3

def validate_each(record, attribute, value)
  return if value.blank?

  pn = PhonyAttribute::PhoneNumber(value)
  return if pn.nil?

  if !pn.valid?
    record.errors.add(attribute,  options[:message] || :invalid)
  else
    record.errors.add(attribute,  'is a blocked number') if pn.blocked? && !options[:allow_blocked]
    record.errors.add(attribute, :too_short, :count => pn.number.length) if options[:minimum_length] && pn.number.length < options[:minimum_length]
  end
  
end