mirror of
https://github.com/discourse/discourse.git
synced 2025-05-25 00:32:52 +08:00
DEV: refactor setting object mixin to helper class (#30529)
This PR moves the logic from the setting-object mixin to a helper class. I've opted to maintain the interface of the previous classes (ThemeSettings / SiteSetting) that used the mixed-in methods through aliases so that we limit the amount of changes here (these are referenced in the string form and across classes/templates). Another option we may consider in future if we want to optimize for performance is straight duplication which would trade off this overhead of aliasing/chaining calls through the helper for some duplicate code - only 2 models require these methods at the time of this PR.
This commit is contained in:
@ -0,0 +1,78 @@
|
||||
import { dependentKeyCompat } from "@ember/object/compat";
|
||||
import { isPresent } from "@ember/utils";
|
||||
import { deepEqual } from "discourse-common/lib/object";
|
||||
import { i18n } from "discourse-i18n";
|
||||
|
||||
export default class SettingObjectHelper {
|
||||
constructor(settingObj) {
|
||||
this.settingObj = settingObj;
|
||||
}
|
||||
|
||||
@dependentKeyCompat
|
||||
get overridden() {
|
||||
let val = this.settingObj.get("value");
|
||||
let defaultVal = this.settingObj.get("default");
|
||||
|
||||
if (val === null) {
|
||||
val = "";
|
||||
}
|
||||
if (defaultVal === null) {
|
||||
defaultVal = "";
|
||||
}
|
||||
|
||||
return !deepEqual(val, defaultVal);
|
||||
}
|
||||
|
||||
@dependentKeyCompat
|
||||
get computedValueProperty() {
|
||||
if (isPresent(this.settingObj.get("valueProperty"))) {
|
||||
return this.settingObj.get("valueProperty");
|
||||
}
|
||||
|
||||
if (isPresent(this.validValues.get("firstObject.value"))) {
|
||||
return "value";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@dependentKeyCompat
|
||||
get computedNameProperty() {
|
||||
if (isPresent(this.settingObj.get("nameProperty"))) {
|
||||
return this.settingObj.get("nameProperty");
|
||||
}
|
||||
|
||||
if (isPresent(this.validValues.get("firstObject.name"))) {
|
||||
return "name";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@dependentKeyCompat
|
||||
get validValues() {
|
||||
const originalValidValues = this.settingObj.get("valid_values");
|
||||
const values = [];
|
||||
const translateNames = this.settingObj.translate_names;
|
||||
|
||||
(originalValidValues || []).forEach((v) => {
|
||||
if (v.name && v.name.length > 0 && translateNames) {
|
||||
values.addObject({ name: i18n(v.name), value: v.value });
|
||||
} else {
|
||||
values.addObject(v);
|
||||
}
|
||||
});
|
||||
return values;
|
||||
}
|
||||
|
||||
@dependentKeyCompat
|
||||
get allowsNone() {
|
||||
if (this.settingObj.get("valid_values")?.includes("")) {
|
||||
return "admin.settings.none";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@dependentKeyCompat
|
||||
get anyValue() {
|
||||
return this.settingObj.get("allow_any");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user