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.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/locomotive/mongoid/patches.rb', line 82

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