Browse Source

CLDC-654: Infer totadult (#168)

* Infer totadult

* disable rubocop for accessing age and relat fields

* Check if age is nil before checking totadult
pull/170/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
2fe46a0cc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      app/models/case_log.rb
  2. 7
      db/migrate/20211214163230_add_totadult.rb
  3. 3
      db/schema.rb
  4. 14
      spec/models/case_log_spec.rb

14
app/models/case_log.rb

@ -133,7 +133,7 @@ class CaseLog < ApplicationRecord
enum postcode_known: POLAR, _suffix: true enum postcode_known: POLAR, _suffix: true
enum la_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 OPTIONAL_FIELDS = %w[postcode_known
la_known la_known
first_time_property_let_as_social_housing].freeze first_time_property_let_as_social_housing].freeze
@ -215,6 +215,7 @@ private
self.la = get_la(property_postcode) self.la = get_la(property_postcode)
self.totchild = get_totchild self.totchild = get_totchild
self.totelder = get_totelder self.totelder = get_totelder
self.totadult = get_totadult
end end
def get_totelder def get_totelder
@ -227,6 +228,17 @@ private
relationships.count("Child - includes young adult and grown-up") relationships.count("Child - includes young adult and grown-up")
end 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) def get_la(postcode)
if postcode.present? if postcode.present?
postcode_lookup = PIO.lookup(postcode) postcode_lookup = PIO.lookup(postcode)

7
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

3
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -177,6 +177,7 @@ ActiveRecord::Schema.define(version: 2021_12_14_153925) do
t.integer "year" t.integer "year"
t.integer "totchild" t.integer "totchild"
t.integer "totelder" t.integer "totelder"
t.integer "totadult"
t.index ["discarded_at"], name: "index_case_logs_on_discarded_at" 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 ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id"

14
spec/models/case_log_spec.rb

@ -1070,9 +1070,14 @@ RSpec.describe Form, type: :model do
relat3: "Child - includes young adult and grown-up", relat3: "Child - includes young adult and grown-up",
relat4: "Other", relat4: "Other",
relat5: "Child - includes young adult and grown-up", relat5: "Child - includes young adult and grown-up",
age4: 60, relat7: "Other",
relat8: "Other",
age1: 22,
age2: 14, age2: 14,
age4: 60,
age6: 88, age6: 88,
age7: 16,
age8: 42,
}) })
end 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] 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) expect(record_from_db["totelder"]).to eq(2)
end 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 end
end end

Loading…
Cancel
Save