lib/validators/phone_validator.rb in phonelib-0.3.0 vs lib/validators/phone_validator.rb in phonelib-0.3.1

- old
+ new

@@ -23,18 +23,45 @@ # # class Phone < ActiveRecord::Base # attr_accessible :number # validates :number, phone: { allow_blank: true } # end +# +# Validates that attribute is a valid phone number of specified type(s). +# It is also possible to check that attribute is a possible number of specified +# type(s). Symbol or array accepted. +# +# class Phone < ActiveRecord::Base +# attr_accessible :number, :mobile +# validates :number, phone: { types: [:mobile, :fixed], allow_blank: true } +# validates :mobile, phone: { possible: true, types: :mobile } +# end +# + class PhoneValidator < ActiveModel::EachValidator # Include all core methods include Phonelib::Core # Validation method def validate_each(record, attribute, value) + return if options[:allow_blank] && value.blank? + phone = parse(value) - valid = options[:possible] ? phone.possible? : phone.valid? - valid = true if options[:allow_blank] && phone.original.blank? + if options[:types] + method = options[:possible] ? :possible_types : :types + valid = (phone.send(method) & types).size > 0 + else + method = options[:possible] ? :possible? : :valid? + valid = phone.send(method) + end record.errors.add(attribute, options[:message] || :invalid) unless valid end + + private + + def types + types = options[:types].is_a?(Array) ? options[:types] : [options[:types]] + types.map &:to_sym + end + end