Module: MongoODM::Document::AttributeMethods::Localization::InstanceMethods

Defined in:
lib/mongo_odm/document/attribute_methods/localization.rb

Instance Method Summary collapse

Instance Method Details

#inspect_attribute_with_localization(attr_name) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/mongo_odm/document/attribute_methods/localization.rb', line 59

def inspect_attribute_with_localization(attr_name)
  if self.class.localized_attribute?(attr_name)
    "#{attr_name}[:#{I18n.locale}]: #{read_localized_attribute(attr_name).inspect}"
  else
    inspect_attribute_without_localization(attr_name)
  end
end

#read_localized_attribute(attr_name, locale = nil) ⇒ Object



48
49
50
51
# File 'lib/mongo_odm/document/attribute_methods/localization.rb', line 48

def read_localized_attribute(attr_name, locale = nil)
  locale = I18n.locale || I18n.default_locale if locale.nil?
  (read_attribute(attr_name) || {}.with_indifferent_access)[locale]
end

#write_localized_attribute(attr_name, value, locale = nil) ⇒ Object



53
54
55
56
57
# File 'lib/mongo_odm/document/attribute_methods/localization.rb', line 53

def write_localized_attribute(attr_name, value, locale = nil)
  locale = I18n.locale || I18n.default_locale if locale.nil?
  current_value = read_attribute(attr_name) || {}.with_indifferent_access
  write_attribute(attr_name, current_value.merge(locale => value))
end