From eb93169f86a3db1b01bf6d46271f62f813bfd833 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Fri, 23 Apr 2021 12:22:04 +0530 Subject: [PATCH] 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