Class: Interactors::Api::CompoundEvents

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

Constant Summary collapse

ALLOWED_JOINS =
['courses', 'schedule_exceptions', 'teachers'].to_set.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interpipe::Interactor

included, #initialize, #setup

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events


19
20
21
# File 'app/interactors/api/compound_events.rb', line 19

def events
  @events
end

#paramsObject (readonly)

Returns the value of attribute params


19
20
21
# File 'app/interactors/api/compound_events.rb', line 19

def params
  @params
end

Instance Method Details

#compoundsObject


27
28
29
# File 'app/interactors/api/compound_events.rb', line 27

def compounds
  @joins.map { |name| [name.to_sym, self.send(name)] }.to_h
end

#coursesObject


38
39
40
41
42
43
# File 'app/interactors/api/compound_events.rb', line 38

def courses  # SELECT id, name FROM courses
  # WHERE id IN (SELECT course_id FROM events WHERE ...)

  course_ids = events.select(:course_id)
  Course.where(id: course_ids).select(:id, :name)
end

#joins(param) ⇒ Object

FIXME: invalid params will be silently ignored


32
33
34
35
36
# File 'app/interactors/api/compound_events.rb', line 32

def joins(param)
  return Set.new if param.blank?
  compounds = param.split(',')
  ALLOWED_JOINS.intersection(compounds)
end

#perform(events:, params: {}) ⇒ Object


21
22
23
24
25
# File 'app/interactors/api/compound_events.rb', line 21

def perform(events:, params: {})
  @events = events
  @params = params
  @joins = joins(params[:include])
end

#schedule_exceptionsObject


45
46
47
48
49
50
# File 'app/interactors/api/compound_events.rb', line 45

def schedule_exceptions  # SELECT * FROM schedule_exceptions
  # WHERE id IN (SELECT unnest(applied_schedule_exception_ids) FROM events WHERE ... )

  exception_ids = events.select(:applied_schedule_exception_ids.pg_array.unnest)
  ScheduleException.where(id: exception_ids).select(:id, :exception_type, :name, :note)
end

#teachersObject


52
53
54
55
56
57
# File 'app/interactors/api/compound_events.rb', line 52

def teachers  # SELECT id, full_name FROM people
  # WHERE id IN (SELECT unnest(teacher_ids) FROM events WHERE ...)

  teacher_ids = events.select(:teacher_ids.pg_array.unnest)
  Person.where(id: teacher_ids).select(:id, :full_name)
end