Module: ActiverecordGlobalize::Translates::InstanceMethods
- Defined in:
- lib/activerecord_globalize/translates.rb
Instance Method Summary collapse
-
#as_json ⇒ Object
Overrides to_json to exclude translated_attrs.
-
#read_translation(attr_name, locale = I18n.locale) ⇒ Object
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.
-
#translated_attr_name(attr_name) ⇒ Object
return translated field name of a field.
-
#update_with_locale!(locale, *attrs) ⇒ Object
activerecord update attributes in specific locale.
-
#write_translation(attr_name, value, locale = I18n.locale) ⇒ Object
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.
Instance Method Details
#as_json ⇒ Object
Overrides to_json to exclude translated_attrs
49 50 51 |
# 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) ⇒ Object
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
36 37 38 39 40 41 |
# 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) ⇒ Object
return translated field name of a field
44 45 46 |
# File 'lib/activerecord_globalize/translates.rb', line 44 def translated_attr_name(attr_name) "#{attr_name}_translations" end |
#update_with_locale!(locale, *attrs) ⇒ Object
activerecord update attributes in specific locale
54 55 56 57 58 |
# 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) ⇒ Object
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
18 19 20 21 22 23 24 25 26 |
# 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 |