Sha256: cf0689cfe827cf4a69d515db51a2de73c51ef673304be19030be490e41582ae6

Contents?: true

Size: 1018 Bytes

Versions: 13

Compression:

Stored size: 1018 Bytes

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)
  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

13 entries across 13 versions & 1 rubygems

Version Path
phony_rails-0.4.1 lib/validators/phony_validator.rb
phony_rails-0.4.0 lib/validators/phony_validator.rb
phony_rails-0.3.2 lib/validators/phony_validator.rb
phony_rails-0.3.1 lib/validators/phony_validator.rb
phony_rails-0.3.0 lib/validators/phony_validator.rb
phony_rails-0.2.2 lib/validators/phony_validator.rb
phony_rails-0.2.1 lib/validators/phony_validator.rb
phony_rails-0.2.0 lib/validators/phony_validator.rb
phony_rails-0.1.12 lib/validators/phony_validator.rb
phony_rails-0.1.11 lib/validators/phony_validator.rb
phony_rails-0.1.10 lib/validators/phony_validator.rb
phony_rails-0.1.9 lib/validators/phony_validator.rb
phony_rails-0.1.8 lib/validators/phony_validator.rb