Module: Mongoid::Validatable::ClassMethods

Defined in:
build/mongoid-master/lib/mongoid/validatable.rb

Instance Method Summary collapse

Instance Method Details

#validates_relation(association) ⇒ Object

Adds an associated validator for the association if the validate option was not provided or set to true.

Examples:

Set up validation.

Person.validates_relation(association)

Parameters:

  • association (Association)

    The association metadata.

Since:

  • 2.0.0.rc.1



137
138
139
140
141
# File 'build/mongoid-master/lib/mongoid/validatable.rb', line 137

def validates_relation(association)
  if association.validate?
    validates_associated(association.name)
  end
end

#validates_with(*args, &block) ⇒ Object

Note:

See ActiveModel::Validations::With for full options. This is overridden to add autosave functionality when presence validation is added.

Add validation with the supplied validators forthe provided fields with options.

Examples:

Validate with a specific validator.

validates_with MyValidator, on: :create

Parameters:

  • args (Class<Array>, Hash)

    The validator classes and options.

Since:

  • 3.0.0



156
157
158
159
160
161
162
163
164
165
166
# File 'build/mongoid-master/lib/mongoid/validatable.rb', line 156

def validates_with(*args, &block)
  if args.first == PresenceValidator
    args.last[:attributes].each do |name|
      association = relations[name.to_s]
      if association && association.autosave?
        Association::Referenced::AutoSave.define_autosave!(association)
      end
    end
  end
  super
end

#validating_with_query?true, false

Are we currently performing a validation that has a query?

Examples:

Are we validating with a query?

Model.validating_with_query?

Returns:

  • (true, false)

    If we are validating with a query.

Since:

  • 3.0.2



176
177
178
# File 'build/mongoid-master/lib/mongoid/validatable.rb', line 176

def validating_with_query?
  Threaded.executing?("#{name}-validate-with-query")
end