Class: Mongoid::Verbalize::VerbalizedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/mongoid/verbalize/verbalized_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mongoid/verbalize/verbalized_validator.rb', line 4

def validate_each record, attribute, value
  if options[:mode] == :only_default
    if record.send("#{attribute}_translations_raw")[::I18n.default_locale.to_s].blank?
      record.errors.add(attribute, :locale_blank, options.except(:mode).merge(
        :cur_locale => ::I18n.t(:"locales.#{::I18n.default_locale}", :default => ::I18n.default_locale.to_s)
      ))
    end
  elsif options[:mode] == :one_locale
    record.errors.add(attribute, :all_locales_blank, options.except(:mode)) if record.send("#{attribute}_translations_raw").empty?
  elsif options[:mode] == :all_locales
    #difference between available locales and stored locales
    diffloc = ::I18n.available_locales - record.send("#{attribute}_translations_raw").keys.collect { |key| key.to_sym }

    #print an error for each missing locale
    diffloc.each do |locale| 
      record.errors.add(attribute, :locale_blank, options.except(:mode).merge(
        :cur_locale => ::I18n.t(:"locales.#{locale}", :default => locale.to_s)
      )) 
    end
  end
end