semester_process_volunteer.rb 3.21 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
  delegate :semester_period, to: :semester_process
9
10
  delegate :creator, to: :semester_process

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

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

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

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

  validates_associated :hours, :semester_feedbacks, :volunteer

35
  scope :index_joins, lambda {
Jiri Strojil's avatar
Fixed    
Jiri Strojil committed
36
    joins(:semester_process).joins(:semester_process_volunteer_missions, volunteer: [:contact])
37
38
39
40
  }

  scope :index, lambda { |semester = nil|
    if semester
Jiri Strojil's avatar
Fixed    
Jiri Strojil committed
41
42
43
      joins(:semester_process).where(semester_process: semester)
          .joins(:semester_process_volunteer_missions, volunteer: [:contact])
          .group('semester_process_volunteers.id, contacts.last_name')
44
45
46
47
48
    else
      index_joins
    end
  }

Jiri Strojil's avatar
Jiri Strojil committed
49

50
  def semester_feedback_with_mission(mission)
Jiri Strojil's avatar
Jiri Strojil committed
51
    self.semester_feedbacks.order(:created_at).select{|sf| sf.mission == mission}.last
52
  end
Jiri Strojil's avatar
Jiri Strojil committed
53

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

  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

68
  def build_hours_and_mails
69
70
71
    missions.each do |mission|
      hours << mission.hours.date_between_inclusion(:meeting_date, semester.begin, semester.end)
    end
72
73
74
    semester_process_mails << SemesterProcessMail.new(kind: :mail, sent_by: creator,
                                subject: semester_process.mail_subject_template,
                                body:    semester_process.mail_body_template)
75
  end
76
77
78
79

  def render_feedback(field)
    semester_feedbacks.map(&field).join(', ')
  end
80
81
82
83
84

  def responsible=(responsible_user)
    self.responsibility_taken_at = Time.zone.now
    super(responsible_user)
  end
85
end