Browse Source

alter order of before validation methods on sales log so that derived fields are calculated after invalidated dependent fields are cleared, fix tests broken by this change

pull/1214/head
Arthur Campbell 3 years ago
parent
commit
fb60ea4f7e
  1. 1
      app/models/log.rb
  2. 2
      app/models/sales_log.rb
  3. 40
      spec/models/sales_log_spec.rb

1
app/models/log.rb

@ -108,7 +108,6 @@ private
return unless form return unless form
form.reset_not_routed_questions(self) form.reset_not_routed_questions(self)
reset_created_by! reset_created_by!
end end

2
app/models/sales_log.rb

@ -23,13 +23,13 @@ class SalesLog < Log
has_paper_trail has_paper_trail
validates_with SalesLogValidator validates_with SalesLogValidator
before_validation :set_derived_fields!
before_validation :reset_invalidated_dependent_fields! before_validation :reset_invalidated_dependent_fields!
before_validation :process_postcode_changes!, if: :postcode_full_changed? before_validation :process_postcode_changes!, if: :postcode_full_changed?
before_validation :process_previous_postcode_changes!, if: :ppostcode_full_changed? before_validation :process_previous_postcode_changes!, if: :ppostcode_full_changed?
before_validation :reset_location_fields!, unless: :postcode_known? before_validation :reset_location_fields!, unless: :postcode_known?
before_validation :reset_previous_location_fields!, unless: :previous_postcode_known? before_validation :reset_previous_location_fields!, unless: :previous_postcode_known?
before_validation :set_mortgage_value_zero, if: :mortgage_not_used? before_validation :set_mortgage_value_zero, if: :mortgage_not_used?
before_validation :set_derived_fields!
scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
scope :search_by, ->(param) { filter_by_id(param) } scope :search_by, ->(param) { filter_by_id(param) }

40
spec/models/sales_log_spec.rb

@ -109,7 +109,7 @@ RSpec.describe SalesLog, type: :model do
let(:sales_log) { FactoryBot.create(:sales_log, :completed) } let(:sales_log) { FactoryBot.create(:sales_log, :completed) }
it "correctly derives and saves exday, exmonth and exyear" do it "correctly derives and saves exday, exmonth and exyear" do
sales_log.update!(exdate: Time.gm(2022, 5, 4)) sales_log.update!(exdate: Time.gm(2022, 5, 4), ownershipsch: 1, staircase: 2, resale: 2)
record_from_db = ActiveRecord::Base.connection.execute("select exday, exmonth, exyear from sales_logs where id=#{sales_log.id}").to_a[0] record_from_db = ActiveRecord::Base.connection.execute("select exday, exmonth, exyear from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["exday"]).to eq(4) expect(record_from_db["exday"]).to eq(4)
expect(record_from_db["exmonth"]).to eq(5) expect(record_from_db["exmonth"]).to eq(5)
@ -238,39 +238,49 @@ RSpec.describe SalesLog, type: :model do
end end
context "when deriving household variables" do context "when deriving household variables" do
let!(:household_lettings_log) do let!(:sales_log) do
described_class.create!({ FactoryBot.create(
:sales_log,
:completed,
jointpur: 1, jointpur: 1,
hholdcount: 3, hholdcount: 4,
details_known_1: 1,
details_known_2: 1,
details_known_3: 1,
details_known_4: 1,
relat2: "C", relat2: "C",
relat3: "C", relat3: "C",
relat4: "X", relat4: "X",
relat5: "X", relat5: "X",
age1: 22, relat6: "P",
age2: 40, ecstat2: 9,
age3: 19, ecstat3: 7,
age1: 47,
age2: 14,
age3: 17,
age4: 88, age4: 88,
age5: 14, age5: 19,
}) age6: 46,
)
end end
it "correctly derives and saves hhmemb" do it "correctly derives and saves hhmemb" do
record_from_db = ActiveRecord::Base.connection.execute("select hhmemb from sales_logs where id=#{household_lettings_log.id}").to_a[0] record_from_db = ActiveRecord::Base.connection.execute("select hhmemb from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["hhmemb"]).to eq(5) expect(record_from_db["hhmemb"]).to eq(6)
end end
it "correctly derives and saves totchild" do it "correctly derives and saves totchild" do
record_from_db = ActiveRecord::Base.connection.execute("select totchild from sales_logs where id=#{household_lettings_log.id}").to_a[0] record_from_db = ActiveRecord::Base.connection.execute("select totchild from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["totchild"]).to eq(2) expect(record_from_db["totchild"]).to eq(2)
end end
it "correctly derives and saves totadult" do it "correctly derives and saves totadult" do
record_from_db = ActiveRecord::Base.connection.execute("select totadult from sales_logs where id=#{household_lettings_log.id}").to_a[0] record_from_db = ActiveRecord::Base.connection.execute("select totadult from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["totadult"]).to eq(3) expect(record_from_db["totadult"]).to eq(4)
end end
it "correctly derives and saves hhtype" do it "correctly derives and saves hhtype" do
record_from_db = ActiveRecord::Base.connection.execute("select hhtype from sales_logs where id=#{household_lettings_log.id}").to_a[0] record_from_db = ActiveRecord::Base.connection.execute("select hhtype from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["hhtype"]).to eq(9) expect(record_from_db["hhtype"]).to eq(9)
end end
end end

Loading…
Cancel
Save