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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/locomotive/mongoid/patches.rb', line 66

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