Class: Mongoid::Validatable::UniquenessValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- Mongoid::Validatable::UniquenessValidator
- Includes:
- Queryable
- Defined in:
- lib/mongoid/validatable/uniqueness.rb
Overview
Validates whether or not a field is unique against the documents in the database.
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
-
#setup(klass) ⇒ Object
Unfortunately, we have to tie Uniqueness validators to a class.
-
#validate_each(document, attribute, value) ⇒ Errors
Validate the document for uniqueness violations.
Methods included from Queryable
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
19 20 21 |
# File 'lib/mongoid/validatable/uniqueness.rb', line 19 def klass @klass end |
Instance Method Details
#setup(klass) ⇒ Object
Unfortunately, we have to tie Uniqueness validators to a class.
UniquenessValidator.new.setup(Person)
29 30 31 |
# File 'lib/mongoid/validatable/uniqueness.rb', line 29 def setup(klass) @klass = klass end |
#validate_each(document, attribute, value) ⇒ Errors
Validate the document for uniqueness violations.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mongoid/validatable/uniqueness.rb', line 45 def validate_each(document, attribute, value) with_query(document) do attrib, val = to_validate(document, attribute, value) return unless validation_required?(document, attrib) if document. (document, attrib, val) else validate_root(document, attrib, val) end end end |