Class: Decidim::TermCustomizer::Loader
- Inherits:
-
Object
- Object
- Decidim::TermCustomizer::Loader
- Defined in:
- lib/decidim/term_customizer/loader.rb
Overview
The loader class is a middleman that converts the Translation model objects to a flat translation hash that can be directly used in the i18n backend. The main purpose of this class is to add caching possibility for the translation hashes.
Instance Method Summary collapse
-
#clear_cache ⇒ Object
Clears the translations cache only for the current context defined by the resolver.
-
#initialize(resolver) ⇒ Loader
constructor
A new instance of Loader.
-
#translations_hash ⇒ Object
Converts the translation objects to a flat hash where the keys are the translatable keys used in the i18n backend containing the locales.
Constructor Details
#initialize(resolver) ⇒ Loader
Returns a new instance of Loader.
10 11 12 |
# File 'lib/decidim/term_customizer/loader.rb', line 10 def initialize(resolver) @resolver = resolver end |
Instance Method Details
#clear_cache ⇒ Object
Clears the translations cache only for the current context defined by the resolver.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/decidim/term_customizer/loader.rb', line 58 def clear_cache Rails.cache.delete_matched("#{cache_key_base}/*") rescue NotImplementedError # Some cache store, such as `ActiveSupport::Cache::MemCacheStore` do not # support `delete_matched`. Therefore, clear all the possibly existing # cache keys manually for each space and component. # Clear all the "organization" translation keys. Rails.cache.delete(cache_key_base) # Iterate over the participatory spaces and their components to manually # clear the cached records for all of them. Decidim.participatory_space_registry.manifests.each do |manifest| manifest.model_class_name.constantize.all.each do |space| Rails.cache.delete("#{cache_key_base}/space_#{space.id}") next unless space.respond_to?(:components) space.components.each do |component| Rails.cache.delete( "#{cache_key_base}/space_#{space.id}/component_#{component.id}" ) end end end end |
#translations_hash ⇒ Object
Converts the translation objects to a flat hash where the keys are the translatable keys used in the i18n backend containing the locales. The values of the hash are the translations for the keys.
The final hash looks similar like this: {
en: {
decidim: {
translation: "Term EN"
}
},
fi: {
decidim: {
translation: "Term FI"
}
}
}
This will also cache the results and fetch the result directly from cache on consequent calls until the cache is expired.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/decidim/term_customizer/loader.rb', line 34 def translations_hash @translations_hash ||= Rails.cache.fetch( cache_key, expires_in: 24.hours ) do final_hash = {} resolver.translations.each do |tr| keyparts = [tr.locale] + tr.key.split(".") lastkey = keyparts.pop.to_sym current = final_hash keyparts.each do |key| current[key.to_sym] ||= {} current = current[key.to_sym] end current[lastkey] = tr.value end final_hash end end |