-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference_card_form.rb
More file actions
51 lines (41 loc) · 1.75 KB
/
reference_card_form.rb
File metadata and controls
51 lines (41 loc) · 1.75 KB
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
class ReferenceCardForm < StackRequest
validate :date_check
MAX_LENGTH = 91
def submit!
RequestMailer.reference_card_email(self).deliver_later
end
def approve!
RequestMailer.reference_card_approved(self).deliver_later
end
def deny!
RequestMailer.reference_card_denied(self).deliver_later
end
# Add up and return the total number of days this requester
# has been approved for (for the current calendar year)
def days_approved
total_approved_days = 0
approvals = ReferenceCardForm.where('email = ? AND approvedeny = ? AND pass_date_end >= ?', email, true, start_calendar_year)
approvals.each do |approval|
total_approved_days = + (approval.pass_date_end - approval.pass_date).to_i + 1
end
total_approved_days
end
private
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def date_check
date_start = pass_date || nil
date_end = pass_date_end || nil
min_date = Date.current
min_date = created_at.to_date if created_at
if date_start && date_end
num_req = + (date_end - date_start).to_i + 1
errors.add(:pass_date, 'must not be in the past') if date_start < min_date
errors.add(:pass_date_end, 'must not precede access start date') if date_start > date_end
errors.add(:pass_date_end, 'cannot be more than 3 months from the start date') if num_req > MAX_LENGTH
else
errors.add(:pass_date, 'must not be blank and must be in the format mm/dd/yyyy') unless date_start
errors.add(:pass_date_end, 'must not be blank and must be in the format mm/dd/yyyy') unless date_end
end
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
end