A platform for community discussion. Free, open, simple.
DEV: Deprecate modifyClass for models (#42214)
Jarek Radosz3c8656a5997a
diff --git a/frontend/discourse/app/deprecation-workflow.js b/frontend/discourse/app/deprecation-workflow.jsindex b866bb05a22..df2e290f535 100644--- a/frontend/discourse/app/deprecation-workflow.js+++ b/frontend/discourse/app/deprecation-workflow.js@@ -374,6 +374,7 @@ const workflows = ["discourse.legacy-array-like-object.proxied-array","discourse.legacy-topic-list","discourse.markdown-it.whitelist",+ "discourse.modify-class-model","discourse.nav-item.built-list-site-settings","discourse.navigation-category-controller","discourse.number-field",diff --git a/frontend/discourse/app/lib/plugin-api.gjs b/frontend/discourse/app/lib/plugin-api.gjsindex 9da3462409f..a9d289e8a90 100644--- a/frontend/discourse/app/lib/plugin-api.gjs+++ b/frontend/discourse/app/lib/plugin-api.gjs@@ -283,9 +283,11 @@ class _PluginApi {* }* });* ```+ *+ * Deprecated for `model:` types — use the `addModel*` APIs instead.**/modifyClass(resolverName, changes, opts) {- this.#deprecateModifyClass(resolverName);+ this.#deprecateModifyClass(resolverName, "modifyClass");const klass = this._resolveClass(resolverName, opts);if (!klass) {@@ -322,8 +324,12 @@ class _PluginApi {* superFinder() { return []; }* });* ```+ *+ * Deprecated for `model:` types — use the `addModel*` APIs instead.**/modifyClassStatic(resolverName, changes, opts) {+ this.#deprecateModifyClass(resolverName, "modifyClassStatic");+const klass = this._resolveClass(resolverName, opts);if (!klass) {return;@@ -3871,17 +3877,18 @@ class _PluginApi {_registerConditionType(ConditionClass);}- // eslint-disable-next-line no-unused-vars- #deprecateModifyClass(className) {- // display notification messages for deprecated classes- // e.g:- //- // if (DEPRECATED_CLASSES.includes(className)) {- // deprecated(- // `Using api.modifyClass for \`${className}\` has been deprecated and is no longer a supported override.`,- // DEPRECATION_OPTIONS- // );- // }+ #deprecateModifyClass(resolverName, apiName) {+ if (!resolverName.startsWith("model:")) {+ return;+ }++ deprecated(+ `Using api.${apiName} on \`${resolverName}\` is deprecated. Use the \`addModelField\`, \`addModelAccessor\`/\`addModelGetter\`/\`addModelSetter\`, \`addModelMethod\`, \`addModelSaveProperty\` and \`addModelCallback\` APIs instead.`,+ {+ id: "discourse.modify-class-model",+ since: "2026.8",+ }+ );}}diff --git a/frontend/discourse/tests/unit/lib/plugin-api-test.js b/frontend/discourse/tests/unit/lib/plugin-api-test.jsindex 0a8257e16e6..876f0d4a218 100644--- a/frontend/discourse/tests/unit/lib/plugin-api-test.js+++ b/frontend/discourse/tests/unit/lib/plugin-api-test.js@@ -3,6 +3,7 @@ import EmberObject, { computed } from "@ember/object";import { getOwner } from "@ember/owner";import { setupTest } from "ember-qunit";import { module, test } from "qunit";+import sinon from "sinon";import { block } from "discourse/blocks";import { BlockCondition, blockCondition } from "discourse/blocks/conditions";import { rollbackAllPrepends } from "discourse/lib/class-prepend";@@ -14,6 +15,11 @@ import {isValidOutlet,resetBlockRegistryForTesting,} from "discourse/tests/helpers/block-testing";+import DeprecationCounter from "discourse/tests/helpers/deprecation-counter";+import {+ disableRaiseOnDeprecation,+ enableRaiseOnDeprecation,+} from "discourse/tests/helpers/raise-on-deprecation";module("Unit | Utility | plugin-api", function (hooks) {setupTest(hooks);@@ -245,6 +251,114 @@ module("Unit | Utility | plugin-api", function (hooks) {);});+ module("model deprecation", function (nestedHooks) {+ nestedHooks.beforeEach(function () {+ disableRaiseOnDeprecation();+ // the deprecations below are expected, keep them out of the test output+ // and out of the counter+ sinon.stub(console, "warn");+ this.counterStub = sinon.stub(+ DeprecationCounter.prototype,+ "incrementCount"+ );++ getOwner(this).register(+ "model:test-model",+ class extends EmberObject {+ static someStaticMethod() {+ return "original static";+ }++ someMethod() {+ return "original";+ }+ }+ );+ });++ nestedHooks.afterEach(function () {+ enableRaiseOnDeprecation();+ });++ test("modifyClass deprecates for model types", function (assert) {+ withPluginApi((api) => {+ api.modifyClass(+ "model:test-model",+ (Superclass) =>+ class extends Superclass {+ someMethod() {+ return `${super.someMethod()} modified`;+ }+ }+ );+ });++ assert.true(+ this.counterStub.calledWith("discourse.modify-class-model"),+ "triggers the deprecation"+ );++ assert.strictEqual(+ getOwner(this).lookup("model:test-model").someMethod(),+ "original modified",+ "still applies the modification"+ );+ });++ test("modifyClassStatic deprecates for model types", function (assert) {+ withPluginApi((api) => {+ api.modifyClassStatic("model:test-model", {+ pluginId: "plugin-api-test",++ someStaticMethod() {+ return "overridden static";+ },+ });+ });++ assert.true(+ this.counterStub.calledWith("discourse.modify-class-model"),+ "triggers the deprecation"+ );++ assert.strictEqual(+ getOwner(this)+ .resolveRegistration("model:test-model")+ .someStaticMethod(),+ "overridden static",+ "still applies the modification"+ );+ });++ test("does not deprecate for other types", function (assert) {+ getOwner(this).register(+ "test-thingy:main",+ class {+ someMethod() {+ return "original";+ }+ }+ );++ withPluginApi((api) => {+ api.modifyClass(+ "test-thingy:main",+ (Superclass) =>+ class extends Superclass {+ someMethod() {+ return `${super.someMethod()} modified`;+ }+ }+ );+ });++ assert.false(+ this.counterStub.calledWith("discourse.modify-class-model"),+ "does not trigger the deprecation"+ );+ });+ });+module("Block APIs", function (nestedHooks) {nestedHooks.beforeEach(function () {resetBlockRegistryForTesting();