Class: DataMapper::Validations::UniquenessValidator

### Home Classes Methods

### In Files

- lib/dm-validations/validators/uniqueness_validator.rb

### Parent

DataMapper::Validations::GenericValidator

### Methods

- ::new
- #call
- #valid?

### Included Modules

- DataMapper::Assertions

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::InvalidContextError
- DataMapper::Validate
- DataMapper::ValidationError
- DataMapper::Validations
- DataMapper::Validations::AbsenceValidator
- DataMapper::Validations::AcceptanceValidator
- DataMapper::Validations::AutoValidations
- DataMapper::Validations::AutoValidations::ModelExtension
- DataMapper::Validations::ClassMethods
- DataMapper::Validations::ConfirmationValidator
- DataMapper::Validations::Context
- DataMapper::Validations::ContextualValidators
- DataMapper::Validations::Format
- DataMapper::Validations::Format::Email
- DataMapper::Validations::Format::Url
- DataMapper::Validations::FormatValidator
- DataMapper::Validations::GenericValidator
- DataMapper::Validations::LengthValidator
- DataMapper::Validations::MethodValidator
- DataMapper::Validations::NumericalityValidator
- DataMapper::Validations::OrderedHash
- DataMapper::Validations::PresenceValidator
- DataMapper::Validations::PrimitiveTypeValidator
- DataMapper::Validations::UniquenessValidator
- DataMapper::Validations::UnknownValidationFormat
- DataMapper::Validations::ValidatesAbsence
- DataMapper::Validations::ValidatesAcceptance
- DataMapper::Validations::ValidatesConfirmation
- DataMapper::Validations::ValidatesFormat
- DataMapper::Validations::ValidatesLength
- DataMapper::Validations::ValidatesNumericality
- DataMapper::Validations::ValidatesPresence
- DataMapper::Validations::ValidatesPrimitiveType
- DataMapper::Validations::ValidatesUniqueness
- DataMapper::Validations::ValidatesWithBlock
- DataMapper::Validations::ValidatesWithMethod
- DataMapper::Validations::ValidatesWithin
- DataMapper::Validations::ValidationErrors
- DataMapper::Validations::WithinValidator
- Object

No matching classes.

# DataMapper::Validations::UniquenessValidator

@author Guy van den Berg @since 0.9

### Public Class Methods

new(field_name, options = {}) click to toggle source

    # File lib/dm-validations/validators/uniqueness_validator.rb, line 9
    def initialize(field_name, options = {})
      if options.has_key?(:scope)
        assert_kind_of('scope', options[:scope], Array, Symbol)
      end

      super

      set_optional_by_default
    end

### Public Instance Methods

call(target) click to toggle source

    # File lib/dm-validations/validators/uniqueness_validator.rb, line 19
    def call(target)
      return true if valid?(target)

      error_message = @options[:message] || ValidationErrors.default_error_message(:taken, field_name)
      add_error(target, error_message, field_name)

      false
    end

valid?(target) click to toggle source

    # File lib/dm-validations/validators/uniqueness_validator.rb, line 28
    def valid?(target)
      value = target.validation_property_value(field_name)
      return true if optional?(value)

      opts = {
        :fields    => target.model.key(target.repository.name),
        field_name => value,
      }

      Array(@options[:scope]).each { |subject|
        unless target.respond_to?(subject)
          raise(ArgumentError,"Could not find property to scope by: #{subject}. Note that :unique does not currently support arbitrarily named groups, for that you should use :unique_index with an explicit validates_uniqueness_of.")
        end

        opts[subject] = target.__send__(subject)
      }

      resource = DataMapper.repository(target.repository.name) do
        target.model.first(opts)
      end

      return true if resource.nil?
      target.saved? && resource.key == target.key
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.