module ActiverecordGlobalize::Translates::InstanceMethods
Public Instance Methods
as_json(*)
click to toggle source
Overrides to_json to exclude translated_attrs
# File lib/activerecord_globalize/translates.rb, line 49 def as_json(*) attributes.delete_if { |k, _v| translated_attrs.map { |f| translated_attr_name(f) }.include?(k) } end
read_translation(attr_name, locale = I18n.locale)
click to toggle source
get translations hstore of the field then if the current locale in translations return it’s value if not return the default locale’s value if current/default locale not found return actual field value
attr_name
field name locale
desired locale
# File lib/activerecord_globalize/translates.rb, line 36 def read_translation(attr_name, locale = I18n.locale) translations = send(translated_attr_name(attr_name)) || {} return translations[locale] if translations.key?(locale) return translations[I18n.default_locale] if translations.key?(I18n.default_locale) self[attr_name] end
translated_attr_name(attr_name)
click to toggle source
return translated field name of a field
# File lib/activerecord_globalize/translates.rb, line 44 def translated_attr_name(attr_name) "#{attr_name}_translations" end
update_with_locale!(locale, *attrs)
click to toggle source
activerecord update attributes in specific locale
# File lib/activerecord_globalize/translates.rb, line 54 def update_with_locale!(locale, *attrs) I18n.with_locale locale do update!(*attrs) end end
write_translation(attr_name, value, locale = I18n.locale)
click to toggle source
set value to a specific/current locale and fire ActiveRecord attribute_will_change! if the current locale equals the default locale update value of original field
attr_name
field name value
locale value locale
desired locale
# File lib/activerecord_globalize/translates.rb, line 18 def write_translation(attr_name, value, locale = I18n.locale) translation_store = translated_attr_name(attr_name) translations = send(translation_store) || {} send("#{translation_store}_will_change!") unless translations[locale.to_s] == value translations[locale.to_s] = value send("#{translation_store}=", translations) self[attr_name] = value if I18n.default_locale == I18n.locale value end