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

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

10
11
12
13
14
15
16
  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
17
  has_many :semester_process_volunteer_missions, dependent: :destroy
18
19
20
  has_many :assignments, through: :semester_process_volunteer_missions
  has_many :group_assignments, through: :semester_process_volunteer_missions

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

  has_many :semester_process_mails, dependent: :destroy
25
26
27
28
  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'
29

30
31
32
33
34
35
36
37
38
39
40
41
  scope :index_joins, lambda {
    joins(:semester_process).joins(volunteer: [:contact]).joins(:semester_process_volunteer_missions)
  }

  scope :index, lambda { |semester = nil|
    if semester
      index_joins.where('semester_processes.semester && daterange(?,?)', semester.begin, semester.end)
    else
      index_joins
    end
  }

42
43
  # will only return an array, not a AD-result
  def missions
44
    semester_process_volunteer_missions.map(&:mission)
45
  end
46
47
48
49
50
51
52
53
54
55

  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

56
  def build_hours_and_mails
57
58
59
60
61
    missions.each do |mission|
      hours << mission.hours.date_between_inclusion(:meeting_date, semester.begin, semester.end)
    end
    semester_process_mails << SemesterProcessMail.new(kind: :mail, sent_by: creator)
  end
62
end