From 911140fde3801dac16456d2315b333d641d755f7 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 22 Apr 2021 23:50:26 +0530 Subject: [PATCH 1/4] improved the logic to accomodate multiple conditions using 'or' --- lib/custom_wizard/mapper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index c1187b0f..9c065237 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -45,8 +45,9 @@ class CustomWizard::Mapper def perform multiple = @opts[:multiple] perform_result = multiple ? [] : nil + input_size = inputs.size - inputs.each do |input| + inputs.each_with_index do |input, index| input_type = input['type'] pairs = input['pairs'] @@ -69,6 +70,8 @@ class CustomWizard::Mapper if multiple perform_result.push(result) + elsif result != true && index < (input_size - 1) + next else perform_result = result break From 2a8a4367d84082223bed03a306ca43eac2746224 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 22 Apr 2021 23:50:54 +0530 Subject: [PATCH 2/4] added specs --- spec/components/custom_wizard/mapper_spec.rb | 60 +++++++++++++++----- spec/fixtures/mapper/inputs.json | 29 ++++++++++ 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index aa34f9f1..ca7de0fe 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -130,21 +130,53 @@ describe CustomWizard::Mapper do end end - it "validates valid data" do - expect(CustomWizard::Mapper.new( - inputs: inputs['validation'], - data: data, - user: user1 - ).perform).to eq(true) - end + context "conditional validation" do + it "validates valid data" do + expect(CustomWizard::Mapper.new( + inputs: inputs['validation'], + data: data, + user: user1 + ).perform).to eq(true) + end - it "does not validate invalid data" do - data["input_2"] = "value 3" - expect(CustomWizard::Mapper.new( - inputs: inputs['validation'], - data: data, - user: user1 - ).perform).to eq(false) + it "does not validate invalid data" do + data["input_2"] = "value 3" + expect(CustomWizard::Mapper.new( + inputs: inputs['validation'], + data: data, + user: user1 + ).perform).to eq(false) + end + context "using or condition" do + it "validates the data when all of the conditions are met" do + expect(CustomWizard::Mapper.new( + inputs: inputs['validation_multiple_pairs'], + data: data, + user: user1 + ).perform).to eq(true) + end + + it "validates the data when one of the conditions are met" do + custom_data = data.dup + custom_data['input_1'] = 'value 3' + expect(CustomWizard::Mapper.new( + inputs: inputs['validation_multiple_pairs'], + data: custom_data, + user: user1 + ).perform).to eq(true) + end + + it "doesn't validate the data when none of the conditions are met" do + custom_data = data.dup + custom_data['input_1'] = 'value 3' + custom_data['input_2'] = 'value 4' + expect(CustomWizard::Mapper.new( + inputs: inputs['validation_multiple_pairs'], + data: custom_data, + user: user1 + ).perform).to eq(false) + end + end end it "maps text fields" do diff --git a/spec/fixtures/mapper/inputs.json b/spec/fixtures/mapper/inputs.json index 8faf2435..f7d98903 100644 --- a/spec/fixtures/mapper/inputs.json +++ b/spec/fixtures/mapper/inputs.json @@ -260,5 +260,34 @@ } ] } + ], + "validation_multiple_pairs": [ + { + "type": "validation", + "pairs": [ + { + "index": 0, + "key": "input_1", + "key_type": "wizard_field", + "value": "value 1", + "value_type": "text", + "connector": "equal" + } + ] + }, + { + "type": "validation", + "connector": "or", + "pairs": [ + { + "index": 0, + "key": "input_2", + "key_type": "wizard_field", + "value": "value 2", + "value_type": "text", + "connector": "equal" + } + ] + } ] } \ No newline at end of file From be1eab609293eb1abeaf188e1ad273256d8ac8bc Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 22 Apr 2021 23:52:38 +0530 Subject: [PATCH 3/4] minor formatting --- spec/components/custom_wizard/mapper_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index ca7de0fe..701f9555 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -147,6 +147,7 @@ describe CustomWizard::Mapper do user: user1 ).perform).to eq(false) end + context "using or condition" do it "validates the data when all of the conditions are met" do expect(CustomWizard::Mapper.new( From eb93169f86a3db1b01bf6d46271f62f813bfd833 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Fri, 23 Apr 2021 12:22:04 +0530 Subject: [PATCH 4/4] use option multiple and aggregate the result --- lib/custom_wizard/builder.rb | 9 +++++++-- lib/custom_wizard/mapper.rb | 5 +---- spec/components/custom_wizard/mapper_spec.rb | 21 ++++++++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index daa73476..3e86c9c4 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -192,11 +192,16 @@ class CustomWizard::Builder def check_condition(template) if template['condition'].present? - CustomWizard::Mapper.new( + result = CustomWizard::Mapper.new( inputs: template['condition'], user: @wizard.user, - data: @wizard.current_submission + data: @wizard.current_submission, + opts: { + multiple: true + } ).perform + + result.any? else true end diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index 9c065237..c1187b0f 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -45,9 +45,8 @@ class CustomWizard::Mapper def perform multiple = @opts[:multiple] perform_result = multiple ? [] : nil - input_size = inputs.size - inputs.each_with_index do |input, index| + inputs.each do |input| input_type = input['type'] pairs = input['pairs'] @@ -70,8 +69,6 @@ class CustomWizard::Mapper if multiple perform_result.push(result) - elsif result != true && index < (input_size - 1) - next else perform_result = result break diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index 701f9555..434f0001 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -153,8 +153,11 @@ describe CustomWizard::Mapper do expect(CustomWizard::Mapper.new( inputs: inputs['validation_multiple_pairs'], data: data, - user: user1 - ).perform).to eq(true) + user: user1, + opts: { + multiple: true + } + ).perform.any?).to eq(true) end it "validates the data when one of the conditions are met" do @@ -163,8 +166,11 @@ describe CustomWizard::Mapper do expect(CustomWizard::Mapper.new( inputs: inputs['validation_multiple_pairs'], data: custom_data, - user: user1 - ).perform).to eq(true) + user: user1, + opts: { + multiple: true + } + ).perform.any?).to eq(true) end it "doesn't validate the data when none of the conditions are met" do @@ -174,8 +180,11 @@ describe CustomWizard::Mapper do expect(CustomWizard::Mapper.new( inputs: inputs['validation_multiple_pairs'], data: custom_data, - user: user1 - ).perform).to eq(false) + user: user1, + opts: { + multiple: true + } + ).perform.any?).to eq(false) end end end