diff --git a/app/presenters/analyze/graph/parents_by_socio_economic_status.rb b/app/presenters/analyze/graph/parents_by_socio_economic_status.rb index e1db0b60..3b2e144a 100644 --- a/app/presenters/analyze/graph/parents_by_socio_economic_status.rb +++ b/app/presenters/analyze/graph/parents_by_socio_economic_status.rb @@ -14,8 +14,9 @@ module Analyze def columns [].tap do |array| array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: [0, 1], label: ["Low Advantage"], show_irrelevancy_message: false) - array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: 2, label: ["Mediumn Advantage"], show_irrelevancy_message: false) + array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: 2, label: ["Medium Advantage"], show_irrelevancy_message: false) array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: 3, label: ["High Advantage"], show_irrelevancy_message: false) + array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: -1, label: ["Unknown"], show_irrelevancy_message: false) array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: [0, 1, 2, 3, nil], label: ["All Students"], show_irrelevancy_message: true) end diff --git a/app/services/socio_economic_calculator.rb b/app/services/socio_economic_calculator.rb index b3162d1b..85d29ae3 100644 --- a/app/services/socio_economic_calculator.rb +++ b/app/services/socio_economic_calculator.rb @@ -2,7 +2,12 @@ class SocioEconomicCalculator def self.update_socio_economic_scores parent_list = [].tap do |list| Parent.all.each do |parent| - parent.socio_economic_status = socio_economic_score(parent.education, parent.benefit, parent.employments) + parent.socio_economic_status = if has_all_socio_economic_data?(parent:) + socio_economic_score(parent.education, parent.benefit, parent.employments) + else + -1 + end + list << parent end end @@ -14,7 +19,18 @@ class SocioEconomicCalculator ) end + def self.has_all_socio_economic_data?(parent:) + parent.education.present? && parent.education.designation != "Unknown" && parent.benefit.present? && parent.benefit.designation != "Unknown" && parent.employments.any? && parent.employments.any? { |employment| employment.designation != "Unknown" } + end + def self.socio_economic_score(education, benefits, employment) + return -1 if education.designation == "Unknown" || benefits.designation == "Unknown" || employment.empty? || employment.all? { |employment| employment.designation == "Unknown" } + + # Calculate the total points from employment, education, and benefits + # Assuming each of these has a method `points` that returns a numeric value + # If any of these are nil, we treat them as 0 points + + # Clamp the total points to be between 0 and 1 employment_points = employment.map(&:points).sum.clamp(0, 1) ed_points = education&.points || 0 benefits_points = benefits&.points || 0 diff --git a/app/services/survey_responses_data_loader.rb b/app/services/survey_responses_data_loader.rb index 01338f56..5aa0a4ca 100644 --- a/app/services/survey_responses_data_loader.rb +++ b/app/services/survey_responses_data_loader.rb @@ -20,6 +20,7 @@ class SurveyResponsesDataLoader ) end end + SocioEconomicCalculator.update_socio_economic_scores end def from_file(file:) @@ -144,7 +145,6 @@ class SurveyResponsesDataLoader tmp_employments = row.employments.map { |employment| employments[employment] }.reject(&:nil?) parent.employments.concat(tmp_employments) - parent.socio_economic_status = SocioEconomicCalculator.socio_economic_score(educations[row.education], benefits[row.benefits], tmp_employments) parent.housing = housings[row.housing] if row.housing.present? parent.save diff --git a/spec/services/survey_responses_data_loader_spec.rb b/spec/services/survey_responses_data_loader_spec.rb index 327784c3..3d6da850 100644 --- a/spec/services/survey_responses_data_loader_spec.rb +++ b/spec/services/survey_responses_data_loader_spec.rb @@ -346,9 +346,6 @@ describe SurveyResponsesDataLoader do it "returns 0 when none of the rubrics meet the standard for higher advantage" do score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "No formal schooling completed"), Benefit.new(designation: "Yes"), [Employment.new(designation: "No full-time or part-time employment")]) expect(score).to eq 0 - - score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Unknown"), Benefit.new(designation: "Unknown"), [Employment.new(designation: "Unknown")]) - expect(score).to eq 0 end it "returns 1 when one of the rubrics meets the standard for higher advantage" do @@ -368,6 +365,20 @@ describe SurveyResponsesDataLoader do score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "No"), [Employment.new(designation: "One adult with full-time employment"), Employment.new(designation: "Two adults with full-time employment")]) expect(score).to eq 3 end + + it "returns -1 when any aspect of the data is unknown" do + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "Unknown"), [Employment.new(designation: "Two adults with full-time employment")]) + expect(score).to eq -1 + + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "No"), [Employment.new(designation: "Unknown")]) + expect(score).to eq -1 + + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Unknown"), Benefit.new(designation: "No"), [Employment.new(designation: "No full-time or part-time employment")]) + expect(score).to eq -1 + + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Unknown"), Benefit.new(designation: "No"), []) + expect(score).to eq -1 + end end end