class Mongoid::Fields::Localized

The behaviour of the Localized fields in the custom fields gem is different because we do not rely on I18n directly but on a slight version Mongoid::Fields::I18n. The main reason is only practical to handle the following case: -> Back-office in English and editing content in French.

TODO: use this gem instead github.com/simi/mongoid-localizer

Public Instance Methods

mongoize(object) click to toggle source
# File lib/custom_fields/extensions/mongoid/fields/localized.rb, line 13
def mongoize(object)
  { locale.to_s => type.mongoize(object) }
end

Private Instance Methods

fallbacks?() click to toggle source
# File lib/custom_fields/extensions/mongoid/fields/localized.rb, line 35
def fallbacks?
  i18n.fallbacks?
end
i18n() click to toggle source
# File lib/custom_fields/extensions/mongoid/fields/localized.rb, line 44
def i18n
  ::Mongoid::Fields::I18n
end
locale() click to toggle source
# File lib/custom_fields/extensions/mongoid/fields/localized.rb, line 39
def locale
  # be careful, it does not return ::I18n.locale
  i18n.locale
end
lookup(object) click to toggle source
# File lib/custom_fields/extensions/mongoid/fields/localized.rb, line 19
def lookup(object)
  value = if object.key?(locale.to_s)
            object[locale.to_s]
          elsif object.key?(locale)
            object[locale]
          end
  return value unless value.nil?

  return unless fallbacks? && i18n.respond_to?(:fallbacks)

  fallback_key = i18n.fallbacks[locale].find do |loc|
    object.key?(loc.to_s) || object.key?(loc)
  end
  object[fallback_key.to_s] || object[fallback_key]
end