module Netfira::WebConnect::Model::Record::Translations
This module is mixed into records that have translations.
Public Instance Methods
[](key)
click to toggle source
Calls superclass method
# File lib/netfira/web_connect/model/record/translations.rb, line 25 def [](key) if translated_attribute_names.include? key.to_s get_translated_string key.to_sym else super key end end
[]=(key, value)
click to toggle source
Calls superclass method
# File lib/netfira/web_connect/model/record/translations.rb, line 33 def []=(key, value) if translated_attribute_names.include? key.to_s set_translated_string key.to_sym, value else super key, value end end
get_translated_string(key)
click to toggle source
Called when getting a translated string, e.g. product.description
# File lib/netfira/web_connect/model/record/translations.rb, line 8 def get_translated_string(key) translated_string_for key end
locale()
click to toggle source
# File lib/netfira/web_connect/model/record/translations.rb, line 21 def locale shop.locale end
set_translated_string(key, value)
click to toggle source
Called when assigning a translated string, e.g. product.description = 'Stuff'
# File lib/netfira/web_connect/model/record/translations.rb, line 13 def set_translated_string(key, value) if Hash === value replace_translations_for key, value else translated_string_for(key)[nil] = value end end
Private Instance Methods
clear_translations()
click to toggle source
# File lib/netfira/web_connect/model/record/translations.rb, line 81 def clear_translations @translations = nil @translated_strings = nil self end
previous_translations()
click to toggle source
# File lib/netfira/web_connect/model/record/translations.rb, line 47 def previous_translations Hash.new { |hash, k| hash[k] = {} }.tap do |result| translations.each do |language, translation| translation.changed_attributes.each do |attr, value| result[attr][language] = value if translation.class.translated_attribute_names.include? attr end end end end
replace_translations_for(key, value)
click to toggle source
# File lib/netfira/web_connect/model/record/translations.rb, line 66 def replace_translations_for(key, value) translations.each { |lang, model| model[key] = value[lang] } translated_string_for(key).merge! value end
translated_attribute_names()
click to toggle source
# File lib/netfira/web_connect/model/record/translations.rb, line 87 def translated_attribute_names self.class::Translation.translated_attribute_names end
translated_string_for(key)
click to toggle source
# File lib/netfira/web_connect/model/record/translations.rb, line 71 def translated_string_for(key) translated_strings[key] ||= begin t = Model::Record::TranslatedString.new t.on(:get) { |lang| translation_for_language(lang || locale).__send__ key } t.on(:set) { |lang, value| translation_for_language(lang || locale).__send__ :"#{key}=", value } t.on(:all) { translations.map{ |lang, model| [lang, model[key]] }.reject{ |x| x[1].nil? }.to_h } t end end
translated_strings()
click to toggle source
# File lib/netfira/web_connect/model/record/translations.rb, line 57 def translated_strings @translated_strings ||= {} end
translation_for_language(lang)
click to toggle source
# File lib/netfira/web_connect/model/record/translations.rb, line 61 def translation_for_language(lang) lang = lang.to_s translations[lang] ||= translation_models.build(language: lang) end
translations()
click to toggle source
# File lib/netfira/web_connect/model/record/translations.rb, line 43 def translations @translations ||= translation_models.map{ |model| [model.language, model]}.to_h end