Class: Mongoid::Validatable::PresenceValidator
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
ActiveModel::EachValidator
|
|
Instance Chain:
self,
ActiveModel::EachValidator
|
|
Inherits: |
ActiveModel::EachValidator
|
Defined in: | lib/mongoid/validatable/presence.rb |
Overview
Validates that the specified attributes are not blank (as defined by Object#blank?).
Instance Method Summary
-
#validate_each(document, attribute, value)
Validate the document for the attribute and value.
-
#not_present?(value) ⇒ true | false
private
Internal use only
Internal use only
For guarding against false values.
-
#relation_or_fk_missing?(doc, attr, value) ⇒ true | false
private
Internal use only
Internal use only
Returns true if the association is blank or the foreign key is blank.
Instance Method Details
#not_present?(value) ⇒ true
| false
(private)
This method is for internal use only.
For guarding against false values.
# File 'lib/mongoid/validatable/presence.rb', line 77
def not_present?(value) value.blank? && value != false end
#relation_or_fk_missing?(doc, attr, value) ⇒ true
| false
(private)
This method is for internal use only.
Returns true if the association is blank or the foreign key is blank.
# File 'lib/mongoid/validatable/presence.rb', line 61
def relation_or_fk_missing?(doc, attr, value) return true if value.blank? && doc.send(attr).blank? association = doc.relations[attr.to_s] association.stores_foreign_key? && doc.send(association.foreign_key).blank? end
#validate_each(document, attribute, value)
Validate the document for the attribute and value.
# File 'lib/mongoid/validatable/presence.rb', line 28
def validate_each(document, attribute, value) field = document.fields[document.database_field_name(attribute)] if field.try(:localized?) && !value.blank? value.each_pair do |_locale, _value| document.errors.add( attribute, :blank_in_locale, ** .merge(location: _locale) ) if not_present?(_value) end elsif document.relations.has_key?(attribute.to_s) if relation_or_fk_missing?(document, attribute, value) document.errors.add(attribute, :blank, ** ) end else document.errors.add(attribute, :blank, ** ) if not_present?(value) end end