FIX: makes setting-object capable of defining value/name properties itself (#9003)

This commit is contained in:
Joffrey JAFFEUX 2020-02-19 10:01:21 +01:00 committed by GitHub
parent 30e2867547
commit 74f2d48018
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 1 deletions

View File

@ -1,5 +1,7 @@
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import { computed } from "@ember/object";
import Mixin from "@ember/object/mixin"; import Mixin from "@ember/object/mixin";
import { isPresent } from "@ember/utils";
export default Mixin.create({ export default Mixin.create({
@discourseComputed("value", "default") @discourseComputed("value", "default")
@ -10,6 +12,34 @@ export default Mixin.create({
return val.toString() !== defaultVal.toString(); return val.toString() !== defaultVal.toString();
}, },
computedValueProperty: computed(
"valueProperty",
"validValues.[]",
function() {
if (isPresent(this.valueProperty)) {
return this.valueProperty;
}
if (isPresent(this.validValues.get("firstObject.value"))) {
return "value";
} else {
return null;
}
}
),
computedNameProperty: computed("nameProperty", "validValues.[]", function() {
if (isPresent(this.nameProperty)) {
return this.nameProperty;
}
if (isPresent(this.validValues.get("firstObject.name"))) {
return "name";
} else {
return null;
}
}),
@discourseComputed("valid_values") @discourseComputed("valid_values")
validValues(validValues) { validValues(validValues) {
const vals = [], const vals = [],

View File

@ -1,8 +1,9 @@
{{combo-box {{combo-box
valueProperty="value"
content=setting.validValues content=setting.validValues
value=value value=value
onChange=(action (mut value)) onChange=(action (mut value))
valueProperty=setting.computedValueProperty
nameProperty=setting.computedNameProperty
options=(hash options=(hash
castInteger=true castInteger=true
allowAny=setting.allowsNone allowAny=setting.allowsNone

View File

@ -0,0 +1,50 @@
import EmberObject from "@ember/object";
import Setting from "admin/mixins/setting-object";
QUnit.module("mixin:setting-object");
QUnit.test("flat array", assert => {
const FooSetting = EmberObject.extend(Setting);
const fooSettingInstance = FooSetting.create({
valid_values: ["foo", "bar"]
});
assert.equal(fooSettingInstance.computedValueProperty, null);
assert.equal(fooSettingInstance.computedNameProperty, null);
});
QUnit.test("object", assert => {
const FooSetting = EmberObject.extend(Setting);
const fooSettingInstance = FooSetting.create({
valid_values: [{ value: "foo", name: "bar" }]
});
assert.equal(fooSettingInstance.computedValueProperty, "value");
assert.equal(fooSettingInstance.computedNameProperty, "name");
});
QUnit.test("no values", assert => {
const FooSetting = EmberObject.extend(Setting);
const fooSettingInstance = FooSetting.create({
valid_values: []
});
assert.equal(fooSettingInstance.computedValueProperty, null);
assert.equal(fooSettingInstance.computedNameProperty, null);
});
QUnit.test("value/name properties defined", assert => {
const FooSetting = EmberObject.extend(Setting);
const fooSettingInstance = FooSetting.create({
valueProperty: "foo",
nameProperty: "bar",
valid_values: []
});
assert.equal(fooSettingInstance.computedValueProperty, "foo");
assert.equal(fooSettingInstance.computedNameProperty, "bar");
});