From 74f2d480186b6cd87a6032ccfadc5721a76eb4b3 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Wed, 19 Feb 2020 10:01:21 +0100 Subject: [PATCH] FIX: makes setting-object capable of defining value/name properties itself (#9003) --- .../admin/mixins/setting-object.js.es6 | 30 +++++++++++ .../components/site-settings/enum.hbs | 3 +- .../mixins/setting-object-test.js.es6 | 50 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 test/javascripts/mixins/setting-object-test.js.es6 diff --git a/app/assets/javascripts/admin/mixins/setting-object.js.es6 b/app/assets/javascripts/admin/mixins/setting-object.js.es6 index f0296e6c553..0d97e798733 100644 --- a/app/assets/javascripts/admin/mixins/setting-object.js.es6 +++ b/app/assets/javascripts/admin/mixins/setting-object.js.es6 @@ -1,5 +1,7 @@ import discourseComputed from "discourse-common/utils/decorators"; +import { computed } from "@ember/object"; import Mixin from "@ember/object/mixin"; +import { isPresent } from "@ember/utils"; export default Mixin.create({ @discourseComputed("value", "default") @@ -10,6 +12,34 @@ export default Mixin.create({ 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") validValues(validValues) { const vals = [], diff --git a/app/assets/javascripts/admin/templates/components/site-settings/enum.hbs b/app/assets/javascripts/admin/templates/components/site-settings/enum.hbs index 648e662f8e3..86faf57d755 100644 --- a/app/assets/javascripts/admin/templates/components/site-settings/enum.hbs +++ b/app/assets/javascripts/admin/templates/components/site-settings/enum.hbs @@ -1,8 +1,9 @@ {{combo-box - valueProperty="value" content=setting.validValues value=value onChange=(action (mut value)) + valueProperty=setting.computedValueProperty + nameProperty=setting.computedNameProperty options=(hash castInteger=true allowAny=setting.allowsNone diff --git a/test/javascripts/mixins/setting-object-test.js.es6 b/test/javascripts/mixins/setting-object-test.js.es6 new file mode 100644 index 00000000000..fa70a0b94e5 --- /dev/null +++ b/test/javascripts/mixins/setting-object-test.js.es6 @@ -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"); +});