chore: init commit
in worker.../repo/GITFOLDER.zip is the .git folder.
This commit is contained in:
42
worker-toolkit-stocks-in-the-future/repo/app/models/year.rb
Normal file
42
worker-toolkit-stocks-in-the-future/repo/app/models/year.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Year < ApplicationRecord
|
||||
has_many :school_years, dependent: :destroy
|
||||
has_many :schools, through: :school_years
|
||||
has_many :classrooms, through: :school_years
|
||||
|
||||
validates :name, presence: true, uniqueness: true
|
||||
|
||||
scope :ordered_by_start_year, -> { order(Arel.sql("CAST(SUBSTRING(name FROM 1 FOR 4) AS INTEGER) DESC")) }
|
||||
|
||||
def self.current_school_year(date = Date.current)
|
||||
name = if date.month <= 6
|
||||
"#{date.year - 1} - #{date.year}"
|
||||
else
|
||||
"#{date.year} - #{date.year + 1}"
|
||||
end
|
||||
where(name: name)
|
||||
end
|
||||
|
||||
def previous_year
|
||||
@previous_year || Year.find_by(name: "#{start_year_value - 1} - #{start_year_value}")
|
||||
end
|
||||
|
||||
def next_year
|
||||
@next_year || Year.find_by(name: "#{end_year_value} - #{end_year_value + 1}")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def start_year_value
|
||||
@start_year_value ||= parsed_year_values.first
|
||||
end
|
||||
|
||||
def end_year_value
|
||||
@end_year_value ||= parsed_year_values.last
|
||||
end
|
||||
|
||||
def parsed_year_values
|
||||
@parsed_year_values ||= name.split(" - ").map(&:to_i)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user