-
Notifications
You must be signed in to change notification settings - Fork 13
/
application_letter.rb
157 lines (139 loc) · 5.12 KB
/
application_letter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# == Schema Information
#
# Table name: application_letters
#
# id :integer not null, primary key
# motivation :string
# user_id :integer not null
# event_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# status :integer not null
#
class ApplicationLetter < ActiveRecord::Base
belongs_to :user
belongs_to :event
has_many :application_notes
serialize :custom_application_fields, Array
VALID_GRADES = 5..13
validates :user, :event, :motivation, :coding_skills, :emergency_number,:organisation, presence: true
validates :grade, presence: true, numericality: { only_integer: true }
#Use 0 as default for hidden event applications
validates_inclusion_of :grade, in: (VALID_GRADES.to_a.push(0))
validates :vegetarian, :vegan, :allergic, inclusion: { in: [true, false] }
validates :vegetarian, :vegan, :allergic, exclusion: { in: [nil] }
validate :deadline_cannot_be_in_the_past, :if => Proc.new { |letter| !(letter.status_changed? || letter.status_notification_sent_changed?) }
validate :status_notification_sent_cannot_be_changed, :if => Proc.new { |letter| letter.status_notification_sent_changed? }
validate :status_cannot_be_changed, :if => Proc.new { |letter| letter.status_changed? }
enum status: {accepted: 1, rejected: 0, pending: 2, alternative: 3, canceled: 4}
validates :status, inclusion: { in: statuses.keys }
# Returns an array of selectable statuses
#
# @param none
# @return [Array <String>] array of selectable statuses
def self.selectable_statuses
["accepted","rejected","pending","alternative"]
end
# Checks if the deadline is over
# additionally only return if event and event.application_deadline is present
#
# @param none
# @return [Boolean] true if deadline is over
def after_deadline?
event.after_deadline? if event.present?
end
# Checks if it is allowed to change the status of the application
#
# @param none
# @return [Boolean] true if status changes are allowed
def status_change_allowed?
if event.phase == :execution
(status_was == 'accepted' && status == 'canceled') || (status_was == 'alternative' && status == 'accepted')
elsif event.phase == :selection && event.participant_selection_locked
false
else
true
end
end
# Checks if it is allowed to set the status_notification_sent flag
#
# @param none
# @return [Boolean] true if it can be changed
def status_notification_sent_change_allowed?
event.phase == :selection || event.phase == :execution
end
# Validator for after_deadline?
# Adds error
def deadline_cannot_be_in_the_past
if after_deadline?
errors.add(:event, I18n.t("application_letters.form.warning"))
end
end
# Since EatingHabits are persited in booleans we need to generate a
# EatingHabitStateCode to allow sorting
# US 28_4.5
def get_eating_habit_state
eating_habit_state = 0
eating_habit_state += 4 if vegetarian
eating_habit_state += 5 if vegan
eating_habit_state += 99 if allergic
return eating_habit_state
end
# Chooses right status based on status and event deadline
#
# @param none
# @return [String] to display on the website
def status_type
case ApplicationLetter.statuses[status]
when ApplicationLetter.statuses[:accepted]
return I18n.t("application_status.accepted")
when ApplicationLetter.statuses[:rejected]
return I18n.t("application_status.rejected")
when ApplicationLetter.statuses[:pending]
if after_deadline?
return I18n.t("application_status.pending_after_deadline")
else
return I18n.t("application_status.pending_before_deadline")
end
when ApplicationLetter.statuses[:canceled]
return I18n.t("application_status.canceled")
else
return I18n.t("application_status.alternative")
end
end
# Validator for status_change_allowed?
# Adds error
def status_cannot_be_changed
unless status_change_allowed?
errors.add(:event, I18n.t('application_letters.validation.status_cannot_be_changed'))
end
end
# Validator for status_change_allowed?
# Adds error
def status_notification_sent_cannot_be_changed
unless status_notification_sent_change_allowed?
errors.add(:event, I18n.t('application_letters.validation.status_notification_sent_cannot_be_changed'))
end
end
# Returns the age of the user based on the date the event starts
#
# @param none
# @return [Int] for age as number of years
def applicant_age_when_event_starts
user.profile.age_at_time(event.start_date)
end
# Returns an array of eating habits (including allergies, vegan and vegetarian)
#
# @param none
# @return [Array <String>] array of eating habits, empty if none
def eating_habits
habits = Array.new
habits.push(ApplicationLetter.human_attribute_name(:vegetarian)) if vegetarian
habits.push(ApplicationLetter.human_attribute_name(:vegan)) if vegan
habits.push(ApplicationLetter.human_attribute_name(:allergies)) if allergic
habits
end
def allergic
not allergies.empty?
end
end