Memoize schools in SurveyItemValues and academic_years in AcademicYear

for performace improvement
This commit is contained in:
rebuilt 2023-03-28 03:38:52 -07:00
parent c15cb7b483
commit b250ebe415
3 changed files with 11 additions and 3 deletions

View file

@ -23,8 +23,14 @@ class AcademicYear < ActiveRecord::Base
end
end
# This may cause problems if academic years get loaded from csv instead of the current method that requires a code change to the seeder script. This is because a change in code will trigger a complete reload of the application whereas loading from csv does not. This means if we change academic year to load from csv, the set of academic years will be stale when new years are added.
def self.academic_years
AcademicYear.all.map { |academic_year| [academic_year.range, academic_year] }.to_h
@@academic_years ||= AcademicYear.all.map { |academic_year| [academic_year.range, academic_year] }.to_h
end
# Needed to reset the academic years when testing with specs that create the same academic year in a before :each block
def self.reset_academic_years
@@academic_years = nil
end
private_class_method :academic_years

View file

@ -57,7 +57,7 @@ class SurveyItemValues
end
def schools
School.all.map { |school| [school.dese_id, school] }.to_h
@schools ||= School.all.map { |school| [school.dese_id, school] }.to_h
end
def grade
@ -65,7 +65,7 @@ class SurveyItemValues
raw_grade = (row['grade'] || row['Grade'] || row['What grade are you in?'])
return nil if raw_grade.blank?
raw_grade = raw_grade.to_i
raw_grade.to_i
end
end

View file

@ -86,6 +86,8 @@ describe SurveyResponsesDataLoader do
end
before :each do
AcademicYear.reset_academic_years
setup
end