Browse Source

refactor on deriving and clearing invalid derived values on sales log

pull/1512/head
Arthur Campbell 3 years ago
parent
commit
0cbc880309
  1. 74
      app/models/derived_variables/sales_log_variables.rb
  2. 40
      app/models/sales_log.rb
  3. 9
      spec/models/sales_log_spec.rb

74
app/models/derived_variables/sales_log_variables.rb

@ -1,5 +1,7 @@
module DerivedVariables::SalesLogVariables
def set_derived_fields!
reset_invalidated_derived_values!
self.ethnic = 17 if ethnic_refused?
self.mscharge = nil if no_monthly_leasehold_charges?
if exdate.present?
@ -13,9 +15,6 @@ module DerivedVariables::SalesLogVariables
self.hoyear = hodate.year
end
self.deposit = value if outright_sale? && mortgage_not_used?
if mortgage_not_used?
self.mortgage = 0
end
self.pcode1, self.pcode2 = postcode_full.split(" ") if postcode_full.present?
self.totchild = total_child
self.totadult = total_adult + total_elder
@ -31,20 +30,73 @@ module DerivedVariables::SalesLogVariables
self.uprn_known = 0
end
if buyers_will_not_live_in?
self.buy1livein = 2
if joint_purchase?
self.buy2livein = 2
set_encoded_derived_values!
end
private
DEPENDENCIES = [
{
conditions: {
buylivein: 2,
},
derived_values: {
buy1livein: 2,
}
},
{
conditions: {
buylivein: 2,
jointpur: 1,
},
derived_values: {
buy1livein: 2,
buy2livein: 2,
}
},
{
conditions: {
buylivein: 1,
jointpur: 2,
},
derived_values: {
buy1livein: 1,
},
},
{
conditions: {
mortgageused: 2,
},
derived_values: {
mortgage: 0,
},
},
].freeze
def reset_invalidated_derived_values!
DEPENDENCIES.each do |dependency|
any_conditions_changed = dependency[:conditions].any? { |attribute, _value| send("#{attribute}_changed?") }
next unless any_conditions_changed
previously_in_derived_state = dependency[:conditions].all? { |attribute, value| send("#{attribute}_was") == value }
next unless previously_in_derived_state
dependency[:derived_values].each do |derived_attribute, _derived_value|
Rails.logger.debug("Cleared derived #{derived_attribute} value")
send("#{derived_attribute}=", nil)
end
end
end
if buyers_will_live_in? && not_joint_purchase?
self.buy1livein = 1
def set_encoded_derived_values!
DEPENDENCIES.each do |dependency|
derivation_applies = dependency[:conditions].all? { |attribute, value| send(attribute) == value }
if derivation_applies
dependency[:derived_values].each { |attribute, value| send("#{attribute}=", value) }
end
end
end
private
def number_of_household_members
return unless hholdcount.present? && jointpur.present?

40
app/models/sales_log.rb

@ -26,7 +26,6 @@ class SalesLog < Log
validates_with SalesLogValidator
before_validation :recalculate_start_year!, if: :saledate_changed?
before_validation :reset_invalidated_dependent_fields!
before_validation :process_postcode_changes!, if: :postcode_full_changed?
before_validation :process_previous_postcode_changes!, if: :ppostcode_full_changed?
before_validation :reset_location_fields!, unless: :postcode_known?
@ -373,43 +372,4 @@ class SalesLog < Log
when 3 then "outright sale"
end
end
private
def reset_invalidated_dependent_fields!
super
reset_derived_questions!
end
def reset_derived_questions!
dependencies = [
{
conditions: {
buylivein: 2,
},
derived_attributes: %i[buy1livein buy2livein],
},
{
conditions: {
buylivein: 1,
jointpur: 1,
},
derived_attributes: [:buy1livein],
},
]
dependencies.each do |dependency|
any_primary_attributes_changed = dependency[:conditions].any? { |attribute, _value| send("#{attribute}_changed?") }
next unless any_primary_attributes_changed
previously_in_derived_state = dependency[:conditions].all? { |attribute, value| send("#{attribute}_was") == value }
next unless previously_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

9
spec/models/sales_log_spec.rb

@ -221,6 +221,15 @@ RSpec.describe SalesLog, type: :model do
expect(record_from_db["mortgage"]).to eq(0.0)
end
it "clears mortgage value if mortgage used is changed from no to yes" do
# to avoid log failing validations when mortgage value is removed:
new_grant_value = sales_log.grant + sales_log.mortgage
sales_log.update!(mortgageused: 2, grant: new_grant_value)
sales_log.update!(mortgageused: 1)
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(nil)
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:) }

Loading…
Cancel
Save