Skip to content

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

DEV: Add registerCategorySaveProperty to allow plugins to save other category attributes (#34896)

Natalie Tayae97fb186585
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs
index 29f4b569efc..5d35fcaaee1 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs
@@ -122,6 +122,7 @@ import {
import { addCustomUserFieldValidationCallback } from "discourse/lib/user-fields-validation-helper";
import { registerUserMenuTab } from "discourse/lib/user-menu/tab";
import { replaceFormatter } from "discourse/lib/utilities";
+import { _addCategoryPropertyForSave } from "discourse/models/category";
import Composer, {
registerCustomizationCallback,
} from "discourse/models/composer";
@@ -3398,6 +3399,24 @@ class PluginApi {
registerRichEditorExtension(extension);
}
+ /**
+ * Registers a property that will be included when saving a category.
+ *
+ * This is useful for plugins that are adding additional parameters to the category
+ * and want to save the new property alongside the default category properties
+ * (all under the same save call), and are not using custom fields for that.
+ *
+ * ```
+ * api.registerCategorySaveProperty("property_one");
+ * api.registerCategorySaveProperty("property_two");
+ * ```
+ *
+ * @param {string} property - The name of the property to include when saving a category.
+ */
+ registerCategorySaveProperty(property) {
+ _addCategoryPropertyForSave(property);
+ }
+
#deprecateModifyClass(className) {
if (DEPRECATED_POST_STREAM_CLASSES.includes(className)) {
deprecated(
diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.js
index 4d7dcf0b57a..6873d689446 100644
--- a/app/assets/javascripts/discourse/app/models/category.js
+++ b/app/assets/javascripts/discourse/app/models/category.js
@@ -17,6 +17,19 @@ import Topic from "discourse/models/topic";
const STAFF_GROUP_NAME = "staff";
const CATEGORY_ASYNC_SEARCH_CACHE = {};
const CATEGORY_ASYNC_HIERARCHICAL_SEARCH_CACHE = {};
+const pluginSaveProperties = new Set();
+
+/**
+ * @internal
+ * Adds a tracked property to the post model.
+ *
+ * Intended to be used only in the plugin API.
+ *
+ * @param {string} propertyKey - The key of the property to track.
+ */
+export function _addCategoryPropertyForSave(propertyKey) {
+ pluginSaveProperties.add(propertyKey);
+}
export default class Category extends RestModel {
// Sort subcategories directly under parents
@@ -785,11 +798,19 @@ export default class Category extends RestModel {
...(this.siteSettings.content_localization_enabled && {
category_localizations: this.localizations,
}),
+ ...this._pluginSaveProperties(),
}),
type: id ? "PUT" : "POST",
});
}
+ _pluginSaveProperties() {
+ return Array.from(pluginSaveProperties).reduce((obj, key) => {
+ obj[key] = this[key];
+ return obj;
+ }, {});
+ }
+
_permissionsForUpdate() {
const permissions = this.permissions;
let rval = {};
diff --git a/app/assets/javascripts/discourse/tests/unit/models/category-test.js b/app/assets/javascripts/discourse/tests/unit/models/category-test.js
index d9755da7954..68215b5ee1a 100644
--- a/app/assets/javascripts/discourse/tests/unit/models/category-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/models/category-test.js
@@ -2,6 +2,7 @@ import { getOwner } from "@ember/owner";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import sinon from "sinon";
+import { withPluginApi } from "discourse/lib/plugin-api";
import Category from "discourse/models/category";
import Site from "discourse/models/site";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
@@ -495,4 +496,39 @@ module("Unit | Model | category", function (hooks) {
await Category.asyncFindByIds([12345]);
assert.deepEqual(requestedIds, [[12345, 12346], [12347]]);
});
+
+ test("registerCategorySaveProperty includes property in save request", async function (assert) {
+ const done = assert.async();
+
+ withPluginApi((api) => {
+ api.registerCategorySaveProperty("cats_and_dogs");
+ });
+
+ const category = Category.create({
+ id: 12345,
+ name: "Test Category",
+ cats_and_dogs: 42,
+ group_permissions: [{ permission_type: 1, group_name: "everyone" }],
+ permissions: ["everyone"],
+ });
+
+ pretender.put("/categories/12345", (request) => {
+ const requestBody = JSON.parse(request.requestBody);
+ assert.strictEqual(
+ requestBody.cats_and_dogs,
+ 42,
+ "registered property is included in category save request"
+ );
+ done();
+ return response(200, {
+ category: {
+ id: 12345,
+ name: "Test Category",
+ cats_and_dogs: 42,
+ },
+ });
+ });
+
+ await category.save();
+ });
});