From 56268823c55e54174f280cc4043eba5859b66293 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Fri, 7 May 2021 03:28:16 +0530 Subject: [PATCH] confine exclusion to wizard fields --- lib/custom_wizard/wizard.rb | 13 ++++++- .../custom_wizard/steps_controller_spec.rb | 37 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/custom_wizard/wizard.rb b/lib/custom_wizard/wizard.rb index 396b8706..e8370966 100644 --- a/lib/custom_wizard/wizard.rb +++ b/lib/custom_wizard/wizard.rb @@ -250,7 +250,18 @@ class CustomWizard::Wizard def filter_conditional_fields(submission) included_fields = steps.map { |s| s.fields.map { |f| f.id } }.flatten - submission.select { |key, _| included_fields.include?(key) } + submission.select do |key, _| + included_fields.include?(key) || + required_fields.include?(key) || + key.include?("action") + end + end + + def required_fields + %w{ + submitted_at + route_to + } end def final_cleanup! diff --git a/spec/requests/custom_wizard/steps_controller_spec.rb b/spec/requests/custom_wizard/steps_controller_spec.rb index c58f13a2..c312cbb1 100644 --- a/spec/requests/custom_wizard/steps_controller_spec.rb +++ b/spec/requests/custom_wizard/steps_controller_spec.rb @@ -249,4 +249,41 @@ describe CustomWizard::StepsController do expect(response.status).to eq(200) expect(response.parsed_body['final']).to eq(true) end + + it "excludes the non-included conditional fields from the submissions" do + new_template = wizard_template.dup + new_template['steps'][1]['fields'][0]['condition'] = wizard_field_condition_template['condition'] + CustomWizard::Template.save(new_template, skip_jobs: true) + + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Condition will pass" + } + } + + put '/w/super-mega-fun-wizard/steps/step_2.json', params: { + fields: { + step_2_field_1: "1995-04-23" + } + } + + put '/w/super-mega-fun-wizard/steps/step_1.json', params: { + fields: { + step_1_field_1: "Condition will not pass" + } + } + wizard_id = response.parsed_body['wizard']['id'] + wizard = CustomWizard::Wizard.create(wizard_id, user) + submission = wizard.submissions.last + expect(submission.keys).to include("step_2_field_1") + + put '/w/super-mega-fun-wizard/steps/step_2.json', params: { + fields: { + step_2_field_1: "1995-04-23" + } + } + wizard = CustomWizard::Wizard.create(wizard_id, user) + submission = wizard.submissions.last + expect(submission.keys).not_to include("step_2_field_1") + end end