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.rbindex 5f560fadb64..4be1c9f0d1f 100644--- a/lib/localization_attributes_replacer.rb+++ b/lib/localization_attributes_replacer.rb@@ -15,8 +15,17 @@ module LocalizationAttributesReplacerdef 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),+ )+ endtopic.excerpt = loc.excerpt if loc.excerpt.present?enddiff --git a/spec/lib/localization_attributes_replacer_spec.rb b/spec/lib/localization_attributes_replacer_spec.rbindex 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 doFabricate(: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" doLocalizationAttributesReplacer.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" doLocalizationAttributesReplacer.replace_topic_attributes(topic, "en")diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 94adc855ce5..2bf0e5fdc06 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -6166,6 +6166,24 @@ RSpec.describe TopicsController doexpect(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)+ endendit "does not have N+1s when loading localizations" do