semester_process_volunteer.rb 2.86 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

Jiri Strojil's avatar
Tmp    
Jiri Strojil committed
30
  accepts_nested_attributes_for :group_assignments, :assignments , :semester_process_volunteer_missions , :hours, :volunteer, :semester_feedbacks
Jiri Strojil's avatar
Jiri Strojil committed
31
32
33

  validates_associated :hours, :semester_feedbacks, :volunteer

34
35
36
37
38
39
40
41
42
43
44
45
  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
  }

46
  def semester_feedback_with_mission(mission)
Jiri Strojil's avatar
Jiri Strojil committed
47
    self.semester_feedbacks.order(:created_at).select{|sf| sf.mission == mission}.last
48
  end
Jiri Strojil's avatar
Jiri Strojil committed
49

50
51
  # will only return an array, not a AD-result
  def missions
52
    semester_process_volunteer_missions.map(&:mission)
53
  end
54
55
56
57
58
59
60
61
62
63

  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

64
  def build_hours_and_mails
65
66
67
    missions.each do |mission|
      hours << mission.hours.date_between_inclusion(:meeting_date, semester.begin, semester.end)
    end
68
69
70
    semester_process_mails << SemesterProcessMail.new(kind: :mail, sent_by: creator,
                                subject: semester_process.mail_subject_template,
                                body:    semester_process.mail_body_template)
71
  end
72
end