Browse Source

CLDC-464: Other tenancy type validation (#68)

* Add other_tenancy_type conditional field

* Extract private validdate_other_field_method

* Implement other tenancy type validation

* Add missing migration file and update API doc
pull/69/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
055cbf8fc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      app/models/case_log.rb
  2. 8
      config/forms/2021_2022.json
  3. 7
      db/migrate/20211027123535_add_other_tenancy_type_field.rb
  4. 3
      db/schema.rb
  5. 7
      docs/api/DLUHC-CORE-Data.v1.json
  6. 27
      spec/models/case_log_spec.rb

28
app/models/case_log.rb

@ -30,13 +30,7 @@ class CaseLogValidator < ActiveModel::Validator
end
def validate_other_reason_for_leaving_last_settled_home(record)
if record.reason_for_leaving_last_settled_home == "Other" && record.other_reason_for_leaving_last_settled_home.blank?
record.errors.add :other_reason_for_leaving_last_settled_home, "If reason for leaving settled home is other then the other reason must be provided"
end
if record.reason_for_leaving_last_settled_home != "Other" && record.other_reason_for_leaving_last_settled_home.present?
record.errors.add :other_reason_for_leaving_last_settled_home, "The other reason must not be provided if the reason for leaving settled home was not other"
end
validate_other_field(record, "reason_for_leaving_last_settled_home", "other_reason_for_leaving_last_settled_home")
end
def validate_reason_for_leaving_last_settled_home(record)
@ -107,6 +101,10 @@ class CaseLogValidator < ActiveModel::Validator
conditions.each { |condition| condition[:condition] ? (record.errors.add :fixed_term_tenancy, condition[:error]) : nil }
end
def validate_other_tenancy_type(record)
validate_other_field(record, "tenancy_type", "other_tenancy_type")
end
def validate(record)
# If we've come from the form UI we only want to validate the specific fields
# that have just been submitted. If we're submitting a log via API or Bulk Upload
@ -126,6 +124,18 @@ class CaseLogValidator < ActiveModel::Validator
private
def validate_other_field(record, main_field, other_field)
main_field_label = main_field.humanize(capitalize: false)
other_field_label = other_field.humanize(capitalize: false)
if record[main_field] == "Other" && record[other_field].blank?
record.errors.add other_field.to_sym, "If #{main_field_label} is other then #{other_field_label} must be provided"
end
if record[main_field] != "Other" && record[other_field].present?
record.errors.add other_field.to_sym, "#{other_field_label} must not be provided if #{main_field_label} was not other"
end
end
def women_of_child_bearing_age_in_household(record)
(1..8).any? do |n|
next if record["person_#{n}_gender"].nil? || record["person_#{n}_age"].nil?
@ -209,6 +219,10 @@ private
dynamically_not_required << "fixed_term_tenancy"
end
if tenancy_type != "Other"
dynamically_not_required << "other_tenancy_type"
end
required.delete_if { |key, _value| dynamically_not_required.include?(key) }
end
end

8
config/forms/2021_2022.json

@ -903,7 +903,15 @@
"3": "Lifetime – Assured",
"4": "License agreement",
"5": "Other"
},
"conditional_for": {
"other_tenancy_type": ["Other"]
}
},
"other_tenancy_type": {
"header": "Please state the tenancy type",
"hint_text": "",
"type": "text"
}
}
},

7
db/migrate/20211027123535_add_other_tenancy_type_field.rb

@ -0,0 +1,7 @@
class AddOtherTenancyTypeField < ActiveRecord::Migration[6.1]
def change
change_table :case_logs, bulk: true do |t|
t.column :other_tenancy_type, :string
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_10_27_091521) do
ActiveRecord::Schema.define(version: 2021_10_27_123535) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -132,6 +132,7 @@ ActiveRecord::Schema.define(version: 2021_10_27_091521) do
t.boolean "reasonable_preference_reason_avoid_hardship"
t.boolean "reasonable_preference_reason_do_not_know"
t.datetime "discarded_at"
t.string "other_tenancy_type"
t.index ["discarded_at"], name: "index_case_logs_on_discarded_at"
end

7
docs/api/DLUHC-CORE-Data.v1.json

@ -1089,6 +1089,10 @@
},
"reasonable_preference_reason_do_not_know": {
"type": "boolean"
},
"other_tenancy-type": {
"type": "string",
"example": "private tenancy"
}
},
"required": [
@ -1203,7 +1207,8 @@
"reasonable_preference_reason_unsatisfactory_housing",
"reasonable_preference_reason_medical_grounds",
"reasonable_preference_reason_avoid_hardship",
"reasonable_preference_reason_do_not_know"
"reasonable_preference_reason_do_not_know",
"other_tenancy-type"
]
}
},

27
spec/models/case_log_spec.rb

@ -201,7 +201,32 @@ RSpec.describe Form, type: :model do
expect {
CaseLog.create!(armed_forces: "Yes - a regular",
armed_forces_active: "Yes",
armed_forces_injured: "Yes")
armed_forces_injured: "Yes")}
end
end
context "other tenancy type validation" do
it "must be provided if tenancy type was given as other" do
expect {
CaseLog.create!(tenancy_type: "Other",
other_tenancy_type: nil)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Other",
other_tenancy_type: "type")
}.not_to raise_error
end
it "must not be provided if tenancy type is not other" do
expect {
CaseLog.create!(tenancy_type: "Fixed",
other_tenancy_type: "the other reason provided")
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed",
other_tenancy_type: nil)
}.not_to raise_error
end
end

Loading…
Cancel
Save