Sha256: 4e2218680610580972a8118b160fd927cd7ae8b6767e5a41708c549a3414aa2b

Contents?: true

Size: 1.86 KB

Versions: 6

Compression:

Stored size: 1.86 KB

Contents

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

  # Validates a String using Phony.plausible? method.
  def validate_each(record, attribute, value)
    return if value.blank?

    @record = record

    value = PhonyRails.normalize_number value, default_country_code: normalized_country_code if normalized_country_code
    @record.errors.add(attribute, error_message) if not Phony.plausible?(value, cc: country_number)
  end

  private

  def error_message
    options[:message] || :improbable_phone
  end

  def country_number
    options[:country_number] || record_country_number || country_number_from_country_code
  end

  def record_country_number
    @record.country_number if @record.respond_to?(:country_number) && !options[:ignore_record_country_number]
  end

  def country_number_from_country_code
    PhonyRails.country_number_for(country_code)
  end

  def country_code
    options[:country_code] || record_country_code
  end

  def record_country_code
    @record.country_code if @record.respond_to?(:country_code) && !options[:ignore_record_country_code]
  end

  def normalized_country_code
    options[:normalized_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

6 entries across 6 versions & 1 rubygems

Version Path
phony_rails-0.13.0 lib/validators/phony_validator.rb
phony_rails-0.12.13 lib/validators/phony_validator.rb
phony_rails-0.12.11 lib/validators/phony_validator.rb
phony_rails-0.12.10 lib/validators/phony_validator.rb
phony_rails-0.12.9 lib/validators/phony_validator.rb
phony_rails-0.12.8 lib/validators/phony_validator.rb