From 9e5d404377a919fb9c3f879d5b7bbde34ae3061d Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 14 Dec 2022 16:03:32 +0000 Subject: [PATCH] Reuse the same page --- .../sales/pages/person_working_situation.rb | 20 +- ...person_working_situation_joint_purchase.rb | 24 --- .../subsections/household_characteristics.rb | 8 +- ...n_working_situation_joint_purchase_spec.rb | 85 --------- .../pages/person_working_situation_spec.rb | 175 +++++++++++++----- 5 files changed, 151 insertions(+), 161 deletions(-) delete mode 100644 app/models/form/sales/pages/person_working_situation_joint_purchase.rb delete mode 100644 spec/models/form/sales/pages/person_working_situation_joint_purchase_spec.rb diff --git a/app/models/form/sales/pages/person_working_situation.rb b/app/models/form/sales/pages/person_working_situation.rb index d29ea89bf..79331d0b7 100644 --- a/app/models/form/sales/pages/person_working_situation.rb +++ b/app/models/form/sales/pages/person_working_situation.rb @@ -5,20 +5,36 @@ class Form::Sales::Pages::PersonWorkingSituation < ::Form::Page @description = "" @subsection = subsection @depends_on = [ - { "details_known_#{PERSON_INDEX[id] - 1}" => 1, "jointpur" => 2 }, + { "details_known_#{person_display_number}" => 1, "jointpur" => joint_purchase? ? 1 : 2 }, ] end def questions @questions ||= [ - Form::Sales::Questions::PersonWorkingSituation.new("ecstat#{PERSON_INDEX[id]}", nil, self), + Form::Sales::Questions::PersonWorkingSituation.new("ecstat#{person_database_number}", nil, self), ] end + def person_database_number + PERSON_INDEX[id] + end + + def person_display_number + joint_purchase? ? PERSON_INDEX[id] - 2 : PERSON_INDEX[id] - 1 + end + + def joint_purchase? + id.include?("_joint_purchase") + end + PERSON_INDEX = { "person_1_working_situation" => 2, "person_2_working_situation" => 3, "person_3_working_situation" => 4, "person_4_working_situation" => 5, + "person_1_working_situation_joint_purchase" => 3, + "person_2_working_situation_joint_purchase" => 4, + "person_3_working_situation_joint_purchase" => 5, + "person_4_working_situation_joint_purchase" => 6, }.freeze end diff --git a/app/models/form/sales/pages/person_working_situation_joint_purchase.rb b/app/models/form/sales/pages/person_working_situation_joint_purchase.rb deleted file mode 100644 index 35a1acc94..000000000 --- a/app/models/form/sales/pages/person_working_situation_joint_purchase.rb +++ /dev/null @@ -1,24 +0,0 @@ -class Form::Sales::Pages::PersonWorkingSituationJointPurchase < ::Form::Page - def initialize(id, hsh, subsection) - super - @header = "" - @description = "" - @subsection = subsection - @depends_on = [ - { "details_known_#{PERSON_INDEX[id] - 2}" => 1, "jointpur" => 1 }, - ] - end - - def questions - @questions ||= [ - Form::Sales::Questions::PersonWorkingSituation.new("ecstat#{PERSON_INDEX[id]}", nil, self), - ] - end - - PERSON_INDEX = { - "person_1_working_situation_joint_purchase" => 3, - "person_2_working_situation_joint_purchase" => 4, - "person_3_working_situation_joint_purchase" => 5, - "person_4_working_situation_joint_purchase" => 6, - }.freeze -end diff --git a/app/models/form/sales/subsections/household_characteristics.rb b/app/models/form/sales/subsections/household_characteristics.rb index 133e9c38b..65af1044b 100644 --- a/app/models/form/sales/subsections/household_characteristics.rb +++ b/app/models/form/sales/subsections/household_characteristics.rb @@ -34,19 +34,19 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection Form::Sales::Pages::Person1GenderIdentity.new(nil, nil, self), Form::Sales::Pages::Person1GenderIdentityJointPurchase.new(nil, nil, self), Form::Sales::Pages::PersonWorkingSituation.new("person_1_working_situation", nil, self), - Form::Sales::Pages::PersonWorkingSituationJointPurchase.new("person_1_working_situation_joint_purchase", nil, self), + Form::Sales::Pages::PersonWorkingSituation.new("person_1_working_situation_joint_purchase", nil, self), Form::Sales::Pages::Person2Known.new(nil, nil, self), Form::Sales::Pages::Person2Age.new(nil, nil, self), Form::Sales::Pages::PersonWorkingSituation.new("person_2_working_situation", nil, self), - Form::Sales::Pages::PersonWorkingSituationJointPurchase.new("person_2_working_situation_joint_purchase", nil, self), + Form::Sales::Pages::PersonWorkingSituation.new("person_2_working_situation_joint_purchase", nil, self), Form::Sales::Pages::Person3Known.new(nil, nil, self), Form::Sales::Pages::Person3Age.new(nil, nil, self), Form::Sales::Pages::PersonWorkingSituation.new("person_3_working_situation", nil, self), - Form::Sales::Pages::PersonWorkingSituationJointPurchase.new("person_3_working_situation_joint_purchase", nil, self), + Form::Sales::Pages::PersonWorkingSituation.new("person_3_working_situation_joint_purchase", nil, self), Form::Sales::Pages::Person4Known.new(nil, nil, self), Form::Sales::Pages::Person4Age.new(nil, nil, self), Form::Sales::Pages::PersonWorkingSituation.new("person_4_working_situation", nil, self), - Form::Sales::Pages::PersonWorkingSituationJointPurchase.new("person_4_working_situation_joint_purchase", nil, self), + Form::Sales::Pages::PersonWorkingSituation.new("person_4_working_situation_joint_purchase", nil, self), ] end end diff --git a/spec/models/form/sales/pages/person_working_situation_joint_purchase_spec.rb b/spec/models/form/sales/pages/person_working_situation_joint_purchase_spec.rb deleted file mode 100644 index a3ff68910..000000000 --- a/spec/models/form/sales/pages/person_working_situation_joint_purchase_spec.rb +++ /dev/null @@ -1,85 +0,0 @@ -require "rails_helper" - -RSpec.describe Form::Sales::Pages::PersonWorkingSituationJointPurchase, type: :model do - subject(:page) { described_class.new(page_id, page_definition, subsection) } - - let(:page_id) { "person_1_working_situation_joint_purchase" } - let(:page_definition) { nil } - let(:subsection) { instance_double(Form::Subsection) } - - it "has correct subsection" do - expect(page.subsection).to eq(subsection) - end - - it "has the correct header" do - expect(page.header).to eq("") - end - - it "has the correct description" do - expect(page.description).to eq("") - end - - context "with person 1" do - let(:page_id) { "person_1_working_situation_joint_purchase" } - - it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[ecstat3]) - end - - it "has the correct id" do - expect(page.id).to eq("person_1_working_situation_joint_purchase") - end - - it "has correct depends_on" do - expect(page.depends_on).to eq([{ "details_known_1" => 1, "jointpur" => 1 }]) - end - end - - context "with person 2" do - let(:page_id) { "person_2_working_situation_joint_purchase" } - - it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[ecstat4]) - end - - it "has the correct id" do - expect(page.id).to eq("person_2_working_situation_joint_purchase") - end - - it "has correct depends_on" do - expect(page.depends_on).to eq([{ "details_known_2" => 1, "jointpur" => 1 }]) - end - end - - context "with person 3" do - let(:page_id) { "person_3_working_situation_joint_purchase" } - - it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[ecstat5]) - end - - it "has the correct id" do - expect(page.id).to eq("person_3_working_situation_joint_purchase") - end - - it "has correct depends_on" do - expect(page.depends_on).to eq([{ "details_known_3" => 1, "jointpur" => 1 }]) - end - end - - context "with person 4" do - let(:page_id) { "person_4_working_situation_joint_purchase" } - - it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[ecstat6]) - end - - it "has the correct id" do - expect(page.id).to eq("person_4_working_situation_joint_purchase") - end - - it "has correct depends_on" do - expect(page.depends_on).to eq([{ "details_known_4" => 1, "jointpur" => 1 }]) - end - end -end diff --git a/spec/models/form/sales/pages/person_working_situation_spec.rb b/spec/models/form/sales/pages/person_working_situation_spec.rb index 4ba7998cf..d1b13b8c1 100644 --- a/spec/models/form/sales/pages/person_working_situation_spec.rb +++ b/spec/models/form/sales/pages/person_working_situation_spec.rb @@ -3,83 +3,166 @@ require "rails_helper" RSpec.describe Form::Sales::Pages::PersonWorkingSituation, type: :model do subject(:page) { described_class.new(page_id, page_definition, subsection) } - let(:page_id) { "person_1_working_situation" } let(:page_definition) { nil } let(:subsection) { instance_double(Form::Subsection) } - it "has correct subsection" do - expect(page.subsection).to eq(subsection) - end - - it "has the correct header" do - expect(page.header).to eq("") - end - - it "has the correct description" do - expect(page.description).to eq("") - end - - context "with person 1" do + context "without joint purchase" do let(:page_id) { "person_1_working_situation" } - it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[ecstat2]) + it "has correct subsection" do + expect(page.subsection).to eq(subsection) end - it "has the correct id" do - expect(page.id).to eq("person_1_working_situation") + it "has the correct header" do + expect(page.header).to eq("") end - it "has correct depends_on" do - expect(page.depends_on).to eq([{ "details_known_1" => 1, "jointpur" => 2 }]) + it "has the correct description" do + expect(page.description).to eq("") end - end - context "with person 2" do - let(:page_id) { "person_2_working_situation" } + context "with person 1" do + let(:page_id) { "person_1_working_situation" } + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ecstat2]) + end + + it "has the correct id" do + expect(page.id).to eq("person_1_working_situation") + end - it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[ecstat3]) + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "details_known_1" => 1, "jointpur" => 2 }]) + end end - it "has the correct id" do - expect(page.id).to eq("person_2_working_situation") + context "with person 2" do + let(:page_id) { "person_2_working_situation" } + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ecstat3]) + end + + it "has the correct id" do + expect(page.id).to eq("person_2_working_situation") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "details_known_2" => 1, "jointpur" => 2 }]) + end + end + + context "with person 3" do + let(:page_id) { "person_3_working_situation" } + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ecstat4]) + end + + it "has the correct id" do + expect(page.id).to eq("person_3_working_situation") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "details_known_3" => 1, "jointpur" => 2 }]) + end end - it "has correct depends_on" do - expect(page.depends_on).to eq([{ "details_known_2" => 1, "jointpur" => 2 }]) + context "with person 4" do + let(:page_id) { "person_4_working_situation" } + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ecstat5]) + end + + it "has the correct id" do + expect(page.id).to eq("person_4_working_situation") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "details_known_4" => 1, "jointpur" => 2 }]) + end end end - context "with person 3" do - let(:page_id) { "person_3_working_situation" } + context "with joint purchase" do + let(:page_id) { "person_1_working_situation_joint_purchase" } - it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[ecstat4]) + it "has correct subsection" do + expect(page.subsection).to eq(subsection) end - it "has the correct id" do - expect(page.id).to eq("person_3_working_situation") + it "has the correct header" do + expect(page.header).to eq("") end - it "has correct depends_on" do - expect(page.depends_on).to eq([{ "details_known_3" => 1, "jointpur" => 2 }]) + it "has the correct description" do + expect(page.description).to eq("") end - end - context "with person 4" do - let(:page_id) { "person_4_working_situation" } + context "with person 1" do + let(:page_id) { "person_1_working_situation_joint_purchase" } + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ecstat3]) + end - it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[ecstat5]) + it "has the correct id" do + expect(page.id).to eq("person_1_working_situation_joint_purchase") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "details_known_1" => 1, "jointpur" => 1 }]) + end end - it "has the correct id" do - expect(page.id).to eq("person_4_working_situation") + context "with person 2" do + let(:page_id) { "person_2_working_situation_joint_purchase" } + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ecstat4]) + end + + it "has the correct id" do + expect(page.id).to eq("person_2_working_situation_joint_purchase") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "details_known_2" => 1, "jointpur" => 1 }]) + end end - it "has correct depends_on" do - expect(page.depends_on).to eq([{ "details_known_4" => 1, "jointpur" => 2 }]) + context "with person 3" do + let(:page_id) { "person_3_working_situation_joint_purchase" } + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ecstat5]) + end + + it "has the correct id" do + expect(page.id).to eq("person_3_working_situation_joint_purchase") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "details_known_3" => 1, "jointpur" => 1 }]) + end + end + + context "with person 4" do + let(:page_id) { "person_4_working_situation_joint_purchase" } + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ecstat6]) + end + + it "has the correct id" do + expect(page.id).to eq("person_4_working_situation_joint_purchase") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "details_known_4" => 1, "jointpur" => 1 }]) + end end end end