diff --git a/app/models/ell.rb b/app/models/ell.rb index 6b121fb1..44e5ea59 100644 --- a/app/models/ell.rb +++ b/app/models/ell.rb @@ -4,4 +4,14 @@ class Ell < ApplicationRecord include FriendlyId friendly_id :designation, use: [:slugged] + def self.to_designation(ell) + case ell + in /lep student 1st year|LEP student not 1st year|EL Student First Year|LEP\s*student/i + "ELL" + in /Does not apply/i + "Not ELL" + else + "Unknown" + end + end end diff --git a/app/models/income.rb b/app/models/income.rb index fd6d7845..2402876c 100644 --- a/app/models/income.rb +++ b/app/models/income.rb @@ -6,14 +6,24 @@ class Income < ApplicationRecord friendly_id :designation, use: [:slugged] - def label - case designation - when "Economically Disadvantaged - Y" - "Economically Disadvantaged" - when "Economically Disadvantaged - N" - "Not Economically Disadvantaged" - when "Unknown" + def self.to_designation(income) + case income + in /Free\s*Lunch|Reduced\s*Lunch|Low\s*Income|Reduced\s*price\s*lunch/i + "Economically Disadvantaged - Y" + in /Not\s*Eligible/i + "Economically Disadvantaged - N" + else "Unknown" end end + + LABELS = { + "Economically Disadvantaged - Y" => "Economically Disadvantaged", + "Economically Disadvantaged - N" => "Not Economically Disadvantaged", + "Unknown" => "Unknown" + } + + def label + LABELS[designation] + end end diff --git a/app/models/race.rb b/app/models/race.rb index 7b82d716..d393d2ae 100644 --- a/app/models/race.rb +++ b/app/models/race.rb @@ -33,17 +33,17 @@ class Race < ApplicationRecord end end -def self.normalize_race_list(codes) + def self.normalize_race_list(codes) # if anyone selects not to disclose their race or prefers to self-describe, categorize that as unknown race races = codes.map do |code| code = 99 if [6, 7].include?(code) || code.nil? || code.zero? code end.uniq - races.delete(99) if races.length > 1 #remove unkown race if other races present + races.delete(99) if races.length > 1 # remove unkown race if other races present races << 100 if races.length > 1 # add multiracial designation if multiple races present races << 99 if races.length == 0 # add unknown race if other races missing races end - end + diff --git a/app/models/sped.rb b/app/models/sped.rb index b2d79694..8769ada3 100644 --- a/app/models/sped.rb +++ b/app/models/sped.rb @@ -4,4 +4,14 @@ class Sped < ApplicationRecord include FriendlyId friendly_id :designation, use: [:slugged] + def self.to_designation(sped) + case sped + in /active/i + "Special Education" + in /^NA$|^#NA$/i + "Unknown" + else + "Not Special Education" + end + end end diff --git a/app/services/survey_item_values.rb b/app/services/survey_item_values.rb index 1508519e..a7f64399 100644 --- a/app/services/survey_item_values.rb +++ b/app/services/survey_item_values.rb @@ -33,7 +33,7 @@ class SurveyItemValues # We don't ensure that ids in the form of s-tint-q1 have a matching pair because not all questions have variants def include_all_headers(headers:) alternates = headers.filter(&:present?) - .filter { |header| header.end_with? "-1" } + .filter { |header| header.match?(/^[st]-\w*-\w*-1$/i) } alternates.each do |header| main = header.sub(/-1\z/, "") headers.push(main) unless headers.include?(main) @@ -82,13 +82,9 @@ class SurveyItemValues def dese_id @dese_id ||= begin - dese_id = nil - dese_headers = ["DESE ID", "Dese ID", "DeseId", "DeseID", "School", "school"] - school_headers = headers.select { |header| /School-\s\w/.match(header) } - dese_headers << school_headers - dese_headers.flatten.each do |header| - dese_id ||= row[header] - end + dese_id = value_from(pattern: /Dese\s*ID/i) + dese_id ||= value_from(pattern: /^School$/i) + dese_id ||= value_from(pattern: /School-\s*\w/i) dese_id.to_i end @@ -140,7 +136,7 @@ class SurveyItemValues race_codes ||= value_from(pattern: /RACE/i) || "" race_codes ||= [] - + race_codes = race_codes.split(",") .map do |word| word.split(/\s+and\s+/i) @@ -168,14 +164,7 @@ class SurveyItemValues end def income - @income ||= case raw_income - in /Free\s*Lunch|Reduced\s*Lunch|Low\s*Income/i - "Economically Disadvantaged - Y" - in /Not\s*Eligible/i - "Economically Disadvantaged - N" - else - "Unknown" - end + @income ||= Income.to_designation(raw_income) end def raw_ell @@ -183,14 +172,7 @@ class SurveyItemValues end def ell - @ell ||= case raw_ell - in /lep student 1st year|LEP student not 1st year|EL Student First Year/i - "ELL" - in /Does not apply/i - "Not ELL" - else - "Unknown" - end + @ell ||= Ell.to_designation(raw_ell) end def raw_sped @@ -198,14 +180,7 @@ class SurveyItemValues end def sped - @sped ||= case raw_sped - in /active/i - "Special Education" - in /^NA$|^#NA$/i - "Unknown" - else - "Not Special Education" - end + @sped ||= Sped.to_designation(raw_sped) end def value_from(pattern:) @@ -223,11 +198,14 @@ class SurveyItemValues output end + def sanitized_headers + @sanitized_headers ||= headers.select(&:present?) + .reject { |key, _value| key.start_with? "Q" } + .reject { |key, _value| key.match?(/^[st]-\w*-\w*-1$/i) } + end + def to_a - headers.select(&:present?) - .reject { |key, _value| key.start_with? "Q" } - .reject { |key, _value| key.end_with? "-1" } - .map { |header| row[header] } + sanitized_headers.map { |header| row[header] } end def duration @@ -247,12 +225,11 @@ class SurveyItemValues end def survey_type - survey_item_ids = headers - .filter(&:present?) - .reject { |header| header.end_with?("-1") } - .filter { |header| header.start_with?("t-", "s-") } + @survey_type ||= SurveyItem.survey_type(survey_item_ids:) + end - SurveyItem.survey_type(survey_item_ids:) + def survey_item_ids + @survey_item_ids ||= sanitized_headers.filter { |header| header.start_with?("t-", "s-") } end def valid_duration? @@ -267,17 +244,14 @@ class SurveyItemValues end def progress - headers.filter(&:present?) - .reject { |header| header.end_with?("-1") } - .filter { |header| header.start_with?("t-", "s-") } - .reject { |header| row[header].nil? }.count + survey_item_ids.reject { |header| row[header].nil? }.count end def valid_progress? return false if progress.nil? return progress >= 12 if survey_type == :teacher - return progress >= 17 if survey_type == :standard + return progress >= 11 if survey_type == :standard return progress >= 5 if survey_type == :short_form return progress >= 5 if survey_type == :early_education