Class: FacultySemester

Inherits:
Sequel::Model
  • Object
show all
Defined in:
app/models/faculty_semester.rb

Class Method Summary collapse

Class Method Details

.active_for(source_type) ⇒ Object


9
10
11
12
13
14
15
# File 'app/models/faculty_semester.rb', line 9

def self.active_for(source_type)
  if source_type == :parallels
    where(update_parallels: true)
  else
    where(update_other: true)
  end
end

.find_by_date(date, faculty_id) ⇒ Object


17
18
19
20
21
22
23
# File 'app/models/faculty_semester.rb', line 17

def self.find_by_date(date, faculty_id)
  where(':date >= starts_at AND :date <= ends_at AND faculty = :faculty',
        date: date, faculty: faculty_id)
    .eager(:semester_periods)
    .limit(1)
    .all.first  # eager loading doesn't work with Dataset.first
end

.find_by_date_range_with_periods(start_date, end_date, faculty_id) ⇒ Array<FacultySemester>

Finds faculty semesters with periods that overlap the specified date range. Note that the returned semester objects may not contain the full set of associated periods, but only a subset that overlaps the date range.

Parameters:

  • start_date (Date)

    date of the first week to return (will be rounded to the beginning of the week).

  • end_date (Date)

    date of the last week to retun (will be rounded to the beginning of the week).

  • faculty_id (Fixnum)

    organizational number of the faculty.

Returns:


37
38
39
40
41
42
43
# File 'app/models/faculty_semester.rb', line 37

def self.find_by_date_range_with_periods(start_date, end_date, faculty_id)
  where('faculty = ? AND (starts_at, ends_at) OVERLAPS (?, ?)',
        faculty_id, start_date.start_of_week, end_date)
    .order_by(:starts_at)
    .eager(semester_periods: ->(ds) { ds.where('starts_at < ?', end_date.end_of_week) })
    .all  # <- this is necessary for eager to work correctly!
end