Skip to content

A platform for community discussion. Free, open, simple.

FIX: Do not write localized fancy title to db when fancy_title is null (#37668)

Natalie Tay8a79c788a547
diff --git a/lib/localization_attributes_replacer.rb b/lib/localization_attributes_replacer.rb
index 5f560fadb64..4be1c9f0d1f 100644
--- a/lib/localization_attributes_replacer.rb
+++ b/lib/localization_attributes_replacer.rb
@@ -15,8 +15,17 @@ module LocalizationAttributesReplacer
def self.replace_topic_attributes(topic, crawl_locale)
if loc = get_localization(topic, crawl_locale)
# assigning directly to title would commit the change to the database
- # due to the setter method defined in the Topic model
- topic.send(:write_attribute, :title, loc.title) if loc.title.present?
+ # due to the setter method defined in the Topic model.
+ # fancy_title must also be set to prevent the lazy DB write in
+ # Topic#fancy_title from persisting a localized value when fancy_title is NULL.
+ if loc.title.present?
+ topic.send(:write_attribute, :title, loc.title)
+ topic.send(
+ :write_attribute,
+ :fancy_title,
+ loc.fancy_title.presence || Topic.fancy_title(loc.title),
+ )
+ end
topic.excerpt = loc.excerpt if loc.excerpt.present?
end
diff --git a/spec/lib/localization_attributes_replacer_spec.rb b/spec/lib/localization_attributes_replacer_spec.rb
index 591288a7932..d258fe5ea31 100644
--- a/spec/lib/localization_attributes_replacer_spec.rb
+++ b/spec/lib/localization_attributes_replacer_spec.rb
@@ -37,13 +37,41 @@ describe LocalizationAttributesReplacer do
Fabricate(:topic_localization, topic:, locale: "ja", title: "猫犬", excerpt: "柴犬は猫のような犬です。")
end
- it "replaces the title and excerpt with localized values" do
+ it "replaces the title, fancy_title, and excerpt with localized values" do
LocalizationAttributesReplacer.replace_topic_attributes(topic, "ja")
expect(topic.title).to eq(ja_localization.title)
+ expect(topic.fancy_title).to eq(ja_localization.fancy_title)
expect(topic.excerpt).to eq(ja_localization.excerpt)
end
+ it "does not write localized fancy_title to the database when fancy_title is null" do
+ topic.update_column(:fancy_title, nil)
+ topic.reload
+
+ LocalizationAttributesReplacer.replace_topic_attributes(topic, "ja")
+
+ expect(topic.title).to eq(ja_localization.title)
+ expect(topic.read_attribute(:fancy_title)).to eq(ja_localization.fancy_title)
+
+ localized_fancy_title = Topic.fancy_title(ja_localization.title)
+ expect(Topic.where(id: topic.id).pick(:fancy_title)).not_to eq(localized_fancy_title)
+ end
+
+ it "generates fancy_title from localized title when localization fancy_title is blank" do
+ ja_localization.update_column(:fancy_title, "")
+ topic.update_column(:fancy_title, nil)
+ topic.reload
+
+ LocalizationAttributesReplacer.replace_topic_attributes(topic, "ja")
+
+ expect(topic.title).to eq(ja_localization.title)
+ expect(topic.read_attribute(:fancy_title)).to eq(Topic.fancy_title(ja_localization.title))
+
+ localized_fancy_title = Topic.fancy_title(ja_localization.title)
+ expect(Topic.where(id: topic.id).pick(:fancy_title)).not_to eq(localized_fancy_title)
+ end
+
it "does not change the title or excerpt if the locale is the same" do
LocalizationAttributesReplacer.replace_topic_attributes(topic, "en")
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 94adc855ce5..2bf0e5fdc06 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -6166,6 +6166,24 @@ RSpec.describe TopicsController do
expect(response.body).to include(ja_category.name)
expect(response.body).to include(ja_subcategory.name)
end
+
+ it "does not persist localized fancy_title to the database when topic fancy_title is null" do
+ topic.update_column(:fancy_title, nil)
+
+ get topic.relative_url,
+ env: {
+ "HTTP_USER_AGENT" => bot_user_agent,
+ },
+ params: {
+ tl: "ja",
+ }
+
+ expect(response.status).to eq(200)
+
+ db_fancy_title = Topic.where(id: topic.id).pick(:fancy_title)
+ localized_fancy_title = Topic.fancy_title(ja_topic.title)
+ expect(db_fancy_title).not_to eq(localized_fancy_title)
+ end
end
it "does not have N+1s when loading localizations" do