Module: Mongoid::Validations::LocalizedEachValidator

Defined in:
lib/locomotive/mongoid/patches.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object

Performs validation on the supplied record. By default this will call validates_each to determine validity therefore subclasses should override validates_each with validation logic.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/locomotive/mongoid/patches.rb', line 110

def validate(record)
  attributes.each do |attribute|
    field = record.fields[attribute.to_s]

    # make sure that we use the localized value and not the translations when we test the allow_nil and allow_blank options
    value = field.try(:localized?) ? record.send(attribute.to_sym) : record.read_attribute_for_validation(attribute)

    next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])

    # use the translations of the localized field for the next part
    value = record.read_attribute_for_validation(attribute) if field.try(:localized?)

    validate_each(record, attribute, value)
  end
end