Browse Source

Specs passing

pull/74/head
baarkerlounger 3 years ago
parent
commit
656c7bda26
  1. 3
      app/models/case_log.rb
  2. 3
      app/validations/financial_validations.rb
  3. 15
      app/validations/household_validations.rb
  4. 5
      app/validations/property_validations.rb
  5. 3
      app/validations/tenancy_validations.rb
  6. 4
      docs/api/DLUHC-CORE-Data.v1.json
  7. 4
      spec/requests/case_log_controller_spec.rb

3
app/models/case_log.rb

@ -1,5 +1,6 @@
class CaseLogValidator < ActiveModel::Validator class CaseLogValidator < ActiveModel::Validator
# Validations methods need to be called 'validate_' to run on model save # Validations methods need to be called 'validate_<page_name>' to run on model save
# or 'validate_' to run on submit as well
include HouseholdValidations include HouseholdValidations
include PropertyValidations include PropertyValidations
include FinancialValidations include FinancialValidations

3
app/validations/financial_validations.rb

@ -1,5 +1,6 @@
module FinancialValidations module FinancialValidations
# Validations methods need to be called 'validate_' to run on model save # Validations methods need to be called 'validate_<page_name>' to run on model save
# or 'validate_' to run on submit as well
def validate_outstanding_rent_amount(record) def validate_outstanding_rent_amount(record)
if record.outstanding_rent_or_charges == "Yes" && record.outstanding_amount.blank? if record.outstanding_rent_or_charges == "Yes" && record.outstanding_amount.blank?
record.errors.add :outstanding_amount, "You must answer the oustanding amout question if you have outstanding rent or charges." record.errors.add :outstanding_amount, "You must answer the oustanding amout question if you have outstanding rent or charges."

15
app/validations/household_validations.rb

@ -1,5 +1,6 @@
module HouseholdValidations module HouseholdValidations
# Validations methods need to be called 'validate_' to run on model save # Validations methods need to be called 'validate_<page_name>' to run on model save
# or 'validate_' to run on submit as well
def validate_reasonable_preference(record) def validate_reasonable_preference(record)
if record.homelessness == "No" && record.reasonable_preference == "Yes" if record.homelessness == "No" && record.reasonable_preference == "Yes"
record.errors.add :reasonable_preference, "Can not be Yes if Not Homeless immediately prior to this letting has been selected" record.errors.add :reasonable_preference, "Can not be Yes if Not Homeless immediately prior to this letting has been selected"
@ -50,16 +51,20 @@ module HouseholdValidations
end end
end end
def validate_other_household_members(record) def validate_household_number_of_other_members(record)
(1..8).each do |n| (2..8).each do |n|
validate_person_age(record, n) validate_person_age(record, n)
validate_person_age_matches_economic_status(record, n) validate_person_age_matches_economic_status(record, n)
validate_person_age_matches_relationship(record, n) if n > 1 validate_person_age_matches_relationship(record, n)
validate_person_age_and_gender_match_economic_status(record, n) validate_person_age_and_gender_match_economic_status(record, n)
end end
validate_partner_count(record) validate_partner_count(record)
end end
def validate_person_1_age(record)
validate_person_age(record, 1)
end
private private
def women_of_child_bearing_age_in_household(record) def women_of_child_bearing_age_in_household(record)
@ -122,7 +127,7 @@ private
def validate_partner_count(record) def validate_partner_count(record)
# TODO probably need to keep track of which specific field is wrong so we can highlight it in the UI # TODO probably need to keep track of which specific field is wrong so we can highlight it in the UI
partner_count = (2..8).map { |n| record.public_send("person_#{n}_relationship") }.uniq.count partner_count = (2..8).select { |n| record.public_send("person_#{n}_relationship") == "Partner" }.count
if partner_count > 1 if partner_count > 1
record.errors.add :base, "Number of partners cannot be greater than 1" record.errors.add :base, "Number of partners cannot be greater than 1"
end end

5
app/validations/property_validations.rb

@ -1,8 +1,9 @@
module PropertyValidations module PropertyValidations
# Validations methods need to be called 'validate_' to run on model save # Validations methods need to be called 'validate_<page_name>' to run on model save
# or 'validate_' to run on submit as well
def validate_property_number_of_times_relet(record) def validate_property_number_of_times_relet(record)
if record.property_number_of_times_relet && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.property_number_of_times_relet.to_s) if record.property_number_of_times_relet && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.property_number_of_times_relet.to_s)
record.errors.add :property_number_of_times_relet, "Must be between 0 and 20" record.errors.add :property_number_of_times_relet, "Property number of times relet must be between 0 and 20"
end end
end end
end end

3
app/validations/tenancy_validations.rb

@ -1,5 +1,6 @@
module TenancyValidations module TenancyValidations
# Validations methods need to be called 'validate_' to run on model save # Validations methods need to be called 'validate_<page_name>' to run on model save
# or 'validate_' to run on submit as well
def validate_fixed_term_tenancy(record) def validate_fixed_term_tenancy(record)
is_present = record.fixed_term_tenancy.present? is_present = record.fixed_term_tenancy.present?
is_in_range = record.fixed_term_tenancy.to_i.between?(2, 99) is_in_range = record.fixed_term_tenancy.to_i.between?(2, 99)

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

@ -110,7 +110,7 @@
"value": { "value": {
"errors": { "errors": {
"person_1_age": [ "person_1_age": [
"Tenant age must be between 0 and 120" "Tenant age must be an integer between 0 and 120"
] ]
} }
} }
@ -220,7 +220,7 @@
"If reasonable preference is Yes, a reason must be given" "If reasonable preference is Yes, a reason must be given"
], ],
"person_1_age": [ "person_1_age": [
"Tenant age must be between 0 and 120" "Tenant age must be an integer between 0 and 120"
] ]
} }
} }

4
spec/requests/case_log_controller_spec.rb

@ -66,7 +66,7 @@ RSpec.describe CaseLogsController, type: :request do
it "validates case log parameters" do it "validates case log parameters" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
expect(json_response["errors"]).to match_array([["property_number_of_times_relet", ["Must be between 0 and 20"]], ["person_1_age", ["Tenant age must be between 0 and 120"]]]) expect(json_response["errors"]).to match_array([["property_number_of_times_relet", ["Property number of times relet must be between 0 and 20"]], ["person_1_age", ["Tenant age must be an integer between 0 and 120"]]])
end end
end end
@ -165,7 +165,7 @@ RSpec.describe CaseLogsController, type: :request do
it "returns an error message" do it "returns an error message" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response["errors"]).to eq({ "person_1_age" => ["Tenant age must be between 0 and 120"] }) expect(json_response["errors"]).to eq({ "person_1_age" => ["Tenant age must be an integer between 0 and 120"] })
end end
end end

Loading…
Cancel
Save