semester_process_volunteer.rb 2.09 KB
Newer Older
1
class SemesterProcessVolunteer < ApplicationRecord
2
3
  attr_accessor :selected

4
5
  belongs_to :volunteer
  belongs_to :semester_process
6
7
8
  delegate :semester, to: :semester_process
  delegate :creator, to: :semester_process

9
10
11
12
13
14
15
  belongs_to :responsible, -> { with_deleted }, class_name: 'User',
    inverse_of: 'semester_processes', optional: true
  belongs_to :reviewed_by, -> { with_deleted }, class_name: 'User',
    inverse_of: 'semester_processes', optional: true
  belongs_to :commited_by, -> { with_deleted }, class_name: 'User',
    inverse_of: 'semester_processes', optional: true

Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
16
  has_many :semester_process_volunteer_missions, dependent: :destroy
17
18
19
  has_many :assignments, through: :semester_process_volunteer_missions
  has_many :group_assignments, through: :semester_process_volunteer_missions

20
  has_many :semester_feedbacks, dependent: :destroy
21
  has_many :hours, dependent: :nullify
22
23

  has_many :semester_process_mails, dependent: :destroy
24
25
26
27
  has_many :mails, -> { where(kind: 'mail') }, class_name: 'SemesterProcessMail',
    foreign_key: 'semester_process_volunteer_id', inverse_of: 'semester_process_volunteer'
  has_many :reminders, -> { where(kind: 'reminder') }, class_name: 'SemesterProcessMail',
    foreign_key: 'semester_process_volunteer_id', inverse_of: 'semester_process_volunteer'
28
29
30

  # will only return an array, not a AD-result
  def missions
31
    semester_process_volunteer_missions.map(&:mission)
32
  end
33
34
35
36
37
38
39
40
41
42

  def build_missions(semester)
    new_missions = volunteer.assignments.active_between(semester.begin, semester.end) +
      volunteer.group_assignments.active_between(semester.begin, semester.end)

    semester_process_volunteer_missions << new_missions.map do |mission|
      SemesterProcessVolunteerMission.new(mission: mission)
    end
  end

43
44
45
46
47
48
49
  def build_hours_feedbacks_and_mails
    missions.each do |mission|
      hours << mission.hours.date_between_inclusion(:meeting_date, semester.begin, semester.end)
      semester_feedbacks << SemesterFeedback.new(mission: mission, volunteer: mission.volunteer)
    end
    semester_process_mails << SemesterProcessMail.new(kind: :mail, sent_by: creator)
  end
50
end