WIP: create backend for socio-economic-status

This commit is contained in:
rebuilt 2025-06-25 11:08:12 -07:00
parent 61c5b0b087
commit a258b32b39
23 changed files with 346 additions and 23 deletions

32
app/models/benefit.rb Normal file
View file

@ -0,0 +1,32 @@
class Benefit < ApplicationRecord
scope :by_designation, -> { all.map { |benefits| [benefits.designation, benefits] }.to_h }
def self.to_designation(benefits)
return "Unknown" if benefits.blank? or benefits.nil?
case benefits
in /^1$/i
"Yes"
in /^2$/i
"No"
in /^3$/i
"Unknown"
in /^99$|^100$/i
"Unknown"
else
puts "************************************"
puts "******** ERROR **********"
puts ""
puts "Error parsing Income column. '#{benefits}' is not a known value. Halting execution"
puts ""
puts "************************************"
exit
end
end
def points
return 1 if designation == "Yes"
0
end
end

45
app/models/education.rb Normal file
View file

@ -0,0 +1,45 @@
class Education < ApplicationRecord
scope :by_designation, -> { all.map { |education| [education.designation, education] }.to_h }
def self.to_designation(education)
return "Unknown" if education.blank? or education.nil?
case education
in /^1$/i
"No formal schooling completed"
in /^2$/i
"Some formal schooling"
in /^3$/i
"High school diploma or GED"
in /^4$/i
"Associates Degree"
in /^5$/i
"Bachelors Degree"
in /^6$/i
"Masters Degree"
in /^7$/i
"Professional Degree"
in /^8$/i
"Doctorate Degree"
in /^99$|^100$/i
"Unknown"
else
puts "************************************"
puts "******** ERROR **********"
puts ""
puts "Error parsing Income column. '#{education}' is not a known value. Halting execution"
puts ""
puts "************************************"
exit
end
end
def points
higher_level_education = ["Associates Degree", "Bachelors Degree", "Masters Degree", "Professional Degree", "Doctorate Degree"]
if higher_level_education.include?(designation)
1
else
0
end
end
end

39
app/models/employment.rb Normal file
View file

@ -0,0 +1,39 @@
class Employment < ApplicationRecord
scope :by_designation, -> { all.map { |employment| [employment.designation, employment] }.to_h }
def self.to_designation(employment)
return "Unknown" if employment.blank? or employment.nil?
case employment
in /^1$/i
"Two adults with full-time employment"
in /^2$/i
"One adult with full-time employment"
in /^3$/i
"Two adults with part-time employment"
in /^4$/i
"One adult with part-time employment"
in /^5$/i
"No full-time or part-time employment"
in /^99$|^100$/i
"Unknown"
else
puts "************************************"
puts "******** ERROR **********"
puts ""
puts "Error parsing Income column. '#{employment}' is not a known value. Halting execution"
puts ""
puts "************************************"
exit
end
end
def points
higher_level_employment = ["Two adults with full-time employment", "One adult with full-time employment"]
if higher_level_employment.include?(designation)
1
else
0
end
end
end

View file

@ -1,7 +1,11 @@
class Parent < ApplicationRecord
belongs_to :housing, optional: true
belongs_to :education, optional: true
belongs_to :benefit, optional: true
has_many :parent_languages
has_and_belongs_to_many :languages, join_table: :parent_languages
has_and_belongs_to_many :races, join_table: :parent_races
has_and_belongs_to_many :genders, join_table: :parent_genders
has_and_belongs_to_many :employments, join_table: :parent_employments
end

View file

@ -67,7 +67,6 @@ module Report
row << academic_year.range
survey_items.each do |survey_item_id|
survey_item = survey_items_by_id[survey_item_id]
byebug if survey_item.nil?
if sufficient_survey_items[grade].include? survey_item_id
row.append("#{survey_item.survey_item_responses.where(school:, academic_year:,
grade:).average(:likert_score).to_f.round(2)}")
@ -87,7 +86,6 @@ module Report
row.append(academic_year.range)
survey_items.each do |survey_item_id|
survey_item = survey_items_by_id[survey_item_id]
byebug if survey_item.nil?
# filter out response rate at subcategory level <24.5% for school average
subcategory = survey_item.scale.measure.subcategory
if ::StudentResponseRateCalculator.new(subcategory:, school:, academic_year:).meets_student_threshold?

View file

@ -10,6 +10,9 @@ class DemographicLoader
create_from_column(column: "Special Ed Status", row:, model: Sped)
create_from_column(column: "Housing", row:, model: Housing)
create_from_column(column: "Language", row:, model: Language)
create_from_column(column: "Employment", row:, model: Employment)
create_from_column(column: "Education", row:, model: Education)
create_from_column(column: "Benefits", row:, model: Benefit)
end
end

View file

@ -102,6 +102,24 @@ class SurveyItemValues
@response_id ||= value_from(pattern: /Response\s*ID/i)
end
def employments
e = value_from(pattern: /^Employment$/i)
return [] if e.nil? || e.empty?
e.split(",").map do |item|
Employment.to_designation(item.strip)
end
end
def education
Education.to_designation(value_from(pattern: /^Education$/i)&.strip)
end
def benefits
Benefit.to_designation(value_from(pattern: /^Benefits$/i)&.strip)
end
def dese_id
@dese_id ||= begin
dese_id = value_from(pattern: /Dese\s*ID/i)

View file

@ -78,6 +78,18 @@ class SurveyResponsesDataLoader
@speds ||= Sped.by_designation
end
def employments
@employments ||= Employment.by_designation
end
def educations
@educations ||= Education.by_designation
end
def benefits
@benefits ||= Benefit.by_designation
end
def academic_years
@academic_years ||= AcademicYear.all
end
@ -110,6 +122,9 @@ class SurveyResponsesDataLoader
if row.respondent_type == :parent
parent = Parent.find_or_create_by(response_id: row.response_id)
parent.number_of_children = row.number_of_children
parent.education = educations[row.education] if row.education.present?
parent.benefits_id = benefits[row.benefits].id if row.benefits.present?
tmp_languages = row.languages.map { |language| languages[language] }.reject(&:nil?)
parent.languages.delete_all
parent.languages.concat(tmp_languages)
@ -122,6 +137,10 @@ class SurveyResponsesDataLoader
tmp_genders = row.genders_of_children.map { |race| genders[race] }.reject(&:nil?)
parent.genders.concat(tmp_genders)
parent.employments.delete_all
tmp_employments = row.employments.map { |employment| employments[employment] }.reject(&:nil?)
parent.employments.concat(tmp_employments)
parent.housing = housings[row.housing] if row.housing.present?
parent.save
end