A platform for community discussion. Free, open, simple.
DEV: Supplement updateCategory API doc to include localizations (#33601)
Natalie Tay3a6afa75e728
diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.jsindex 4a5b2aabfb9..4d7dcf0b57a 100644--- a/app/assets/javascripts/discourse/app/models/category.js+++ b/app/assets/javascripts/discourse/app/models/category.js@@ -783,27 +783,13 @@ export default class Category extends RestModel {emoji: this.emoji,icon: this.icon,...(this.siteSettings.content_localization_enabled && {- category_localizations_attributes: this._buildUpdatedLocalizations(),+ category_localizations: this.localizations,}),}),type: id ? "PUT" : "POST",});}- _buildUpdatedLocalizations() {- const localizationsToDestroy = this.category_localizations- .filter(- (original) =>- !this.localizations.some((newLoc) => newLoc.id === original.id)- )- .map((loc) => ({- id: loc.id,- _destroy: true,- }));-- return [...this.localizations, ...localizationsToDestroy];- }-_permissionsForUpdate() {const permissions = this.permissions;let rval = {};diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rbindex 162b2b38c80..d6b4cfb632b 100644--- a/app/controllers/categories_controller.rb+++ b/app/controllers/categories_controller.rb@@ -544,9 +544,7 @@ class CategoriesController < ApplicationControllerendif SiteSetting.content_localization_enabled?- conditional_param_keys << {- category_localizations_attributes: %i[id category_id locale name description _destroy],- }+ conditional_param_keys << { category_localizations: %i[id locale name description] }endresult =diff --git a/app/models/category.rb b/app/models/category.rbindex 34efef54297..2fd277caf97 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -1275,6 +1275,18 @@ class Category < ActiveRecord::Basetags.count > 0 || tag_groups.count > 0end+ def category_localizations=(localizations_params)+ return self.category_localizations_attributes = localizations_params unless persisted?++ incoming_ids = localizations_params.map { |loc| loc["id"] }+ category_localizations+ .where.not(id: incoming_ids)+ .select(:id)+ .each { |record| localizations_params << { "id" => record.id, "_destroy" => true } }++ self.category_localizations_attributes = localizations_params+ end+privatedef ensure_category_settingdiff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rbindex 875c8070fe0..09ba0f188dd 100644--- a/app/serializers/category_serializer.rb+++ b/app/serializers/category_serializer.rb@@ -9,7 +9,7 @@ class CategorySerializer < SiteCategorySerializerendclass CategoryLocalizationSerializer < ApplicationSerializer- attributes :id, :category_id, :locale, :name, :description+ attributes :id, :locale, :name, :descriptionendattributes :read_restricted,diff --git a/spec/requests/api/schemas/json/category_create_request.json b/spec/requests/api/schemas/json/category_create_request.jsonindex 6c8b558c07a..c9ef16d3068 100644--- a/spec/requests/api/schemas/json/category_create_request.json+++ b/spec/requests/api/schemas/json/category_create_request.json@@ -52,9 +52,37 @@"form_template_ids": {"type": "array","items": {}+ },+ "category_localizations": {+ "type": "array",+ "items": {+ "type": "object",+ "properties": {+ "id": {+ "type": "integer",+ "description": "The unique identifier for an existing localization. Must be included otherwise the record will be deleted."+ },+ "locale": {+ "type": "string",+ "description": "The locale for the localization, e.g., 'en', 'zh_CN'. Locale should be in the list of SiteSetting.content_localization_supported_locales."+ },+ "name": {+ "type": "string",+ "description": "The name of the category in the specified locale."+ },+ "description": {+ "type": "string",+ "description": "The description excerpt of the category in the specified locale."+ }+ },+ "required": [+ "locale",+ "name"+ ]+ }}},"required": ["name"]-}\ No newline at end of file+}diff --git a/spec/system/edit_category_localizations_spec.rb b/spec/system/edit_category_localizations_spec.rbindex 467e0f3346a..e797f4568a7 100644--- a/spec/system/edit_category_localizations_spec.rb+++ b/spec/system/edit_category_localizations_spec.rb@@ -61,7 +61,9 @@ describe "Edit Category Localizations", type: :system docategory_page.save_settingspage.refresh- expect(CategoryLocalization.where(category_id: category.id).count).to eq(2)+ try_until_success do+ expect(CategoryLocalization.where(category_id: category.id).count).to eq(2)+ endexpect(CategoryLocalization.where(category_id: category.id, locale: "es").count).to eq(1)expect(CategoryLocalization.where(category_id: category.id, locale: "fr").count).to eq(1)expect(CategoryLocalization.where(category_id: category.id, locale: "es").first.name).to eq(@@ -82,13 +84,15 @@ describe "Edit Category Localizations", type: :system dodescribe "when editing a category with localizations" dofab!(:category_localization) { Fabricate(:category_localization, category: category) }- it "should allow you to delete localizations" do+ it "allows you to delete localizations" doexpect(CategoryLocalization.where(category_id: category.id).count).to eq(1)category_page.visit_edit_localizations(category)page.find(".edit-category-tab-localizations .remove-localization").clickcategory_page.save_settingspage.refresh- expect(CategoryLocalization.where(category_id: category.id).count).to eq(0)+ try_until_success do+ expect(CategoryLocalization.where(category_id: category.id).count).to eq(0)+ endendendend