Class: RenumberEvents

Inherits:
Object
  • Object
show all
Includes:
Interpipe::Interactor
Defined in:
app/interactors/renumber_events.rb

Instance Method Summary collapse

Methods included from Interpipe::Interactor

included, #initialize, #setup

Instance Method Details

#cleanup_numbering(faculty_semester) ⇒ Object

Removes relative sequence numbers from deleted events as they do not give much sense anymore.

Parameters:

  • faculty_semester (FacultySemester)

    for which the cleanup should be performed


50
51
52
53
54
55
56
57
58
59
60
# File 'app/interactors/renumber_events.rb', line 50

def cleanup_numbering(faculty_semester)
  DB['update events
       set relative_sequence_number = null
     where deleted = true
       and relative_sequence_number is not null
       and faculty = :faculty
       and semester = :semester',
    faculty: faculty_semester.faculty,
    semester: faculty_semester.code,
  ].update
end

#perform(faculty_semester:) ⇒ Object

Recalculates relative_sequence_number for all Events belonging to a faculty semester.

Numbering is calculated so that normal events are grouped by course, parallel and event_type. Teacher timetable slots are grouped by event type and source because grouping for other event types would not make much sense for them. Groups are then ordered by start time and numbered sequentially starting from 1.

Parameters:

  • faculty_semester (FacultySemester)

    for which then renumbering should be performed


14
15
16
17
18
19
20
21
# File 'app/interactors/renumber_events.rb', line 14

def perform(faculty_semester:)
  renumber_events(faculty_semester,
    %w[laboratory assessment course_event lecture exam tutorial],
    %i[event_type course_id parallel_id]
  )
  renumber_events(faculty_semester, %w[teacher_timetable_slot], %i[event_type source_type source_id])
  cleanup_numbering(faculty_semester)
end

#renumber_events(faculty_semester, event_types, partition_columns) ⇒ Object

Performs an actual renumbering query for specified faculty semester and event types.

Parameters:

  • faculty_semester (FacultySemester)

    for which then renumbering should be performed

  • event_types (Array<String>)

    for which the renumbering should be done

  • partition_columns (Array<Symbol>)

    columns from events table that are used for making numbering groups


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/interactors/renumber_events.rb', line 28

def renumber_events(faculty_semester, event_types, partition_columns)
  DB['with positions as (
      select
        id,
        row_number() over (partition by :partition_columns order by starts_at) as position
      from events
      where deleted = false and faculty = :faculty and semester = :semester and event_type IN :event_types
    )
    update events
      set relative_sequence_number = p.position
    from positions p
    where p.id = events.id;',
    faculty: faculty_semester.faculty,
    semester: faculty_semester.code,
    partition_columns: partition_columns,
    event_types: event_types
  ].update
end