Browse Source

Merge branch 'main' into CLCD-501/validate_unitletas

pull/171/head
Matthew Phelan 3 years ago
parent
commit
63c5611ca3
  1. 14
      app/models/case_log.rb
  2. 7
      db/migrate/20211214163230_add_totadult.rb
  3. 3
      db/schema.rb
  4. 12
      docs/adr/adr-013-inferring-la-from-postcode.md
  5. 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"

12
docs/adr/adr-013-inferring-la-from-postcode.md

@ -0,0 +1,12 @@
### ADR - 012: Inferring LA from postcode
We use ONS data to infer local authority from postcode in the property information section.
The Office for National Statistics (ONS) publishes the National Statistics
Postcode Lookup (NSPL) and ONS Postcode Directory (ONSPD) datasets,
which may be used to find a local authority district for a postcode when compiling statistics.
We're using postcodes.io API with postcodes_io gem.
Postcodes.io uses OS and ONS data which is updated as soon as new data becomes available.
We are not using OS places API due to the lack of data.
Closest datapoint to LA in OS places api is ADMINISTRATIVE_AREA which does not always match with local authority.

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