Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ahukkanen committed Mar 1, 2021
2 parents 90cdb20 + 677cc20 commit 35f3dca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/decidim/term_customizer/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,29 @@ def initialize(resolver)
# This will also cache the results and fetch the result directly from
# cache on consequent calls until the cache is expired.
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
# In order to avoid endless loops with cache logging calling the I18n
# calling the translation loader, calling cache logging calling I18n
# (etc.), temporarily mute the cache logging during this call. If the
# cache logging level is set to `Logger::DEBUG`, it could happen as
# explained at:
# https://github.com/mainio/decidim-module-term_customizer/issues/38
@translations_hash ||= Rails.cache.mute do
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 = final_hash
keyparts.each do |key|
current[key.to_sym] ||= {}
current = current[key.to_sym]
end

current[lastkey] = tr.value
current[lastkey] = tr.value
end
final_hash
end
final_hash
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/lib/decidim/term_customizer/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@

subject.translations_hash
end

it "mutes the cache logging during fetch" do
expect(Rails.cache).to receive(:mute).and_call_original
expect(Rails.cache).to receive(:fetch)

subject.translations_hash
end
end

describe "#clear_cache" do
Expand Down

0 comments on commit 35f3dca

Please sign in to comment.