Revert "Revert "PERF: Cache each theme field value once (#23192)" (#23354)" (#23356)

This reverts commit 9821ca9413.
This commit is contained in:
Joffrey JAFFEUX
2023-08-31 21:12:03 +02:00
committed by GitHub
parent d3c9bb69a1
commit f1d8cd529e
3 changed files with 180 additions and 21 deletions

View File

@@ -1119,4 +1119,104 @@ HTML
expect(messages).to include(["development-mode-theme-changed"])
end
end
describe "#lookup_field when a theme component is used in multiple themes" do
fab!(:theme_1) { Fabricate(:theme, user: user) }
fab!(:theme_2) { Fabricate(:theme, user: user) }
fab!(:child) { Fabricate(:theme, user: user, component: true) }
before_all do
theme_1.add_relative_theme!(:child, child)
theme_2.add_relative_theme!(:child, child)
end
it "efficiently caches fields of theme component by only caching the fields once across multiple themes" do
child.set_field(target: :common, name: "header", value: "World")
child.save!
expect(Theme.lookup_field(theme_1.id, :desktop, "header")).to eq("World")
expect(Theme.lookup_field(theme_2.id, :desktop, "header")).to eq("World")
expect(
Theme.cache.defer_get_set("#{child.id}:common:header:#{Theme.compiler_version}") { raise },
).to eq(["World"])
expect(
Theme.cache.defer_get_set("#{child.id}:desktop:header:#{Theme.compiler_version}") { raise },
).to eq(nil)
expect(
Theme
.cache
.defer_get_set("#{theme_1.id}:common:header:#{Theme.compiler_version}") { raise },
).to eq(nil)
expect(
Theme
.cache
.defer_get_set("#{theme_1.id}:desktop:header:#{Theme.compiler_version}") { raise },
).to eq(nil)
expect(
Theme
.cache
.defer_get_set("#{theme_2.id}:common:header:#{Theme.compiler_version}") { raise },
).to eq(nil)
expect(
Theme
.cache
.defer_get_set("#{theme_2.id}:desktop:header:#{Theme.compiler_version}") { raise },
).to eq(nil)
end
it "puts the parent value ahead of the child" do
theme_1.set_field(target: :common, name: "header", value: "theme_1")
theme_1.save!
child.set_field(target: :common, name: "header", value: "child")
child.save!
expect(Theme.lookup_field(theme_1.id, :desktop, "header")).to eq("theme_1\nchild")
end
it "puts parent translations ahead of child translations" do
theme_1.set_field(target: :translations, name: "en", value: <<~YAML)
en:
theme_1: "test"
YAML
theme_1.save!
theme_field = ThemeField.order(:id).last
child.set_field(target: :translations, name: "en", value: <<~YAML)
en:
child: "test"
YAML
child.save!
child_field = ThemeField.order(:id).last
expect(theme_field.value_baked).not_to eq(child_field.value_baked)
expect(Theme.lookup_field(theme_1.id, :translations, :en)).to eq(
[theme_field, child_field].map(&:value_baked).join("\n"),
)
end
it "prioritizes a locale over its fallback" do
theme_1.set_field(target: :translations, name: "en", value: <<~YAML)
en:
theme_1: "hello"
YAML
theme_1.save!
en_field = ThemeField.order(:id).last
theme_1.set_field(target: :translations, name: "es", value: <<~YAML)
es:
theme_1: "hola"
YAML
theme_1.save!
es_field = ThemeField.order(:id).last
expect(es_field.value_baked).not_to eq(en_field.value_baked)
expect(Theme.lookup_field(theme_1.id, :translations, :en)).to eq(en_field.value_baked)
expect(Theme.lookup_field(theme_1.id, :translations, :es)).to eq(es_field.value_baked)
expect(Theme.lookup_field(theme_1.id, :translations, :fr)).to eq(en_field.value_baked)
end
end
end