sqm-dashboards/app/models/academic_year.rb
Nelson Jovel 33da0859b9 Split academic year into seasons if the academic year's range is
initialized with a season, i.e. "2024-25 Fall".  Update scapers for
admin data, enrollment and staffing to use the new range standard
correctly.   Update the loaders for admin data, enrollment and staffing
so that it populates all seasons in a given year.  So admin data for
2024-25 gets loaded into "2024-25 Fall" and "2024-25 Spring".  Add tests
for the new range format.  Set the default cutoff for the start of Spring season will be the last Sunday in February
2024-04-25 09:21:04 -07:00

60 lines
1.7 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
require "date"
class AcademicYear < ActiveRecord::Base
scope :by_range, -> { all.map { |academic_year| [academic_year.range, academic_year] }.to_h }
scope :of_year, ->(range) { all.select { |ay| ay.range.start_with?(range) } }
def self.range_from_date(date, ranges)
year = parse_year_range(date:)
range = ranges.find { |item| item.downcase == "#{year.start}-#{year.end.to_s[2, 3]} #{year.season.downcase}" }
range ||= ranges.find { |item| item.downcase == "#{year.start}-#{year.end.to_s[2, 3]}" }
range
end
def formatted_range
years = range.split("-").map(&:split).flatten
"#{years.first} 20#{years.second} #{years[2]}".chomp
end
def range_without_season
range.scan(/[\d-]/).join
end
private
def self.parse_year_range(date:)
year = date.year
ayr = if date.month > 6
AcademicYearRange.new(year, year + 1)
else
AcademicYearRange.new(year - 1, year)
end
ayr.season = if in_fall?(date, ayr)
"Fall"
else
"Spring"
end
ayr
end
def self.in_fall?(date, ayr)
date.between?(Date.parse("#{ayr.start}-7-1"), last_sunday(2, ayr.end) - 1)
end
# returns a Date object being the last sunday of the given month/year
# month: integer between 1 and 12
def self.last_sunday(month, year)
# get the last day of the month
date = Date.new year, month, -1
# subtract number of days we are ahead of sunday
date -= date.wday
end
private_class_method :in_fall?
private_class_method :parse_year_range
end
AcademicYearRange = Struct.new(:start, :end, :season)