semester_process.rb 2.54 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

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

  def reminders
    semester_process_mails.where(kind: 'reminder')
  end
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

  def mail?
    self.mail_subject_template && self.mail_body_template
  end

  def reminder?
    self.reminder_mail_subject_template && self.reminder_mail_body_template
  end

  def kind
    if self.mail?
      return :mail
    elsif self.reminder?
      return :reminder
    end
  end

  def subject
    self.mail_subject_template || self.reminder_mail_subject_template
  end

  def body
    self.mail_body_template || self.reminder_mail_body_template
  end
50
51
52

  # will only return an array, not a AD-result
  delegate :missions, to: :semester_process_volunteers
53
54

  # creates semester date range from string '[year],[semester_number]' e.g. '2018,2'
55
56
57
58
59
60
61
62
63
64
  def semester=(set_semester)
    set_semester = Semester.parse(set_semester) if set_semester.is_a?(String)

    # for very strange reason the end of the range is shifted one day after save
    # possibly a bug in Active Directory
    super(set_semester.begin..set_semester.end.advance(days: -1))
  end

  def semester_t(short: true)
    Semester.i18n_t(semester, short: short)
65
66
  end

67
68
69
70
  def semester_period
    Semester.period(semester)
  end

71
72
73
  def build_semester_volunteers(volunteers, selected = nil)
    volunteers = volunteers.where(id: selected) if selected
    semester_process_volunteers << volunteers.to_a.map do |volunteer|
Jiri Strojil's avatar
Jiri Strojil committed
74
      spv = SemesterProcessVolunteer.new(volunteer: volunteer, selected: false)
75
76
77
78
      spv.build_missions(semester)
      spv
    end
  end
79
80

  def build_volunteers_hours_feedbacks_and_mails
81
    semester_process_volunteers.map(&:build_hours_and_mails)
82
  end
83
end