diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 7ceeaecdc..a5cc2eff7 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -133,7 +133,7 @@ class CaseLog < ApplicationRecord enum postcode_known: POLAR, _suffix: true enum la_known: POLAR, _suffix: true - AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype is_la_inferred totchild totelder].freeze + AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype is_la_inferred totchild totelder totadult].freeze OPTIONAL_FIELDS = %w[postcode_known la_known first_time_property_let_as_social_housing].freeze @@ -215,6 +215,7 @@ private self.la = get_la(property_postcode) self.totchild = get_totchild self.totelder = get_totelder + self.totadult = get_totadult end def get_totelder @@ -227,6 +228,17 @@ private relationships.count("Child - includes young adult and grown-up") end + def get_totadult + total = !age1.nil? && age1 >= 16 && age1 < 60 ? 1 : 0 + total + (2..8).count do |i| + # rubocop:disable Style/EvalWithLocation, Security/Eval:: + age = eval("age#{i}") + relat = eval("relat#{i}") + # rubocop:enable Style/EvalWithLocation, Security/Eval:: + !age.nil? && ((age >= 16 && age < 18 && %w[Partner Other].include?(relat)) || age >= 18 && age < 60) + end + end + def get_la(postcode) if postcode.present? postcode_lookup = PIO.lookup(postcode) diff --git a/db/migrate/20211214163230_add_totadult.rb b/db/migrate/20211214163230_add_totadult.rb new file mode 100644 index 000000000..1dc8e1367 --- /dev/null +++ b/db/migrate/20211214163230_add_totadult.rb @@ -0,0 +1,7 @@ +class AddTotadult < ActiveRecord::Migration[6.1] + def change + change_table :case_logs, bulk: true do |t| + t.column :totadult, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 17a7babce..242c019fe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_12_14_153925) do +ActiveRecord::Schema.define(version: 2021_12_14_163230) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -177,6 +177,7 @@ ActiveRecord::Schema.define(version: 2021_12_14_153925) do t.integer "year" t.integer "totchild" t.integer "totelder" + t.integer "totadult" t.index ["discarded_at"], name: "index_case_logs_on_discarded_at" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index ae17489fb..7b82ecd90 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1070,9 +1070,14 @@ RSpec.describe Form, type: :model do relat3: "Child - includes young adult and grown-up", relat4: "Other", relat5: "Child - includes young adult and grown-up", - age4: 60, + relat7: "Other", + relat8: "Other", + age1: 22, age2: 14, + age4: 60, age6: 88, + age7: 16, + age8: 42, }) end @@ -1089,6 +1094,13 @@ RSpec.describe Form, type: :model do record_from_db = ActiveRecord::Base.connection.execute("select totelder from case_logs where id=#{household_case_log.id}").to_a[0] expect(record_from_db["totelder"]).to eq(2) end + + it "correctly derives and saves totadult" do + household_case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select totadult from case_logs where id=#{household_case_log.id}").to_a[0] + expect(record_from_db["totadult"]).to eq(3) + end end end end