Class: Mongoid::I18n::LocalizedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/mongoid/i18n/localized_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/i18n/localized_validator.rb', line 4

def validate_each record, attribute, value
  if options[:mode] == :only_default
    if record.send("#{attribute}_translations")[::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").empty?
  elsif options[:mode] == :all_locales
    #difference between available locales and stored locales
    diffloc = ::I18n.available_locales - record.send("#{attribute}_translations").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