Class: PathUtilities::Form::UniquenessValidator::Mongoid

Inherits:
Mongoid::Validatable::UniquenessValidator
  • Object
show all
Defined in:
lib/path_utilities/form/uniqueness_validator/mongoid.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Mongoid

Returns a new instance of Mongoid.



7
8
9
10
# File 'lib/path_utilities/form/uniqueness_validator/mongoid.rb', line 7

def initialize(options)
  @klass = options[:model] if options[:model]
  super
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/path_utilities/form/uniqueness_validator/mongoid.rb', line 12

def validate_each(record, attribute, value)
  # UniquenessValidator can't be used outside of Mongoid::Document instances, here
  # we return the exact same error, unless the 'model' option is given.
  #
  if !options[:model] && !record.class.ancestors.include?(Mongoid::Document)
    fail ArgumentError, "Unknown validator: 'UniquenessValidator'"

  # If we're inside an ActiveRecord class, and `model` isn't set, use the
  # default behaviour of the validator.
  #
  elsif !options[:model]
    super

  # Custom validator options. The validator can be called in any class, as
  # long as it includes `ActiveModel::Validations`. You can tell the validator
  # which Mongoid::Document based class to check against, using the `model`
  # option. Also, if you are using a different attribute name, you can set the
  # correct one for the ActiveRecord class using the `attribute` option.
  #
  else
    record_org, attribute_org = record, attribute

    attribute = options[:attribute].to_sym if options[:attribute]
    record = options[:model].new(attribute => value)
    is_new_record = record_org.instance_model_for(attribute).new_record?

    if is_new_record || record_org.changes?(attribute)
      super

      if record.errors.any?

        record_org.errors.add(attribute_org, :taken,
          options.except(:case_sensitive, :scope).merge(value: value))
      end
    end
  end
end