REFACTOR: Remove less effective method of injecting `siteSettings`

This commit is contained in:
Robin Ward 2020-07-14 13:07:54 -04:00
parent ca13160435
commit a37a19b55c
8 changed files with 55 additions and 39 deletions

View File

@ -171,7 +171,6 @@ const Bookmark = RestModel.extend({
Bookmark.reopenClass({ Bookmark.reopenClass({
create(args) { create(args) {
args = args || {}; args = args || {};
args.siteSettings = args.siteSettings || Discourse.SiteSettings;
args.currentUser = args.currentUser || Discourse.currentUser; args.currentUser = args.currentUser || Discourse.currentUser;
return this._super(args); return this._super(args);
} }

View File

@ -1211,7 +1211,6 @@ Composer.reopenClass({
args = args || {}; args = args || {};
args.user = args.user || User.current(); args.user = args.user || User.current();
args.site = args.site || Site.current(); args.site = args.site || Site.current();
args.siteSettings = args.siteSettings || Discourse.SiteSettings;
return this._super(args); return this._super(args);
}, },

View File

@ -1,6 +1,6 @@
import I18n from "I18n"; import I18n from "I18n";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import EmberObject, { computed, get } from "@ember/object"; import EmberObject, { get } from "@ember/object";
import { isEmpty } from "@ember/utils"; import { isEmpty } from "@ember/utils";
import { equal, and, or, not } from "@ember/object/computed"; import { equal, and, or, not } from "@ember/object/computed";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
@ -18,18 +18,6 @@ import User from "discourse/models/user";
import showModal from "discourse/lib/show-modal"; import showModal from "discourse/lib/show-modal";
const Post = RestModel.extend({ const Post = RestModel.extend({
// TODO: Remove this once one instantiate all `Discourse.Post` models via the store.
siteSettings: computed({
get() {
return Discourse.SiteSettings;
},
// prevents model created from json to overridde this property
set() {
return Discourse.SiteSettings;
}
}),
@discourseComputed("url") @discourseComputed("url")
shareUrl(url) { shareUrl(url) {
const user = User.current(); const user = User.current();

View File

@ -4,6 +4,7 @@ import { autoLoadModules } from "discourse/initializers/auto-load-modules";
import TopicTrackingState from "discourse/models/topic-tracking-state"; import TopicTrackingState from "discourse/models/topic-tracking-state";
import User from "discourse/models/user"; import User from "discourse/models/user";
import Site from "discourse/models/site"; import Site from "discourse/models/site";
import { currentSettings } from "helpers/site-settings";
export default function(name, opts) { export default function(name, opts) {
opts = opts || {}; opts = opts || {};
@ -15,7 +16,7 @@ export default function(name, opts) {
test(name, function(assert) { test(name, function(assert) {
this.site = Site.current(); this.site = Site.current();
this.registry.register("site-settings:main", Discourse.SiteSettings, { this.registry.register("site-settings:main", currentSettings(), {
instantiate: false instantiate: false
}); });
this.registry.register("capabilities:main", EmberObject); this.registry.register("capabilities:main", EmberObject);
@ -25,7 +26,7 @@ export default function(name, opts) {
this.registry.injection("component", "capabilities", "capabilities:main"); this.registry.injection("component", "capabilities", "capabilities:main");
this.registry.injection("component", "site", "site:main"); this.registry.injection("component", "site", "site:main");
this.siteSettings = Discourse.SiteSettings; this.siteSettings = currentSettings();
autoLoadModules(this.registry, this.registry); autoLoadModules(this.registry, this.registry);

View File

@ -4,6 +4,7 @@ import KeyValueStore from "discourse/lib/key-value-store";
import TopicListAdapter from "discourse/adapters/topic-list"; import TopicListAdapter from "discourse/adapters/topic-list";
import TopicTrackingState from "discourse/models/topic-tracking-state"; import TopicTrackingState from "discourse/models/topic-tracking-state";
import { buildResolver } from "discourse-common/resolver"; import { buildResolver } from "discourse-common/resolver";
import { currentSettings } from "helpers/site-settings";
const CatAdapter = RestAdapter.extend({ const CatAdapter = RestAdapter.extend({
primaryKey: "cat_id" primaryKey: "cat_id"
@ -40,7 +41,7 @@ export default function(customLookup = () => {}) {
return this._tracker; return this._tracker;
} }
if (type === "site-settings:main") { if (type === "site-settings:main") {
this._settings = this._settings || Discourse.SiteSettings; this._settings = this._settings || currentSettings();
return this._settings; return this._settings;
} }
return customLookup(type); return customLookup(type);

View File

@ -25,6 +25,7 @@ import { resetCustomPostMessageCallbacks } from "discourse/controllers/topic";
import { _clearSnapshots } from "select-kit/components/composer-actions"; import { _clearSnapshots } from "select-kit/components/composer-actions";
import User from "discourse/models/user"; import User from "discourse/models/user";
import { mapRoutes } from "discourse/mapping-router"; import { mapRoutes } from "discourse/mapping-router";
import { currentSettings, mergeSettings } from "helpers/site-settings";
export function currentUser() { export function currentUser() {
return User.create(sessionFixtures["/session/current.json"].current_user); return User.create(sessionFixtures["/session/current.json"].current_user);
@ -107,7 +108,7 @@ export function controllerModule(name, args = {}) {
setup() { setup() {
this.registry.register("router:main", mapRoutes()); this.registry.register("router:main", mapRoutes());
let controller = this.subject(); let controller = this.subject();
controller.siteSettings = Discourse.SiteSettings; controller.siteSettings = currentSettings();
if (args.setupController) { if (args.setupController) {
args.setupController(controller); args.setupController(controller);
} }
@ -119,8 +120,7 @@ export function controllerModule(name, args = {}) {
export function discourseModule(name, hooks) { export function discourseModule(name, hooks) {
QUnit.module(name, { QUnit.module(name, {
beforeEach() { beforeEach() {
// Give us an API to change site settings without `Discourse.SiteSettings` in tests this.siteSettings = currentSettings();
this.siteSettings = Discourse.SiteSettings;
if (hooks && hooks.beforeEach) { if (hooks && hooks.beforeEach) {
hooks.beforeEach.call(this); hooks.beforeEach.call(this);
} }
@ -161,16 +161,12 @@ export function acceptance(name, options) {
} }
if (options.settings) { if (options.settings) {
Discourse.SiteSettings = jQuery.extend( mergeSettings(options.settings);
true,
Discourse.SiteSettings,
options.settings
);
} }
this.siteSettings = Discourse.SiteSettings; this.siteSettings = currentSettings();
if (options.site) { if (options.site) {
resetSite(Discourse.SiteSettings, options.site); resetSite(currentSettings(), options.site);
} }
clearOutletCache(); clearOutletCache();
@ -186,7 +182,7 @@ export function acceptance(name, options) {
flushMap(); flushMap();
localStorage.clear(); localStorage.clear();
User.resetCurrent(); User.resetCurrent();
resetSite(Discourse.SiteSettings); resetSite(currentSettings());
resetExtraClasses(); resetExtraClasses();
clearOutletCache(); clearOutletCache();
clearHTMLCache(); clearHTMLCache();

View File

@ -1,6 +1,4 @@
// discourse-skip-module const ORIGINAL_SETTINGS = {
Discourse.SiteSettingsOriginal = {
title: "QUnit Discourse Tests", title: "QUnit Discourse Tests",
site_logo_url: "/assets/logo.png", site_logo_url: "/assets/logo.png",
site_logo_url: "/assets/logo.png", site_logo_url: "/assets/logo.png",
@ -103,8 +101,40 @@ Discourse.SiteSettingsOriginal = {
secure_media: false secure_media: false
}; };
Discourse.SiteSettings = jQuery.extend( let siteSettings = Object.assign({}, ORIGINAL_SETTINGS);
true, Discourse.SiteSettings = siteSettings;
{},
Discourse.SiteSettingsOriginal export function currentSettings() {
); return siteSettings;
}
// In debug mode, Ember will decorate objects with setters that remind you to use
// this.set() because they are bound (even if you use `unbound` or `readonly` in templates!).
// Site settings are only ever changed in tests and these warnings are not wanted, so we'll
// strip them when resetting our settings between tests.
function setValue(k, v) {
let desc = Object.getOwnPropertyDescriptor(siteSettings, k);
if (desc && !desc.writable) {
Object.defineProperty(siteSettings, k, { writable: true });
}
siteSettings[k] = v;
}
export function mergeSettings(other) {
for (let p in other) {
if (other.hasOwnProperty(p)) {
setValue(p, other[p]);
}
}
return siteSettings;
}
export function resetSettings() {
for (let p in siteSettings) {
if (siteSettings.hasOwnProperty(p)) {
let v = ORIGINAL_SETTINGS[p];
typeof v !== "undefined" ? setValue(p, v) : delete siteSettings[p];
}
}
return siteSettings;
}

View File

@ -42,6 +42,8 @@
// //
//= require jquery.magnific-popup.min.js //= require jquery.magnific-popup.min.js
let resetSettings = require("helpers/site-settings").resetSettings;
const buildResolver = require("discourse-common/resolver").buildResolver; const buildResolver = require("discourse-common/resolver").buildResolver;
window.setResolver(buildResolver("discourse").create({ namespace: Discourse })); window.setResolver(buildResolver("discourse").create({ namespace: Discourse }));
@ -104,6 +106,7 @@ function resetSite(siteSettings, extras) {
} }
QUnit.testStart(function(ctx) { QUnit.testStart(function(ctx) {
resetSettings();
server = createPretender.default; server = createPretender.default;
createPretender.applyDefaultHandlers(server); createPretender.applyDefaultHandlers(server);
server.handlers = []; server.handlers = [];
@ -149,8 +152,7 @@ QUnit.testStart(function(ctx) {
); );
} }
// Allow our tests to change site settings and have them reset before the next test resetSettings();
Discourse.SiteSettings = dup(Discourse.SiteSettingsOriginal);
let getURL = require("discourse-common/lib/get-url"); let getURL = require("discourse-common/lib/get-url");
getURL.setupURL(null, "http://localhost:3000", ""); getURL.setupURL(null, "http://localhost:3000", "");