Browse Source

feat: refactor out person classes and update tests

pull/1093/head
natdeanlewissoftwire 4 years ago
parent
commit
2be86e97f9
  1. 22
      app/models/form/sales/pages/person.rb
  2. 19
      app/models/form/sales/pages/person_relationship_to_buyer_1.rb
  3. 18
      app/models/form/sales/questions/person.rb
  4. 18
      app/models/form/sales/questions/person_relationship_to_buyer_1.rb
  5. 16
      app/models/form/sales/subsections/household_characteristics.rb
  6. 53
      spec/models/form/sales/questions/person_relationship_to_buyer1_spec.rb

22
app/models/form/sales/pages/person.rb

@ -0,0 +1,22 @@
class Form::Sales::Pages::Person < ::Form::Page
def initialize(id, hsh, subsection, person_index:)
super(id, hsh, subsection)
@person_index = person_index
end
def person_display_number
joint_purchase? ? @person_index - 2 : @person_index - 1
end
def joint_purchase?
id.include?("_joint_purchase")
end
def details_known_question_id
"details_known_#{person_display_number}"
end
def field_for_person(field, suffix = "")
[field, @person_index, suffix].join
end
end

19
app/models/form/sales/pages/person_relationship_to_buyer_1.rb

@ -1,28 +1,17 @@
class Form::Sales::Pages::PersonRelationshipToBuyer1 < ::Form::Page class Form::Sales::Pages::PersonRelationshipToBuyer1 < ::Form::Sales::Pages::Person
def initialize(id, hsh, subsection) def initialize(id, hsh, subsection, person_index:)
super super
@header = "" @header = ""
@description = "" @description = ""
@subsection = subsection @subsection = subsection
@depends_on = [ @depends_on = [
{ "details_known_#{person_display_number(PERSON_INDEX)}" => 1, "jointpur" => joint_purchase? ? 1 : 2 }, { details_known_question_id => 1, "jointpur" => joint_purchase? ? 1 : 2 },
] ]
end end
PERSON_INDEX = {
"person_1_relationship_to_buyer_1" => 2,
"person_2_relationship_to_buyer_1" => 3,
"person_3_relationship_to_buyer_1" => 4,
"person_4_relationship_to_buyer_1" => 5,
"person_1_relationship_to_buyer_1_joint_purchase" => 3,
"person_2_relationship_to_buyer_1_joint_purchase" => 4,
"person_3_relationship_to_buyer_1_joint_purchase" => 5,
"person_4_relationship_to_buyer_1_joint_purchase" => 6,
}.freeze
def questions def questions
@questions ||= [ @questions ||= [
Form::Sales::Questions::PersonRelationshipToBuyer1.new("relat#{person_database_number(PERSON_INDEX)}", nil, self), Form::Sales::Questions::PersonRelationshipToBuyer1.new(field_for_person("relat"), nil, self, person_index: @person_index),
] ]
end end
end end

18
app/models/form/sales/questions/person.rb

@ -0,0 +1,18 @@
class Form::Sales::Questions::Person < ::Form::Question
def initialize(id, hsh, page, person_index:)
super(id, hsh, page)
@person_index = person_index
end
def person_display_number
joint_purchase? ? @person_index - 2 : @person_index - 1
end
def joint_purchase?
page.id.include?("_joint_purchase")
end
def field_for_person(field, suffix = "")
[field, @person_index, suffix].join
end
end

18
app/models/form/sales/questions/person_relationship_to_buyer_1.rb

@ -1,13 +1,13 @@
class Form::Sales::Questions::PersonRelationshipToBuyer1 < ::Form::Question class Form::Sales::Questions::PersonRelationshipToBuyer1 < ::Form::Sales::Questions::Person
def initialize(id, hsh, page) def initialize(id, hsh, page, person_index:)
super super
@check_answer_label = "Person #{person_display_number(PERSON_INDEX)}’s relationship to buyer 1" @check_answer_label = "Person #{person_display_number}’s relationship to Buyer 1"
@header = "What is Person #{person_display_number(PERSON_INDEX)}’s relationship to buyer 1?" @header = "What is Person #{person_display_number}’s relationship to Buyer 1?"
@type = "radio" @type = "radio"
@hint_text = "" @hint_text = ""
@page = page @page = page
@answer_options = ANSWER_OPTIONS @answer_options = ANSWER_OPTIONS
@check_answers_card_number = person_database_number(PERSON_INDEX) @check_answers_card_number = person_index
end end
ANSWER_OPTIONS = { ANSWER_OPTIONS = {
@ -16,12 +16,4 @@ class Form::Sales::Questions::PersonRelationshipToBuyer1 < ::Form::Question
"X" => { "value" => "Other" }, "X" => { "value" => "Other" },
"R" => { "value" => "Person prefers not to say" }, "R" => { "value" => "Person prefers not to say" },
}.freeze }.freeze
PERSON_INDEX = {
"relat2" => 2,
"relat3" => 3,
"relat4" => 4,
"relat5" => 5,
"relat6" => 6,
}.freeze
end end

16
app/models/form/sales/subsections/household_characteristics.rb

@ -30,8 +30,8 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection
Form::Sales::Pages::Buyer2LiveInProperty.new(nil, nil, self), Form::Sales::Pages::Buyer2LiveInProperty.new(nil, nil, self),
Form::Sales::Pages::NumberOfOthersInProperty.new(nil, nil, self), Form::Sales::Pages::NumberOfOthersInProperty.new(nil, nil, self),
Form::Sales::Pages::Person1Known.new(nil, nil, self), Form::Sales::Pages::Person1Known.new(nil, nil, self),
Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_1_relationship_to_buyer_1", nil, self), Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_1_relationship_to_buyer_1", nil, self, person_index: 2),
Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_1_relationship_to_buyer_1_joint_purchase", nil, self), Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_1_relationship_to_buyer_1_joint_purchase", nil, self, person_index: 3),
Form::Sales::Pages::PersonAge.new("person_1_age", nil, self), Form::Sales::Pages::PersonAge.new("person_1_age", nil, self),
Form::Sales::Pages::PersonAge.new("person_1_age_joint_purchase", nil, self), Form::Sales::Pages::PersonAge.new("person_1_age_joint_purchase", nil, self),
Form::Sales::Pages::Person1GenderIdentity.new(nil, nil, self), Form::Sales::Pages::Person1GenderIdentity.new(nil, nil, self),
@ -39,22 +39,22 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection
Form::Sales::Pages::PersonWorkingSituation.new("person_1_working_situation", nil, self), Form::Sales::Pages::PersonWorkingSituation.new("person_1_working_situation", nil, self),
Form::Sales::Pages::PersonWorkingSituation.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::Person2Known.new(nil, nil, self),
Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_2_relationship_to_buyer_1", nil, self), Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_2_relationship_to_buyer_1", nil, self, person_index: 3),
Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_2_relationship_to_buyer_1_joint_purchase", nil, self), Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_2_relationship_to_buyer_1_joint_purchase", nil, self, person_index: 4),
Form::Sales::Pages::PersonAge.new("person_2_age", nil, self), Form::Sales::Pages::PersonAge.new("person_2_age", nil, self),
Form::Sales::Pages::PersonAge.new("person_2_age_joint_purchase", nil, self), Form::Sales::Pages::PersonAge.new("person_2_age_joint_purchase", nil, self),
Form::Sales::Pages::PersonWorkingSituation.new("person_2_working_situation", nil, self), Form::Sales::Pages::PersonWorkingSituation.new("person_2_working_situation", nil, self),
Form::Sales::Pages::PersonWorkingSituation.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::Person3Known.new(nil, nil, self),
Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_3_relationship_to_buyer_1", nil, self), Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_3_relationship_to_buyer_1", nil, self, person_index: 4),
Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_3_relationship_to_buyer_1_joint_purchase", nil, self), Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_3_relationship_to_buyer_1_joint_purchase", nil, self, person_index: 5),
Form::Sales::Pages::PersonAge.new("person_3_age", nil, self), Form::Sales::Pages::PersonAge.new("person_3_age", nil, self),
Form::Sales::Pages::PersonAge.new("person_3_age_joint_purchase", nil, self), Form::Sales::Pages::PersonAge.new("person_3_age_joint_purchase", nil, self),
Form::Sales::Pages::PersonWorkingSituation.new("person_3_working_situation", nil, self), Form::Sales::Pages::PersonWorkingSituation.new("person_3_working_situation", nil, self),
Form::Sales::Pages::PersonWorkingSituation.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::Person4Known.new(nil, nil, self),
Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_4_relationship_to_buyer_1", nil, self), Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_4_relationship_to_buyer_1", nil, self, person_index: 5),
Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_4_relationship_to_buyer_1_joint_purchase", nil, self), Form::Sales::Pages::PersonRelationshipToBuyer1.new("person_4_relationship_to_buyer_1_joint_purchase", nil, self, person_index: 6),
Form::Sales::Pages::PersonAge.new("person_4_age", nil, self), Form::Sales::Pages::PersonAge.new("person_4_age", nil, self),
Form::Sales::Pages::PersonAge.new("person_4_age_joint_purchase", nil, self), Form::Sales::Pages::PersonAge.new("person_4_age_joint_purchase", nil, self),
Form::Sales::Pages::PersonWorkingSituation.new("person_4_working_situation", nil, self), Form::Sales::Pages::PersonWorkingSituation.new("person_4_working_situation", nil, self),

53
spec/models/form/sales/questions/person_relationship_to_buyer1_spec.rb

@ -1,11 +1,12 @@
require "rails_helper" require "rails_helper"
RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model do RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model do
subject(:question) { described_class.new(question_id, question_definition, page) } subject(:question) { described_class.new(question_id, question_definition, page, person_index:) }
let(:question_id) { "relat2" } let(:question_id) { "relat2" }
let(:question_definition) { nil } let(:question_definition) { nil }
let(:page) { instance_double(Form::Page) } let(:page) { instance_double(Form::Page) }
let(:person_index) { 2 }
before do before do
allow(page).to receive(:id).and_return("person_1_relationship_to_buyer_1") allow(page).to receive(:id).and_return("person_1_relationship_to_buyer_1")
@ -29,16 +30,17 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
it "has the correct answer_options" do it "has the correct answer_options" do
expect(question.answer_options).to eq({ expect(question.answer_options).to eq({
"P" => { "value" => "Partner" }, "P" => { "value" => "Partner" },
"C" => { "value" => "Child", "hint" => "Must be eligible for child benefit, aged under 16 or under 20 if still in full-time education." }, "C" => { "value" => "Child", "hint" => "Must be eligible for child benefit, aged under 16 or under 20 if still in full-time education." },
"X" => { "value" => "Other" }, "X" => { "value" => "Other" },
"R" => { "value" => "Person prefers not to say" }, "R" => { "value" => "Person prefers not to say" },
}) })
end end
context "when person 1" do context "when person 1" do
context "and not joint purchase" do context "and not joint purchase" do
let(:question_id) { "relat2" } let(:question_id) { "relat2" }
let(:person_index) { 2 }
before do before do
allow(page).to receive(:id).and_return("person_1_relationship_to_buyer_1") allow(page).to receive(:id).and_return("person_1_relationship_to_buyer_1")
@ -49,11 +51,11 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
end end
it "has the correct header" do it "has the correct header" do
expect(question.header).to eq("What is Person 1’s relationship to buyer 1?") expect(question.header).to eq("What is Person 1’s relationship to Buyer 1?")
end end
it "has the correct check_answer_label" do it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Person 1’s relationship to buyer 1") expect(question.check_answer_label).to eq("Person 1’s relationship to Buyer 1")
end end
it "has expected check answers card number" do it "has expected check answers card number" do
@ -62,6 +64,7 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
end end
context "and joint purchase" do context "and joint purchase" do
let(:person_index) { 3 }
let(:question_id) { "relat3" } let(:question_id) { "relat3" }
before do before do
@ -73,11 +76,11 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
end end
it "has the correct header" do it "has the correct header" do
expect(question.header).to eq("What is Person 1’s relationship to buyer 1?") expect(question.header).to eq("What is Person 1’s relationship to Buyer 1?")
end end
it "has the correct check_answer_label" do it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Person 1’s relationship to buyer 1") expect(question.check_answer_label).to eq("Person 1’s relationship to Buyer 1")
end end
it "has expected check answers card number" do it "has expected check answers card number" do
@ -89,6 +92,7 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
context "when person 2" do context "when person 2" do
context "and not joint purchase" do context "and not joint purchase" do
let(:question_id) { "relat3" } let(:question_id) { "relat3" }
let(:person_index) { 3 }
before do before do
allow(page).to receive(:id).and_return("person_2_relationship_to_buyer_1") allow(page).to receive(:id).and_return("person_2_relationship_to_buyer_1")
@ -99,11 +103,11 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
end end
it "has the correct header" do it "has the correct header" do
expect(question.header).to eq("What is Person 2’s relationship to buyer 1?") expect(question.header).to eq("What is Person 2’s relationship to Buyer 1?")
end end
it "has the correct check_answer_label" do it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Person 2’s relationship to buyer 1") expect(question.check_answer_label).to eq("Person 2’s relationship to Buyer 1")
end end
it "has expected check answers card number" do it "has expected check answers card number" do
@ -113,6 +117,7 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
context "and joint purchase" do context "and joint purchase" do
let(:question_id) { "relat4" } let(:question_id) { "relat4" }
let(:person_index) { 4 }
before do before do
allow(page).to receive(:id).and_return("person_2_relationship_to_buyer_1_joint_purchase") allow(page).to receive(:id).and_return("person_2_relationship_to_buyer_1_joint_purchase")
@ -123,11 +128,11 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
end end
it "has the correct header" do it "has the correct header" do
expect(question.header).to eq("What is Person 2’s relationship to buyer 1?") expect(question.header).to eq("What is Person 2’s relationship to Buyer 1?")
end end
it "has the correct check_answer_label" do it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Person 2’s relationship to buyer 1") expect(question.check_answer_label).to eq("Person 2’s relationship to Buyer 1")
end end
it "has expected check answers card number" do it "has expected check answers card number" do
@ -139,6 +144,7 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
context "when person 3" do context "when person 3" do
context "and not joint purchase" do context "and not joint purchase" do
let(:question_id) { "relat4" } let(:question_id) { "relat4" }
let(:person_index) { 4 }
before do before do
allow(page).to receive(:id).and_return("person_3_relationship_to_buyer_1") allow(page).to receive(:id).and_return("person_3_relationship_to_buyer_1")
@ -149,11 +155,11 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
end end
it "has the correct header" do it "has the correct header" do
expect(question.header).to eq("What is Person 3’s relationship to buyer 1?") expect(question.header).to eq("What is Person 3’s relationship to Buyer 1?")
end end
it "has the correct check_answer_label" do it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Person 3’s relationship to buyer 1") expect(question.check_answer_label).to eq("Person 3’s relationship to Buyer 1")
end end
it "has expected check answers card number" do it "has expected check answers card number" do
@ -163,6 +169,7 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
context "and joint purchase" do context "and joint purchase" do
let(:question_id) { "relat5" } let(:question_id) { "relat5" }
let(:person_index) { 5 }
before do before do
allow(page).to receive(:id).and_return("person_3_relationship_to_buyer_1_joint_purchase") allow(page).to receive(:id).and_return("person_3_relationship_to_buyer_1_joint_purchase")
@ -173,11 +180,11 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
end end
it "has the correct header" do it "has the correct header" do
expect(question.header).to eq("What is Person 3’s relationship to buyer 1?") expect(question.header).to eq("What is Person 3’s relationship to Buyer 1?")
end end
it "has the correct check_answer_label" do it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Person 3’s relationship to buyer 1") expect(question.check_answer_label).to eq("Person 3’s relationship to Buyer 1")
end end
it "has expected check answers card number" do it "has expected check answers card number" do
@ -189,6 +196,7 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
context "when person 4" do context "when person 4" do
context "and not joint purchase" do context "and not joint purchase" do
let(:question_id) { "relat5" } let(:question_id) { "relat5" }
let(:person_index) { 5 }
before do before do
allow(page).to receive(:id).and_return("person_4_relationship_to_buyer_1") allow(page).to receive(:id).and_return("person_4_relationship_to_buyer_1")
@ -199,11 +207,11 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
end end
it "has the correct header" do it "has the correct header" do
expect(question.header).to eq("What is Person 4’s relationship to buyer 1?") expect(question.header).to eq("What is Person 4’s relationship to Buyer 1?")
end end
it "has the correct check_answer_label" do it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Person 4’s relationship to buyer 1") expect(question.check_answer_label).to eq("Person 4’s relationship to Buyer 1")
end end
it "has expected check answers card number" do it "has expected check answers card number" do
@ -213,6 +221,7 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
context "and joint purchase" do context "and joint purchase" do
let(:question_id) { "relat6" } let(:question_id) { "relat6" }
let(:person_index) { 6 }
before do before do
allow(page).to receive(:id).and_return("person_4_relationship_to_buyer_1_joint_purchase") allow(page).to receive(:id).and_return("person_4_relationship_to_buyer_1_joint_purchase")
@ -223,11 +232,11 @@ RSpec.describe Form::Sales::Questions::PersonRelationshipToBuyer1, type: :model
end end
it "has the correct header" do it "has the correct header" do
expect(question.header).to eq("What is Person 4’s relationship to buyer 1?") expect(question.header).to eq("What is Person 4’s relationship to Buyer 1?")
end end
it "has the correct check_answer_label" do it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Person 4’s relationship to buyer 1") expect(question.check_answer_label).to eq("Person 4’s relationship to Buyer 1")
end end
it "has expected check answers card number" do it "has expected check answers card number" do

Loading…
Cancel
Save