Class: CouchRest::Validation::UniquenessValidator

Inherits:
GenericValidator
  • Object
show all
Defined in:
lib/couchrest-uniqueness-validation.rb

Instance Method Summary collapse

Constructor Details

#initialize(field_name, options = {}) ⇒ UniquenessValidator

Returns a new instance of UniquenessValidator.



8
9
10
11
12
# File 'lib/couchrest-uniqueness-validation.rb', line 8

def initialize(field_name, options = {})
  super
  @field_name, @options = field_name, options
  @options[:view] ||= "by_#{@field_name}"
end

Instance Method Details

#call(target) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/couchrest-uniqueness-validation.rb', line 14

def call(target)
  return true if unique?(target)
  
  error_message = @options[:message] || ValidationErrors.default_error_message(:taken, field_name)
  add_error(target, error_message, field_name)        
  false
end