semester_process.rb 1.84 KB
Newer Older
1
2
class SemesterProcess < ApplicationRecord
  belongs_to :creator, -> { with_deleted }, class_name: 'User', inverse_of: 'semester_processes'
3
4
  belongs_to :mail_posted_by, -> { with_deleted }, class_name: 'User',
    inverse_of: 'semester_process_mails_posted', optional: true
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
5
6
  belongs_to :reminder_mail_posted_by, -> { with_deleted }, class_name: 'User',
    inverse_of: 'semester_process_reminder_mail_posted', optional: true
7
8

  has_many :semester_process_volunteers, dependent: :destroy
9
10
  accepts_nested_attributes_for :semester_process_volunteers, allow_destroy: true

11
  has_many :volunteers, through: :semester_process_volunteers
12
  has_many :semester_feedbacks, through: :semester_process_volunteers
13
14
15
16
  has_many :hours, through: :semester_process_volunteers

  has_many :semester_process_volunteer_missions, through: :semester_process_volunteers

17
  has_many :semester_process_mails, through: :semester_process_volunteers
18
19
20
21
22
23
24
25
26
27
28

  def mails
    semester_process_mails.where(kind: 'mail')
  end

  def reminders
    semester_process_mails.where(kind: 'reminder')
  end

  # will only return an array, not a AD-result
  delegate :missions, to: :semester_process_volunteers
29
30
31
32
33
34
35
36
37
38

  # creates semester date range from string '[year],[semester_number]' e.g. '2018,2'
  def semester=(semester)
    if semester.is_a?(String)
      super(Semester.new(*semester.split(',').map(&:to_i)).current)
    else
      super(semester)
    end
  end

39
40
41
42
43
44
45
46
  def build_semester_volunteers(volunteers, selected = nil)
    volunteers = volunteers.where(id: selected) if selected
    semester_process_volunteers << volunteers.to_a.map do |volunteer|
      spv = SemesterProcessVolunteer.new(volunteer: volunteer, selected: true)
      spv.build_missions(semester)
      spv
    end
  end
47
48
49
50

  def build_volunteers_hours_feedbacks_and_mails
    semester_process_volunteers.map(&:build_hours_feedbacks_and_mails)
  end
51
end