A platform for community discussion. Free, open, simple.
FIX: Opening category dropdown should not reset translated name (#35232)
Natalie Tay16a2ff65b2a3
diff --git a/app/models/site.rb b/app/models/site.rbindex e63e3205155..c8b5318a496 100644--- a/app/models/site.rb+++ b/app/models/site.rb@@ -73,33 +73,20 @@ class Sitecategories =beginquery =- Category.includes(- :uploaded_logo,- :uploaded_logo_dark,- :uploaded_background,- :uploaded_background_dark,- :tags,- :tag_groups,- :form_templates,- category_required_tag_groups: :tag_group,- ).joins("LEFT JOIN topics t on t.id = categories.topic_id")-- if SiteSetting.content_localization_enabled- locale = I18n.locale.to_s- query =- query.joins(- "LEFT JOIN category_localizations cl ON cl.category_id = categories.id AND cl.locale = '#{ActiveRecord::Base.connection.quote_string(locale)}'",- ).select(- "categories.*,- t.slug topic_slug,- COALESCE(cl.name, categories.name) AS name,- COALESCE(cl.description, categories.description) AS description",+ Category+ .includes(+ :uploaded_logo,+ :uploaded_logo_dark,+ :uploaded_background,+ :uploaded_background_dark,+ :tags,+ :tag_groups,+ :form_templates,+ category_required_tag_groups: :tag_group,)- else- query = query.select("categories.*, t.slug topic_slug")- end-- query = query.order(:position)+ .joins("LEFT JOIN topics t on t.id = categories.topic_id")+ .select("categories.*, t.slug topic_slug")+ .order(:position)query =DiscoursePluginRegistry.apply_modifier(:site_all_categories_cache_query, query, self)query.to_adiff --git a/app/serializers/basic_category_serializer.rb b/app/serializers/basic_category_serializer.rbindex fadb09d05d6..770d2e9f412 100644--- a/app/serializers/basic_category_serializer.rb+++ b/app/serializers/basic_category_serializer.rb@@ -1,6 +1,7 @@# frozen_string_literal: trueclass BasicCategorySerializer < ApplicationSerializer+ include BasicCategoryAttributesattributes :id,:name,:color,@@ -45,14 +46,6 @@ class BasicCategorySerializer < ApplicationSerializerparent_category_idend- def name- if object.uncategorized?- I18n.t("uncategorized_category_name", locale: SiteSetting.default_locale)- else- object.name- end- end-def description_textif object.uncategorized?I18n.t("category.uncategorized_description", locale: SiteSetting.default_locale)@@ -61,14 +54,6 @@ class BasicCategorySerializer < ApplicationSerializerendend- def description- if object.uncategorized?- I18n.t("category.uncategorized_description", locale: SiteSetting.default_locale)- else- object.description- end- end-def description_excerptif object.uncategorized?I18n.t("category.uncategorized_description", locale: SiteSetting.default_locale)diff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rbindex 09ba0f188dd..c87e88214a9 100644--- a/app/serializers/category_serializer.rb+++ b/app/serializers/category_serializer.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass CategorySerializer < SiteCategorySerializer+ include BasicCategoryAttributes+class CategorySettingSerializer < ApplicationSerializerattributes :auto_bump_cooldown_days,:num_auto_bump_daily,diff --git a/app/serializers/concerns/basic_category_attributes.rb b/app/serializers/concerns/basic_category_attributes.rbnew file mode 100644index 00000000000..dd8dc54c9e7--- /dev/null+++ b/app/serializers/concerns/basic_category_attributes.rb@@ -0,0 +1,19 @@+# frozen_string_literal: true++module BasicCategoryAttributes+ def name+ if object.uncategorized?+ I18n.t("uncategorized_category_name", locale: SiteSetting.default_locale)+ else+ object.name+ end+ end++ def description+ if object.uncategorized?+ I18n.t("category.uncategorized_description", locale: SiteSetting.default_locale)+ else+ object.description+ end+ end+enddiff --git a/app/serializers/site_category_serializer.rb b/app/serializers/site_category_serializer.rbindex 67a27c980b2..77e18bc6bc7 100644--- a/app/serializers/site_category_serializer.rb+++ b/app/serializers/site_category_serializer.rb@@ -36,4 +36,28 @@ class SiteCategorySerializer < BasicCategorySerializerdef include_required_tag_groups?SiteSetting.tagging_enabledend++ def name+ return I18n.t("uncategorized_category_name") if object.uncategorized?++ translated_name =+ if (ContentLocalization.show_translated_category?(object, scope))+ object.get_localization&.name+ else+ object.name+ end++ translated_name || object.name+ end++ def description+ translated_description =+ if (ContentLocalization.show_translated_category?(object, scope))+ object.get_localization&.description+ else+ object.description+ end++ translated_description || object.description+ endenddiff --git a/spec/system/category_localizations_spec.rb b/spec/system/category_localizations_spec.rbindex 457ee0df905..e96adfa999f 100644--- a/spec/system/category_localizations_spec.rb+++ b/spec/system/category_localizations_spec.rb@@ -29,8 +29,7 @@ describe "Category Localizations", type: :system dodef get_category_dropdown(nth)selector = ".category-breadcrumb li:nth-child(#{nth}) .category-drop"expect(page).to have_css(selector)- el = find(selector, visible: :all)- PageObjects::Components::SelectKit.new(el)+ PageObjects::Components::SelectKit.new(selector)endcontext "when content localization setting is disabled" do@@ -138,20 +137,14 @@ describe "Category Localizations", type: :system docount: 2,)expect(- category_page.find("#control-localizations-0-locale option.--selected"),- ).to have_content("Spanish (Español)")- expect(- category_page.find("#control-localizations-1-locale option.--selected"),- ).to have_content("Japanese (日本語)")+ page.all(".form-kit__control-select option.--selected").map(&:text),+ ).to contain_exactly("Spanish (Español)", "Japanese (日本語)")page.find(".edit-category-tab-localizations .remove-localization", match: :first).clickcategory_page.save_settingspage.refreshexpect(category_page).to_not have_css("#control-localizations-0-locale option.--selected")- expect(- category_page.find("#control-localizations-0-locale option.--selected"),- ).to have_content("Japanese (日本語)")endendend@@ -223,6 +216,26 @@ describe "Category Localizations", type: :system doexpect(topic_list.topic(cat_topic)).to have_text("Solicitudes")end++ it "keeps the translated category name when navigating category dropdown" do+ visit("/latest")+ switcher.expand+ switcher.option("[data-menu-option-id='es']").click++ category_dropdown = get_category_dropdown("1")++ expect(category_dropdown.component).to have_text(+ I18n.t("js.categories.categories_label", locale: "es"),+ )+ category_dropdown.component.click+ expect(category_dropdown.component).to have_css(".is-expanded")+ expect(category_dropdown.component.find(".select-kit-body")).to have_css(+ ".select-kit-collection",+ )+ expect(page.find(".select-kit-collection div[data-name='Solicitudes']")).to have_text(+ "Solicitudes",+ )+ endenddescribe "for anonymous users" do@@ -230,6 +243,20 @@ describe "Category Localizations", type: :system doenddescribe "logged in users" do+ shared_examples_for "editing category settings" do+ it "shows the original category name in the category edit page" do+ sign_in(admin)+ category_page.visit_general(category)++ switcher.expand+ switcher.option("[data-menu-option-id='es']").click++ expect(find(".edit-category-tab-general input.category-name").value).to eq(+ category.name,+ )+ end+ end+describe "lazy loaded categories" dobefore doSiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}"@@ -237,6 +264,8 @@ describe "Category Localizations", type: :system doendit_behaves_like "navigating the site via various category links"++ it_behaves_like "editing category settings"enddescribe "no lazy loaded categories" do@@ -246,6 +275,8 @@ describe "Category Localizations", type: :system doendit_behaves_like "navigating the site via various category links"++ it_behaves_like "editing category settings"endendend