implement suggestions from static code analysis tools

This commit is contained in:
rebuilt 2022-07-12 19:15:54 -07:00
parent bb5f668497
commit e89358dacc
21 changed files with 252 additions and 121 deletions

View file

@ -51,19 +51,17 @@ class Measure < ActiveRecord::Base
@includes_admin_data_items ||= admin_data_items.any?
end
def sources
@sources ||= begin
sources = []
sources << :admin_data if includes_admin_data_items?
sources << :student_surveys if includes_student_survey_items?
sources << :teacher_surveys if includes_teacher_survey_items?
sources
end
end
# def sources
# @sources ||= begin
# sources = []
# sources << Source.new(name: :admin_data, collection: admin_data_items) if includes_admin_data_items?
# sources << Source.new(name: :student_surveys, collection: student_survey_items) if includes_student_survey_items?
# sources << Source.new(name: :teacher_surveys, collection: teacher_survey_items) if includes_teacher_survey_items?
# sources
# end
# end
def score(school:, academic_year:)
# average = SurveyItemResponse.where(school:, academic_year:).first
# Score.new(average, false, false, false)
@score ||= Hash.new do |memo, (school, academic_year)|
next Score::NIL_SCORE if incalculable_score(school:, academic_year:)
@ -149,9 +147,9 @@ class Measure < ActiveRecord::Base
def collect_survey_item_average(survey_items:, school:, academic_year:)
@collect_survey_item_average ||= Hash.new do |memo, (survey_items, school, academic_year)|
averages = survey_items.map do |survey_item|
grouped_responses(school:, academic_year:)[survey_item] || 0
grouped_responses(school:, academic_year:)[survey_item]
end.remove_blanks
memo[[survey_items, school, academic_year]] = averages.any? ? averages.average : 0
memo[[survey_items, school, academic_year]] = averages.average || 0
end
@collect_survey_item_average[[survey_items, school, academic_year]]
end
@ -210,8 +208,7 @@ class Measure < ActiveRecord::Base
def no_teacher_responses_exist?(school:, academic_year:)
@no_teacher_responses_exist ||= Hash.new do |memo, (school, academic_year)|
memo[[school, academic_year]] = teacher_survey_items.all? do |survey_item|
survey_item.survey_item_responses.where(school:,
academic_year:).none?
survey_item.survey_item_responses.where(school:, academic_year:).none?
end
end
@no_teacher_responses_exist[[school, academic_year]]
@ -219,9 +216,8 @@ class Measure < ActiveRecord::Base
def incalculable_score(school:, academic_year:)
@incalculable_score ||= Hash.new do |memo, (school, academic_year)|
meets_student_threshold = sufficient_student_data?(school:, academic_year:)
meets_teacher_threshold = sufficient_teacher_data?(school:, academic_year:)
lacks_sufficient_survey_data = !meets_student_threshold && !meets_teacher_threshold
lacks_sufficient_survey_data = !sufficient_student_data?(school:, academic_year:) &&
!sufficient_teacher_data?(school:, academic_year:)
memo[[school, academic_year]] = lacks_sufficient_survey_data && !includes_admin_data_items?
end
@ -247,8 +243,8 @@ class Measure < ActiveRecord::Base
def student_average(school:, academic_year:)
@student_average ||= Hash.new do |memo, (school, academic_year)|
memo[[school, academic_year]] = collect_survey_item_average(survey_items: student_survey_items_by_survey_type(school:, academic_year:), school:,
academic_year:)
survey_items = student_survey_items_by_survey_type(school:, academic_year:)
memo[[school, academic_year]] = collect_survey_item_average(survey_items:, school:, academic_year:)
end
@student_average[[school, academic_year]]
end

View file

@ -12,16 +12,13 @@ module ResponseRateCalculator
end
def rate
return 100 if Respondent.where(school: @school, academic_year: @academic_year).count.zero?
return 100 if population_data_unavailable?
return 0 unless survey_item_count.positive?
average_responses_per_survey_item = response_count / survey_item_count.to_f
return 0 unless total_possible_responses.positive?
response_rate = (average_responses_per_survey_item / total_possible_responses.to_f * 100).round
cap_at_one_hundred(response_rate)
cap_at_one_hundred(raw_response_rate)
end
def meets_student_threshold?
@ -37,4 +34,20 @@ module ResponseRateCalculator
def cap_at_one_hundred(response_rate)
response_rate > 100 ? 100 : response_rate
end
def survey
Survey.find_by(school:, academic_year:)
end
def raw_response_rate
(average_responses_per_survey_item / total_possible_responses.to_f * 100).round
end
def average_responses_per_survey_item
response_count / survey_item_count.to_f
end
def population_data_unavailable?
Respondent.where(school: @school, academic_year: @academic_year).count.zero?
end
end

View file

@ -7,8 +7,6 @@ class StudentResponseRateCalculator
def survey_item_count
@survey_item_count ||= begin
survey = Survey.find_by(school:, academic_year:)
survey_items = SurveyItem.includes(%i[scale
measure]).student_survey_items.where("scale.measure": @subcategory.measures)
survey_items = survey_items.where(on_short_form: true) if survey.form == 'short'
@ -22,7 +20,6 @@ class StudentResponseRateCalculator
def response_count
@response_count ||= @subcategory.measures.map do |measure|
measure.student_survey_items.map do |survey_item|
survey = Survey.find_by(school:, academic_year:)
next 0 if survey.form == 'short' && survey_item.on_short_form == false
survey_item.survey_item_responses.where(school:,

View file

@ -18,11 +18,7 @@ class Subcategory < ActiveRecord::Base
memo[[school, academic_year]] = ResponseRate.find_by(subcategory: self, school:, academic_year:)
end
if @response_rate[[school, academic_year]].nil?
@response_rate[[school, academic_year]] = create_response_rate(school:, academic_year:)
end
@response_rate[[school, academic_year]]
@response_rate[[school, academic_year]] || create_response_rate(school:, academic_year:)
end
private

7
app/models/today.rb Normal file
View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class Today
def updated_at
Date.today
end
end