Skip to content

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

FIX: Ensure `Theme#settings` are instances of `ThemeSettings` (#26481)

Alan Guo Xiang Tan0eda1c96f165
diff --git a/app/assets/javascripts/admin/addon/adapters/theme.js b/app/assets/javascripts/admin/addon/adapters/theme.js
index f6d6fa4bb8a..4f0d7e3651c 100644
--- a/app/assets/javascripts/admin/addon/adapters/theme.js
+++ b/app/assets/javascripts/admin/addon/adapters/theme.js
@@ -1,8 +1,8 @@
import RestAdapter from "discourse/adapters/rest";
-import ThemeSettings from "admin/models/theme-settings";
export default class Theme extends RestAdapter {
jsonMode = true;
+
basePath() {
return "/admin/";
}
@@ -22,13 +22,6 @@ export default class Theme extends RestAdapter {
let mappedParents = theme.get("parent_themes") || [];
mappedParents = mappedParents.map((t) => map[t.id]);
theme.set("parentThemes", mappedParents);
-
- if (theme.settings) {
- theme.set(
- "settings",
- theme.settings.map((setting) => ThemeSettings.create(setting))
- );
- }
});
return results;
diff --git a/app/assets/javascripts/admin/addon/models/theme.js b/app/assets/javascripts/admin/addon/models/theme.js
index c6eda044fba..af01ff1681d 100644
--- a/app/assets/javascripts/admin/addon/models/theme.js
+++ b/app/assets/javascripts/admin/addon/models/theme.js
@@ -5,6 +5,7 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
import RestModel from "discourse/models/rest";
import discourseComputed from "discourse-common/utils/decorators";
import I18n from "discourse-i18n";
+import ThemeSettings from "admin/models/theme-settings";
const THEME_UPLOAD_VAR = 2;
const FIELDS_IDS = [0, 1, 5];
@@ -14,6 +15,16 @@ export const COMPONENTS = "components";
const SETTINGS_TYPE_ID = 5;
class Theme extends RestModel {
+ static munge(json) {
+ if (json.settings) {
+ json.settings = json.settings.map((setting) =>
+ ThemeSettings.create(setting)
+ );
+ }
+
+ return json;
+ }
+
@or("default", "user_selectable") isActive;
@gt("remote_theme.commits_behind", 0) isPendingUpdates;
@gt("editedFields.length", 0) hasEditedFields;
diff --git a/app/assets/javascripts/discourse/tests/unit/models/theme-test.js b/app/assets/javascripts/discourse/tests/unit/models/theme-test.js
index ac4d17e1e02..2f1c831e755 100644
--- a/app/assets/javascripts/discourse/tests/unit/models/theme-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/models/theme-test.js
@@ -1,10 +1,32 @@
import { getOwner } from "@ember/application";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
+import ThemeSettings from "admin/models/theme-settings";
module("Unit | Model | theme", function (hooks) {
setupTest(hooks);
+ test("create munges settings property to ThemeSettings instances", function (assert) {
+ const store = getOwner(this).lookup("service:store");
+
+ const theme = store.createRecord("theme", {
+ settings: [
+ { id: 1, name: "setting1" },
+ { id: 2, name: "setting2" },
+ ],
+ });
+
+ assert.ok(
+ theme.settings[0] instanceof ThemeSettings,
+ "should be an instance of ThemeSettings"
+ );
+
+ assert.ok(
+ theme.settings[1] instanceof ThemeSettings,
+ "should be an instance of ThemeSettings"
+ );
+ });
+
test("can add an upload correctly", function (assert) {
const store = getOwner(this).lookup("service:store");
const theme = store.createRecord("theme");