Skip to content

Commit 0954baf

Browse files
committed
fixing #494
1 parent 2f6ab02 commit 0954baf

File tree

7 files changed

+217
-5
lines changed

7 files changed

+217
-5
lines changed

app/concepts/matestack/ui/core/form/checkbox/base.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,21 @@ def vue_attributes_for_single_input
7373
(options[:attributes] || {}).merge({
7474
"@change": change_event,
7575
ref: "input.#{attr_key}",
76-
'init-value': init_value,
76+
'init-value': init_value_for_single_input,
7777
'v-bind:class': "{ '#{input_error_class}': #{error_key} }",
7878
"#{v_model_type}": input_key
7979
})
8080
end
8181

82+
def init_value_for_single_input
83+
if init_value == true || init_value == 1
84+
return "true"
85+
end
86+
if init_value == false || init_value == 0
87+
return "false"
88+
end
89+
end
90+
8291
def value_type
8392
item_value(checkbox_options.first).is_a?(Integer) ? Integer : nil
8493
end

app/concepts/matestack/ui/core/form/checkbox/mixin.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,53 @@ const formCheckboxMixin = {
1212
if (key.startsWith("select.multiple.")) {
1313
if (initValue) {
1414
data[key.replace("select.multiple.", "")] = JSON.parse(initValue["value"]);
15+
Object.assign(self.$parent.data, data);
1516
self.afterInitialize(JSON.parse(initValue["value"]))
1617
} else {
1718
data[key.replace("select.multiple.", "")] = [];
19+
Object.assign(self.$parent.data, data);
1820
self.afterInitialize([])
1921
}
2022
} else {
2123
if (initValue) {
2224
if (valueType && valueType["value"] == "Integer") {
2325
data[key.replace("select.", "")] = parseInt(initValue["value"]);
26+
Object.assign(self.$parent.data, data);
2427
self.afterInitialize(parseInt(initValue["value"]))
2528
} else {
29+
2630
data[key.replace("select.", "")] = initValue["value"];
31+
Object.assign(self.$parent.data, data);
2732
self.afterInitialize(initValue["value"])
2833
}
2934
} else {
3035
data[key.replace("select.", "")] = null;
36+
Object.assign(self.$parent.data, data);
3137
self.afterInitialize(null)
3238
}
3339
}
40+
} else {
41+
if (initValue) {
42+
if(initValue["value"] === "true"){
43+
data[key.replace("input.", "")] = true;
44+
Object.assign(self.$parent.data, data);
45+
self.afterInitialize(true)
46+
}
47+
if(initValue["value"] === "false"){
48+
data[key.replace("input.", "")] = false;
49+
Object.assign(self.$parent.data, data);
50+
self.afterInitialize(false)
51+
}
52+
} else {
53+
data[key.replace("input.", "")] = null;
54+
Object.assign(self.$parent.data, data);
55+
self.afterInitialize(null)
56+
}
3457
}
3558
}
3659

3760
//without the timeout it's somehow not working
3861
setTimeout(function () {
39-
Object.assign(self.$parent.data, data);
4062
self.$forceUpdate()
4163
}, 1);
4264
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddBooleanValueToTestModels < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :test_models, :some_boolean_value, :boolean
4+
end
5+
end

spec/dummy/db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_04_27_170812) do
13+
ActiveRecord::Schema.define(version: 2020_12_22_161321) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -66,6 +66,7 @@
6666
t.text "more_data"
6767
t.datetime "created_at", null: false
6868
t.datetime "updated_at", null: false
69+
t.boolean "some_boolean_value"
6970
end
7071

7172
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"

spec/test/components/dynamic/form/checkbox/checkbox_spec.rb

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,159 @@ def form_config
235235
expect(TestModel.last.status).to eq(1)
236236
end
237237

238+
it "can be initialized with a boolean/integer value as true" do
239+
Object.send(:remove_const, :TestModel)
240+
load "#{Rails.root}/app/models/test_model.rb"
241+
242+
class ExamplePage < Matestack::Ui::Page
243+
def prepare
244+
@test_model = TestModel.new
245+
@test_model.status = 1
246+
@test_model.some_boolean_value = true
247+
end
248+
249+
def response
250+
form form_config, :include do
251+
form_checkbox id: "init-as-integer-from-model", key: :status, label: 'Integer Value from Model'
252+
form_checkbox id: "init-as-boolean-from-model", key: :some_boolean_value, label: 'Boolean Value from Model'
253+
form_checkbox id: "init-as-integer-from-config", key: :foo, label: 'Integer Value from Config', init: 1
254+
form_checkbox id: "init-as-boolean-from-config", key: :bar, label: 'Boolean Value from Config', init: true
255+
form_submit do
256+
button text: "Submit me!"
257+
end
258+
toggle show_on: 'success', id: 'async-page' do
259+
plain 'Success'
260+
end
261+
end
262+
end
263+
264+
def form_config
265+
return {
266+
for: @test_model,
267+
method: :post,
268+
path: checkbox_success_form_test_path(id: 42),
269+
success: {
270+
emit: :success
271+
}
272+
}
273+
end
274+
end
275+
276+
visit "/example"
277+
278+
expect(page).to have_field('Integer Value from Model', checked: true)
279+
expect(page).to have_field('Boolean Value from Model', checked: true)
280+
expect(page).to have_field('Integer Value from Config', checked: true)
281+
expect(page).to have_field('Boolean Value from Config', checked: true)
282+
283+
expect_any_instance_of(FormTestController).to receive(:expect_params)
284+
.with(hash_including(test_model: { status: true, some_boolean_value: true, foo: true, bar: true }))
285+
click_button "Submit me!"
286+
287+
end
288+
289+
it "can be initialized with a boolean/integer value as false" do
290+
Object.send(:remove_const, :TestModel)
291+
load "#{Rails.root}/app/models/test_model.rb"
292+
293+
class ExamplePage < Matestack::Ui::Page
294+
def prepare
295+
@test_model = TestModel.new
296+
@test_model.status = 0
297+
@test_model.some_boolean_value = false
298+
end
299+
300+
def response
301+
form form_config, :include do
302+
form_checkbox id: "init-as-integer-from-model", key: :status, label: 'Integer Value from Model'
303+
form_checkbox id: "init-as-boolean-from-model", key: :some_boolean_value, label: 'Boolean Value from Model'
304+
form_checkbox id: "init-as-integer-from-config", key: :foo, label: 'Integer Value from Config', init: 0
305+
form_checkbox id: "init-as-boolean-from-config", key: :bar, label: 'Boolean Value from Config', init: false
306+
form_submit do
307+
button text: "Submit me!"
308+
end
309+
toggle show_on: 'success', id: 'async-page' do
310+
plain 'Success'
311+
end
312+
end
313+
end
314+
315+
def form_config
316+
return {
317+
for: @test_model,
318+
method: :post,
319+
path: checkbox_success_form_test_path(id: 42),
320+
success: {
321+
emit: :success
322+
}
323+
}
324+
end
325+
end
326+
327+
visit "/example"
328+
329+
expect(page).to have_field('Integer Value from Model', checked: false)
330+
expect(page).to have_field('Boolean Value from Model', checked: false)
331+
expect(page).to have_field('Integer Value from Config', checked: false)
332+
expect(page).to have_field('Boolean Value from Config', checked: false)
333+
334+
expect_any_instance_of(FormTestController).to receive(:expect_params)
335+
.with(hash_including(test_model: { status: false, some_boolean_value: false, foo: false, bar: false }))
336+
click_button "Submit me!"
337+
338+
end
339+
340+
it "can be initialized with a boolean/integer value as null" do
341+
Object.send(:remove_const, :TestModel)
342+
load "#{Rails.root}/app/models/test_model.rb"
343+
344+
class ExamplePage < Matestack::Ui::Page
345+
def prepare
346+
@test_model = TestModel.new
347+
# @test_model.status = 0
348+
# @test_model.some_boolean_value = false
349+
end
350+
351+
def response
352+
form form_config, :include do
353+
form_checkbox id: "init-as-integer-from-model", key: :status, label: 'Integer Value from Model'
354+
form_checkbox id: "init-as-boolean-from-model", key: :some_boolean_value, label: 'Boolean Value from Model'
355+
form_checkbox id: "init-as-integer-from-config", key: :foo, label: 'Integer Value from Config' #, init: 0
356+
form_checkbox id: "init-as-boolean-from-config", key: :bar, label: 'Boolean Value from Config' #, init: false
357+
form_submit do
358+
button text: "Submit me!"
359+
end
360+
toggle show_on: 'success', id: 'async-page' do
361+
plain 'Success'
362+
end
363+
end
364+
end
365+
366+
def form_config
367+
return {
368+
for: @test_model,
369+
method: :post,
370+
path: checkbox_success_form_test_path(id: 42),
371+
success: {
372+
emit: :success
373+
}
374+
}
375+
end
376+
end
377+
378+
visit "/example"
379+
380+
expect(page).to have_field('Integer Value from Model', checked: false)
381+
expect(page).to have_field('Boolean Value from Model', checked: false)
382+
expect(page).to have_field('Integer Value from Config', checked: false)
383+
expect(page).to have_field('Boolean Value from Config', checked: false)
384+
385+
expect_any_instance_of(FormTestController).to receive(:expect_params)
386+
.with(hash_including(test_model: { status: nil, some_boolean_value: nil, foo: nil, bar: nil }))
387+
click_button "Submit me!"
388+
389+
end
390+
238391
end
239392

240393
end

vendor/assets/javascripts/dist/matestack-ui-core.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,31 +1073,53 @@ const formCheckboxMixin = {
10731073
if (key.startsWith("select.multiple.")) {
10741074
if (initValue) {
10751075
data[key.replace("select.multiple.", "")] = JSON.parse(initValue["value"]);
1076+
Object.assign(self.$parent.data, data);
10761077
self.afterInitialize(JSON.parse(initValue["value"]))
10771078
} else {
10781079
data[key.replace("select.multiple.", "")] = [];
1080+
Object.assign(self.$parent.data, data);
10791081
self.afterInitialize([])
10801082
}
10811083
} else {
10821084
if (initValue) {
10831085
if (valueType && valueType["value"] == "Integer") {
10841086
data[key.replace("select.", "")] = parseInt(initValue["value"]);
1087+
Object.assign(self.$parent.data, data);
10851088
self.afterInitialize(parseInt(initValue["value"]))
10861089
} else {
1090+
10871091
data[key.replace("select.", "")] = initValue["value"];
1092+
Object.assign(self.$parent.data, data);
10881093
self.afterInitialize(initValue["value"])
10891094
}
10901095
} else {
10911096
data[key.replace("select.", "")] = null;
1097+
Object.assign(self.$parent.data, data);
10921098
self.afterInitialize(null)
10931099
}
10941100
}
1101+
} else {
1102+
if (initValue) {
1103+
if(initValue["value"] === "true"){
1104+
data[key.replace("input.", "")] = true;
1105+
Object.assign(self.$parent.data, data);
1106+
self.afterInitialize(true)
1107+
}
1108+
if(initValue["value"] === "false"){
1109+
data[key.replace("input.", "")] = false;
1110+
Object.assign(self.$parent.data, data);
1111+
self.afterInitialize(false)
1112+
}
1113+
} else {
1114+
data[key.replace("input.", "")] = null;
1115+
Object.assign(self.$parent.data, data);
1116+
self.afterInitialize(null)
1117+
}
10951118
}
10961119
}
10971120

10981121
//without the timeout it's somehow not working
10991122
setTimeout(function () {
1100-
Object.assign(self.$parent.data, data);
11011123
self.$forceUpdate()
11021124
}, 1);
11031125
},

vendor/assets/javascripts/dist/matestack-ui-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)