Methods

Included Modules

Class Index [+]

Quicksearch

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
 9:       def initialize(field_name, options = {})
10:         if options.has_key?(:scope)
11:           assert_kind_of('scope', options[:scope], Array, Symbol)
12:         end
13: 
14:         super
15: 
16:         set_optional_by_default
17:       end

Public Instance Methods

call(target) click to toggle source
    # File lib/dm-validations/validators/uniqueness_validator.rb, line 19
19:       def call(target)
20:         return true if valid?(target)
21: 
22:         error_message = @options[:message] || ValidationErrors.default_error_message(:taken, field_name)
23:         add_error(target, error_message, field_name)
24: 
25:         false
26:       end
valid?(target) click to toggle source
    # File lib/dm-validations/validators/uniqueness_validator.rb, line 28
28:       def valid?(target)
29:         value = target.validation_property_value(field_name)
30:         return true if optional?(value)
31: 
32:         opts = {
33:           :fields    => target.model.key(target.repository.name),
34:           field_name => value,
35:         }
36: 
37:         Array(@options[:scope]).each { |subject|
38:           unless target.respond_to?(subject)
39:             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.")
40:           end
41: 
42:           opts[subject] = target.__send__(subject)
43:         }
44: 
45:         resource = DataMapper.repository(target.repository.name) do
46:           target.model.first(opts)
47:         end
48: 
49:         return true if resource.nil?
50:         target.saved? && resource.key == target.key
51:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.