Browse Source

CLDC-653: totchild derived field (#166)

* Add missing mappings and infer totchild

* Extract method;
pull/167/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
ac6e8c23b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      app/models/case_log.rb
  2. 14
      app/models/constants/case_log.rb
  3. 7
      db/migrate/20211214143841_add_totchild.rb
  4. 3
      db/schema.rb
  5. 21
      spec/models/case_log_spec.rb

22
app/models/case_log.rb

@ -56,6 +56,20 @@ class CaseLog < ApplicationRecord
enum ecstat6: ECSTAT, _suffix: true
enum ecstat7: ECSTAT, _suffix: true
enum ecstat8: ECSTAT, _suffix: true
enum relat2: RELAT, _suffix: true
enum relat3: RELAT, _suffix: true
enum relat4: RELAT, _suffix: true
enum relat5: RELAT, _suffix: true
enum relat6: RELAT, _suffix: true
enum relat7: RELAT, _suffix: true
enum relat8: RELAT, _suffix: true
enum sex2: GENDER, _suffix: true
enum sex3: GENDER, _suffix: true
enum sex4: GENDER, _suffix: true
enum sex5: GENDER, _suffix: true
enum sex6: GENDER, _suffix: true
enum sex7: GENDER, _suffix: true
enum sex8: GENDER, _suffix: true
enum prevten: PREVIOUS_TENANCY, _suffix: true
enum homeless: HOMELESS, _suffix: true
enum underoccupation_benefitcap: BENEFITCAP, _suffix: true
@ -119,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].freeze
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype is_la_inferred totchild].freeze
OPTIONAL_FIELDS = %w[postcode_known
la_known
first_time_property_let_as_social_housing].freeze
@ -199,6 +213,12 @@ private
self.lettype = "#{renttype} #{needstype} #{owning_organisation['Org type']}" if renttype.present? && needstype.present? && owning_organisation["Org type"].present?
self.is_la_inferred = false if is_la_inferred.nil?
self.la = get_la(property_postcode)
self.totchild = get_totchild
end
def get_totchild
relationships = [relat2, relat3, relat4, relat5, relat6, relat7, relat8]
relationships.count("Child - includes young adult and grown-up")
end
def get_la(postcode)

14
app/models/constants/case_log.rb

@ -733,4 +733,18 @@ module Constants::CaseLog
"Waltham Forest",
"Wandsworth",
"Westminster"].freeze
RELAT = {
"Child - includes young adult and grown-up" => "C",
"Partner" => "P",
"Other" => "X",
"Prefer not to say" => "R",
}.freeze
GENDER = {
"Female" => "F",
"Male" => "M",
"Non-binary" => "X",
"Prefer not to say" => "R",
}.freeze
end

7
db/migrate/20211214143841_add_totchild.rb

@ -0,0 +1,7 @@
class AddTotchild < ActiveRecord::Migration[6.1]
def change
change_table :case_logs, bulk: true do |t|
t.column :totchild, :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.
ActiveRecord::Schema.define(version: 2021_12_13_122642) do
ActiveRecord::Schema.define(version: 2021_12_14_143841) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -175,6 +175,7 @@ ActiveRecord::Schema.define(version: 2021_12_13_122642) do
t.integer "month"
t.integer "year"
t.boolean "is_la_inferred"
t.integer "totchild"
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"

21
spec/models/case_log_spec.rb

@ -1059,5 +1059,26 @@ RSpec.describe Form, type: :model do
expect(record_from_db["la"]).to eq(nil)
end
end
context "household members derived vars" do
let!(:household_case_log) do
CaseLog.create({
managing_organisation: organisation,
owning_organisation: organisation,
other_hhmemb: 4,
relat2: "Child - includes young adult and grown-up",
relat3: "Child - includes young adult and grown-up",
relat4: "Other",
relat5: "Child - includes young adult and grown-up",
})
end
it "correctly derives and saves totchild" do
household_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select totchild from case_logs where id=#{household_case_log.id}").to_a[0]
expect(record_from_db["totchild"]).to eq(3)
end
end
end
end

Loading…
Cancel
Save