Sha256: 7b16ed0d46a2921e04e26c5618ba0f6320aace8be64b0d21eb599489175a9a91

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

module Mongoid
  module I18n
    class LocalizedValidator < ActiveModel::EachValidator
      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	
    end	
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid_i18n-0.5.1 lib/mongoid/i18n/localized_validator.rb
mongoid_i18n-0.5.0 lib/mongoid/i18n/localized_validator.rb
mongoid_i18n-0.4.0 lib/mongoid/i18n/localized_validator.rb
mongoid_i18n-0.3.1 lib/mongoid/i18n/localized_validator.rb
mongoid_i18n-0.3.0 lib/mongoid/i18n/localized_validator.rb