Class: ServicePack

Inherits:
ApplicationRecord show all
Defined in:
app/models/service_pack.rb

Overview

freeze_literal_string: true

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_expired_spObject

START CRON JOBS ### modify User param first deliver_later doesn’t work



126
127
128
129
130
# File 'app/models/service_pack.rb', line 126

def self.check_expired_sp
  ServicePack.find_each do |sp|
    ServicePacksMailer.expired_email(User.last, sp).deliver_now if sp.expired?
  end
end

.check_threshold1Object

will be replaced # notify immediately at entries

def self.check_used_up
  ServicePack.find_each do |sp|
    ServicePacksMailer.used_up_email(User.last, sp).deliver_now if sp.used_up?
  end
end


140
141
142
143
144
# File 'app/models/service_pack.rb', line 140

def self.check_threshold1
  ServicePack.notifiable(1).find_each do |sp|
    ServicePacksMailer.notify_under_threshold1(User.last, sp).deliver_now
  end
end

.check_threshold2Object



146
147
148
149
150
# File 'app/models/service_pack.rb', line 146

def self.check_threshold2
  ServicePack.notifiable(2).find_each do |sp|
    ServicePacksMailer.notify_under_threshold2(User.last, sp).deliver_now
  end
end

Instance Method Details

#assigned?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/models/service_pack.rb', line 73

def assigned?
  assigns.where(assigned: true).exists?
end

#assignmentsObject

def self.cron_send_default

# modify the User param
ServicePack.find_each do |sp|
  ExpiredSpMailer.expired_email(User.last, sp).deliver_now
end

end END TESTING ONLY



112
113
114
# File 'app/models/service_pack.rb', line 112

def assignments
  assigns.where(assigned: true)
end

#available?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/service_pack.rb', line 63

def available?
  !unavailable?
end

#cron_send_specificObject



99
100
101
102
# File 'app/models/service_pack.rb', line 99

def cron_send_specific
  # modify the User param
  ServicePacksMailer.expired_email(User.last, ServicePack.first).deliver_later
end

#default_remained_unitsObject



42
43
44
# File 'app/models/service_pack.rb', line 42

def default_remained_units
  self.remained_units = total_units
end

#expired?Boolean

CHECKERS ###

Returns:

  • (Boolean)


51
52
53
# File 'app/models/service_pack.rb', line 51

def expired?
  true if Time.now > expired_date
end

#expired_notificationObject

FOR TESTING ONLY



92
93
94
95
96
97
# File 'app/models/service_pack.rb', line 92

def expired_notification # send to the first user in the first record in the DB
  if expired?
    user = User.first
    ServicePacksMailer.expired_email(user, self).deliver_later
  end
end

#grant(units) ⇒ Object



116
117
118
119
120
# File 'app/models/service_pack.rb', line 116

def grant(units)
  self.total_units += units
  self.remained_units += units
  self
end

#is_notify?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'app/models/service_pack.rb', line 67

def is_notify?
  # so what is with the two thresholds!?
  dates_to_notify = (expired_date - Date.today).to_i
  dates_to_notify.between?(1, 2)
end

#revoke_all_assignmentsObject



46
47
48
# File 'app/models/service_pack.rb', line 46

def revoke_all_assignments
  assignments.where(assigned: true).update_all(assigned: false, unassign_date: Date.today)
end

#unavailable?Boolean

available SP might not be assignable

Returns:

  • (Boolean)


59
60
61
# File 'app/models/service_pack.rb', line 59

def unavailable? # available SP might not be assignable
  used_up? && expired?
end

#used_up?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/service_pack.rb', line 55

def used_up?
  true if remained_units <= 0
end