Sha256: 66745c71547bd5019de8ab8122d07573b192ad8cf191453ce0d9302479349af3

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# Uses the Phony.plausible method to validate an attribute.
# Usage:
#   validate :phone_number, :phony_plausible => true
class PhonyPlausibleValidator < ActiveModel::EachValidator

  # Validates a String using Phony.plausible? method.
  def validate_each(record, attribute, value)
    return if value.blank?
    record.errors.add(attribute,  options[:message] || :improbable_phone) if not Phony.plausible?(value, cc: options[:country_code])
  end

end

module ActiveModel
  module Validations
    module HelperMethods

      def validates_plausible_phone(*attr_names)
        # merged attributes are modified somewhere, so we are cloning them for each validator
        merged_attributes = _merge_attributes(attr_names)

        validates_with PresenceValidator, merged_attributes.clone if merged_attributes[:presence]
        validates_with FormatValidator, merged_attributes.clone if (merged_attributes[:with] or merged_attributes[:without])
        validates_with PhonyPlausibleValidator, merged_attributes.clone
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
phony_rails-0.5.0 lib/validators/phony_validator.rb
phony_rails-0.4.2 lib/validators/phony_validator.rb