Class: Mongoid::I18n::LocalizedField
- Inherits:
-
Object
- Object
- Mongoid::I18n::LocalizedField
- Includes:
- Fields::Serializable
- Defined in:
- lib/mongoid/i18n/localized_field.rb
Instance Method Summary collapse
-
#assign(object, value) ⇒ Object
Assing new translation to translation table.
-
#deserialize(object) ⇒ Object
Return translated values of field, accoring to current locale.
-
#locale ⇒ Object
Return current locale as string.
-
#replace(object, values) ⇒ Object
Replace translation hash with new one.
-
#to_hash(object) ⇒ Object
Return translations as keys.
Instance Method Details
#assign(object, value) ⇒ Object
Assing new translation to translation table.
33 34 35 |
# File 'lib/mongoid/i18n/localized_field.rb', line 33 def assign(object, value) (object || {}).merge(locale => value) end |
#deserialize(object) ⇒ Object
Return translated values of field, accoring to current locale. If :use_default_if_empty is set, then in case when there no translation available for current locale, if will try to get translation for defalt_locale.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mongoid/i18n/localized_field.rb', line 10 def deserialize(object) lookups = [self.locale] # TODO: Add I18n.fallbacks support instead of :use_default_if_empty if [:use_default_if_empty] lookups.push ::I18n.default_locale.to_s end # Find first localized value in lookup path and return corresponding value object[lookups.find{|locale| object[locale]}] end |
#locale ⇒ Object
Return current locale as string
44 45 46 |
# File 'lib/mongoid/i18n/localized_field.rb', line 44 def locale ::I18n.locale.to_s end |
#replace(object, values) ⇒ Object
Replace translation hash with new one. If :clear_empty_values is set to pairs with empty values will be reject
39 40 41 |
# File 'lib/mongoid/i18n/localized_field.rb', line 39 def replace(object, values) self.to_hash(values) end |
#to_hash(object) ⇒ Object
Return translations as keys. If :clear_empty_values is set to true pairs with empty values will be rejected
24 25 26 27 28 29 30 |
# File 'lib/mongoid/i18n/localized_field.rb', line 24 def to_hash(object) if [:clear_empty_values] object.reject!{|k,v| v.blank?} end object end |