Browse Source

handle derivation of values around buyers living in the property

- derive values where appropriate
- clear these values when the derived state no longer holds
- update the routing for pages holding questions about whether particular buyers will live in the property to reflect when they are derived
- test the deriving and associated clearing of values
- update tests on page routing and sales log factory
pull/1512/head
Arthur Campbell 3 years ago
parent
commit
720a3ec140
  1. 11
      app/models/derived_variables/sales_log_variables.rb
  2. 12
      app/models/form/sales/pages/buyer1_live_in_property.rb
  3. 14
      app/models/form/sales/pages/buyer2_live_in_property.rb
  4. 48
      app/models/sales_log.rb
  5. 21
      spec/models/form/sales/pages/buyer1_live_in_property_spec.rb
  6. 14
      spec/models/form/sales/pages/buyer2_live_in_property_spec.rb
  7. 55
      spec/models/sales_log_spec.rb
  8. 2
      spec/models/validations/sales/household_validations_spec.rb

11
app/models/derived_variables/sales_log_variables.rb

@ -30,6 +30,17 @@ module DerivedVariables::SalesLogVariables
self.uprn = nil self.uprn = nil
self.uprn_known = 0 self.uprn_known = 0
end end
if buyers_will_not_live_in?
self.buy1livein = 2
if joint_purchase?
self.buy2livein = 2
end
end
if buyers_will_live_in? && not_joint_purchase?
self.buy1livein = 1
end
end end
private private

12
app/models/form/sales/pages/buyer1_live_in_property.rb

@ -5,9 +5,21 @@ class Form::Sales::Pages::Buyer1LiveInProperty < ::Form::Page
@depends_on = [ @depends_on = [
{ {
"buyer_has_seen_privacy_notice?" => true, "buyer_has_seen_privacy_notice?" => true,
"outright_sale?" => false,
}, },
{ {
"buyer_not_interviewed?" => true, "buyer_not_interviewed?" => true,
"outright_sale?" => false,
},
{
"buyer_has_seen_privacy_notice?" => true,
"joint_purchase?" => true,
"buyers_will_live_in?" => true,
},
{
"buyer_not_interviewed?" => true,
"joint_purchase?" => true,
"buyers_will_live_in?" => true,
}, },
] ]
end end

14
app/models/form/sales/pages/buyer2_live_in_property.rb

@ -4,12 +4,24 @@ class Form::Sales::Pages::Buyer2LiveInProperty < ::Form::Page
@id = "buyer_2_live_in_property" @id = "buyer_2_live_in_property"
@depends_on = [ @depends_on = [
{ {
"joint_purchase?" => true,
"buyer_has_seen_privacy_notice?" => true, "buyer_has_seen_privacy_notice?" => true,
"outright_sale?" => false,
"joint_purchase?" => true,
},
{
"buyer_not_interviewed?" => true,
"outright_sale?" => false,
"joint_purchase?" => true,
}, },
{ {
"buyer_has_seen_privacy_notice?" => true,
"joint_purchase?" => true, "joint_purchase?" => true,
"buyers_will_live_in?" => true,
},
{
"buyer_not_interviewed?" => true, "buyer_not_interviewed?" => true,
"joint_purchase?" => true,
"buyers_will_live_in?" => true,
}, },
] ]
end end

48
app/models/sales_log.rb

@ -368,12 +368,48 @@ class SalesLog < Log
def ownership_scheme def ownership_scheme
case ownershipsch case ownershipsch
when 1 when 1 then "shared ownership"
"shared ownership" when 2 then "discounted ownership"
when 2 when 3 then "outright sale"
"discounted ownership" end
when 3 end
"outright sale"
private
def reset_invalidated_dependent_fields!
super
reset_derived_questions!
end
def reset_derived_questions!
dependencies = [
{
parent_conditions: [
{ attribute: :buylivein, value: 2 },
],
derived_attributes: %i[buy1livein buy2livein],
},
{
parent_conditions: [
{ attribute: :buylivein, value: 1 },
{ attribute: :jointpur, value: 1 },
],
derived_attributes: [:buy1livein],
},
]
dependencies.each do |dependency|
any_parent_attributes_changed = dependency[:parent_conditions].any? { |parent_condition| send("#{parent_condition[:attribute]}_changed?") }
next unless any_parent_attributes_changed
were_in_derived_state = dependency[:parent_conditions].all? { |parent_condition| send("#{parent_condition[:attribute]}_was") == parent_condition[:value] }
next unless were_in_derived_state
dependency[:derived_attributes].each do |derived_attribute|
Rails.logger.debug("Cleared derived #{derived_attribute} value")
send("#{derived_attribute}=", nil)
end
end end
end end
end end

21
spec/models/form/sales/pages/buyer1_live_in_property_spec.rb

@ -28,6 +28,25 @@ RSpec.describe Form::Sales::Pages::Buyer1LiveInProperty, type: :model do
end end
it "has correct depends_on" do it "has correct depends_on" do
expect(page.depends_on).to eq([{ "buyer_has_seen_privacy_notice?" => true }, { "buyer_not_interviewed?" => true }]) expect(page.depends_on).to eq([
{
"buyer_has_seen_privacy_notice?" => true,
"outright_sale?" => false,
},
{
"buyer_not_interviewed?" => true,
"outright_sale?" => false,
},
{
"buyer_has_seen_privacy_notice?" => true,
"joint_purchase?" => true,
"buyers_will_live_in?" => true,
},
{
"buyer_not_interviewed?" => true,
"joint_purchase?" => true,
"buyers_will_live_in?" => true,
},
])
end end
end end

14
spec/models/form/sales/pages/buyer2_live_in_property_spec.rb

@ -30,12 +30,24 @@ RSpec.describe Form::Sales::Pages::Buyer2LiveInProperty, type: :model do
it "has correct depends_on" do it "has correct depends_on" do
expect(page.depends_on).to eq([ expect(page.depends_on).to eq([
{ {
"joint_purchase?" => true,
"buyer_has_seen_privacy_notice?" => true, "buyer_has_seen_privacy_notice?" => true,
"outright_sale?" => false,
"joint_purchase?" => true,
},
{
"buyer_not_interviewed?" => true,
"outright_sale?" => false,
"joint_purchase?" => true,
}, },
{ {
"buyer_has_seen_privacy_notice?" => true,
"joint_purchase?" => true, "joint_purchase?" => true,
"buyers_will_live_in?" => true,
},
{
"buyer_not_interviewed?" => true, "buyer_not_interviewed?" => true,
"joint_purchase?" => true,
"buyers_will_live_in?" => true,
}, },
]) ])
end end

55
spec/models/sales_log_spec.rb

@ -220,6 +220,61 @@ RSpec.describe SalesLog, type: :model do
record_from_db = ActiveRecord::Base.connection.execute("select mortgage from sales_logs where id=#{sales_log.id}").to_a[0] record_from_db = ActiveRecord::Base.connection.execute("select mortgage from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["mortgage"]).to eq(0.0) expect(record_from_db["mortgage"]).to eq(0.0)
end end
context "when outright sale and buyers will live in the property" do
let(:sales_log) { create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur:) }
context "and the sale is not a joint purchase" do
let(:jointpur) { 2 }
it "derives that buyer 1 will live in the property" do
expect(sales_log.buy1livein).to be 1
end
it "does not derive a value for whether buyer 2 will live in the property" do
expect(sales_log.buy2livein).to be nil
end
it "clears that buyer 1 will live in the property if joint purchase is updated" do
sales_log.update!(jointpur: 1)
expect(sales_log.buy1livein).to be nil
end
end
context "and the sale is a joint purchase" do
let(:jointpur) { 1 }
it "does not derive values for whether buyer 1 or buyer 2 will live in the property" do
expect(sales_log.buy1livein).to be nil
expect(sales_log.buy2livein).to be nil
end
end
end
context "when outright sale and buyers will not live in the property" do
let(:sales_log) { create(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur:) }
context "and the sale is not a joint purchase" do
let(:jointpur) { 2 }
it "derives that buyer 1 will not live in the property" do
expect(sales_log.buy1livein).to be 2
end
it "does not derive a value for whether buyer 2 will live in the property" do
expect(sales_log.buy2livein).to be nil
end
end
context "and the sale is a joint purchase" do
let(:jointpur) { 1 }
it "derives that neither buyer 1 nor buyer 2 will live in the property" do
expect(sales_log.buy1livein).to be 2
expect(sales_log.buy2livein).to be 2
end
end
end
end end
context "when saving addresses" do context "when saving addresses" do

2
spec/models/validations/sales/household_validations_spec.rb

@ -203,7 +203,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
describe "validating fields about buyers living in the property" do describe "validating fields about buyers living in the property" do
let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale_setup_complete, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) }
context "when buyers will live in the property and the sale is a joint purchase" do context "when buyers will live in the property and the sale is a joint purchase" do
let(:buylivein) { 1 } let(:buylivein) { 1 }

Loading…
Cancel
Save