Skip to content

Commit 2ba204a

Browse files
authored
FIX: boolean field default state mismatch (#353)
When reading an existing state from the params defaults or from URL params, the input elements weren't representing the right state for booleans and "3-state/null booleans". internal /t/-/118495
1 parent bda0c29 commit 2ba204a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

assets/javascripts/discourse/components/param-input-form.gjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ export default class ParamInputForm extends Component {
194194
case "category_id":
195195
return digitalizeCategoryId(value);
196196
case "boolean":
197-
if (value == null) {
197+
if (value == null || value === "#null") {
198198
return info.nullable ? "#null" : false;
199199
}
200-
return value;
200+
return value === "true";
201201
case "group_id":
202202
case "group_list":
203203
const normalized = this.normalizeGroups(value);

spec/system/param_input_spec.rb

+28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
-- bigint :bigint_with_default = 12345678912345
2727
-- boolean :boolean
2828
-- null boolean :boolean_three_with_default = #null
29+
-- boolean :boolean_with_default = true
2930
-- string :string_with_default = little bunny foo foo
3031
-- date :date_with_default = 14 jul 2015
3132
-- time :time_with_default = 5:02 pm
@@ -67,6 +68,33 @@
6768
.create_from_sql(ALL_PARAMS_SQL)
6869
.each do |param|
6970
expect(page).to have_css(".query-params .param [name=\"#{param.identifier}\"]")
71+
72+
# select-kit fields
73+
ignore_fields = %i[user_id post_id topic_id category_id group_id group_list user_list]
74+
75+
if param.default.present? && ignore_fields.exclude?(param.type)
76+
expect(page).to have_field(
77+
param.identifier,
78+
with: simple_normalize(param.type, param.default),
79+
)
80+
end
7081
end
7182
end
7283
end
84+
85+
def simple_normalize(type, value)
86+
case type
87+
when :date
88+
value.to_date.to_s
89+
when :time
90+
value.to_time.strftime("%H:%M")
91+
when :datetime
92+
value.to_datetime.strftime("%Y-%m-%dT%H:%M")
93+
when :boolean
94+
value == "#null" ? "#null" : value ? "on" : "off"
95+
when :boolean_three
96+
value == "#null" ? "#null" : value ? "Y" : "N"
97+
else
98+
value.to_s
99+
end
100+
end

0 commit comments

Comments
 (0)