Class: Effective::CpdAudit

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/cpd_audit.rb

Constant Summary collapse

ADMIN_PROCESS_REQUEST_OPTIONS =
['Granted', 'Denied']
COMPLETED_STATES =
[:exemption_granted, :closed]
WAITING_ON_ADMIN_STATES =
[:conflicted, :exemption_requested, :extension_requested, :reviewed]
WAITING_ON_REVIEWERS_STATES =
[:submitted]
WAITING_ON_AUDITEE_STATES =
[:opened, :started, :conflicted_resolved, :exemption_denied, :extension_granted, :extension_denied]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#admin_process_requestObject

Returns the value of attribute admin_process_request.



6
7
8
# File 'app/models/effective/cpd_audit.rb', line 6

def admin_process_request
  @admin_process_request
end

#current_stepObject

Returns the value of attribute current_step.



4
5
6
# File 'app/models/effective/cpd_audit.rb', line 4

def current_step
  @current_step
end

#current_userObject

Returns the value of attribute current_user.



3
4
5
# File 'app/models/effective/cpd_audit.rb', line 3

def current_user
  @current_user
end

Instance Method Details

#can_visit_step?(step) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
# File 'app/models/effective/cpd_audit.rb', line 180

def can_visit_step?(step)
  return (step == :complete) if   # Can only view complete step once submitted
  can_revisit_completed_steps(step)
end

#close!Object

Admin action



355
356
357
358
# File 'app/models/effective/cpd_audit.rb', line 355

def close!
  closed!
  send_email(:cpd_audit_closed)
end

#completed?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'app/models/effective/cpd_audit.rb', line 213

def completed?
  COMPLETED_STATES.include?(status.to_sym)
end

#cpd_audit_level_section(wizard_step) ⇒ Object



221
222
223
224
# File 'app/models/effective/cpd_audit.rb', line 221

def cpd_audit_level_section(wizard_step)
  position = (wizard_step.to_s.split('section').last.to_i rescue false)
  cpd_audit_level.cpd_audit_level_sections.find { |section| (section.position + 1) == position }
end

#cpd_audit_response(cpd_audit_level_question) ⇒ Object

Find or build



227
228
229
230
# File 'app/models/effective/cpd_audit.rb', line 227

def cpd_audit_response(cpd_audit_level_question)
  cpd_audit_response = cpd_audit_responses.find { |r| r.cpd_audit_level_question_id == cpd_audit_level_question.id }
  cpd_audit_response ||= cpd_audit_responses.build(cpd_audit: self, cpd_audit_level_question: cpd_audit_level_question)
end

#deadline_dateObject



209
210
211
# File 'app/models/effective/cpd_audit.rb', line 209

def deadline_date
  (extension_date || notification_date)
end

#deadline_to_conflict_of_interestObject



369
370
371
372
373
374
375
# File 'app/models/effective/cpd_audit.rb', line 369

def deadline_to_conflict_of_interest
  return nil unless cpd_audit_level&.conflict_of_interest?
  return nil unless cpd_audit_level.days_to_declare_conflict.present?

  date = (notification_date || created_at || Time.zone.now)
  EffectiveResources.advance_date(date, business_days: cpd_audit_level.days_to_declare_conflict)
end

#deadline_to_exemptionObject



377
378
379
380
381
382
383
# File 'app/models/effective/cpd_audit.rb', line 377

def deadline_to_exemption
  return nil unless cpd_audit_level&.can_request_exemption?
  return nil unless cpd_audit_level.days_to_request_exemption.present?

  date = (notification_date || created_at || Time.zone.now)
  EffectiveResources.advance_date(date, business_days: cpd_audit_level.days_to_request_exemption)
end

#deadline_to_extensionObject



385
386
387
388
389
390
391
# File 'app/models/effective/cpd_audit.rb', line 385

def deadline_to_extension
  return nil unless cpd_audit_level&.can_request_extension?
  return nil unless cpd_audit_level.days_to_request_extension.present?

  date = (notification_date || created_at || Time.zone.now)
  EffectiveResources.advance_date(date, business_days: cpd_audit_level.days_to_request_extension)
end

#deadline_to_submitObject



393
394
395
396
397
398
# File 'app/models/effective/cpd_audit.rb', line 393

def deadline_to_submit
  return nil unless cpd_audit_level&.days_to_submit.present?

  date = (extension_date || notification_date || created_at || Time.zone.now)
  EffectiveResources.advance_date(date, business_days: cpd_audit_level.days_to_submit)
end

#deny_exemption!Object



273
274
275
276
277
# File 'app/models/effective/cpd_audit.rb', line 273

def deny_exemption!
  assign_attributes(exemption_request: false)
  exemption_denied!
  send_email(:cpd_audit_exemption_denied)
end

#deny_extension!Object



306
307
308
309
310
# File 'app/models/effective/cpd_audit.rb', line 306

def deny_extension!
  assign_attributes(extension_request: false)
  extension_denied!
  send_email(:cpd_audit_extension_denied)
end

#dynamic_wizard_stepsObject



174
175
176
177
178
# File 'app/models/effective/cpd_audit.rb', line 174

def dynamic_wizard_steps
  cpd_audit_level.cpd_audit_level_sections.each_with_object({}) do |section, h|
    h["section#{section.position+1}".to_sym] = section.title
  end
end

#email_form_defaults(action) ⇒ Object



360
361
362
# File 'app/models/effective/cpd_audit.rb', line 360

def email_form_defaults(action)
  { from: EffectiveCpd.mailer_sender }
end

#exemption!Object

Auditee wizard action



250
251
252
253
254
255
# File 'app/models/effective/cpd_audit.rb', line 250

def exemption!
  return started! unless exemption_request?

  update!(status: :exemption_requested)
  send_email(:cpd_audit_exemption_request)
end

#extension!Object

Auditee wizard action



280
281
282
283
284
285
# File 'app/models/effective/cpd_audit.rb', line 280

def extension!
  return started! unless extension_request?

  update!(status: :extension_requested)
  send_email(:cpd_audit_extension_request)
end

#grant_exemption!Object



267
268
269
270
271
# File 'app/models/effective/cpd_audit.rb', line 267

def grant_exemption!
  wizard_steps[:submit] ||= Time.zone.now
   && exemption_granted!
  send_email(:cpd_audit_exemption_granted)
end

#grant_extension!Object



297
298
299
300
301
302
303
304
# File 'app/models/effective/cpd_audit.rb', line 297

def grant_extension!
  self.extension_date = extension_request_date
  self.due_date = deadline_to_submit()

  cpd_audit_reviews.each { |cpd_audit_review| cpd_audit_review.extension_granted! }
  extension_granted!
  send_email(:cpd_audit_extension_granted)
end

#in_progress?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'app/models/effective/cpd_audit.rb', line 217

def in_progress?
  COMPLETED_STATES.include?(status.to_sym) == false
end

#process_exemption!Object

Admin action



258
259
260
261
262
263
264
265
# File 'app/models/effective/cpd_audit.rb', line 258

def process_exemption!
  case admin_process_request
  when 'Granted' then grant_exemption!
  when 'Denied' then deny_exemption!
  else
    self.errors.add(:admin_process_request, "can't be blank"); save!
  end
end

#process_extension!Object

Admin action



288
289
290
291
292
293
294
295
# File 'app/models/effective/cpd_audit.rb', line 288

def process_extension!
  case admin_process_request
  when 'Granted' then grant_extension!
  when 'Denied' then deny_extension!
  else
    self.errors.add(:admin_process_request, "can't be blank"); save!
  end
end

#reportable_scopesObject

effective_reports



125
126
127
# File 'app/models/effective/cpd_audit.rb', line 125

def reportable_scopes
  { draft: nil, available: nil, completed: nil, waiting_on_admin: nil, waiting_on_auditee: nil, waiting_on_reviewers: nil }
end

#required_cpd_cycleObject



323
324
325
326
327
328
# File 'app/models/effective/cpd_audit.rb', line 323

def required_cpd_cycle
  @required_cpd_cycle ||= begin
    last_year = ((notification_date || created_at || Time.zone.now) - 1.year).all_year
    Effective::CpdCycle.available.where(start_at: last_year).first
  end
end

#required_stepsObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/models/effective/cpd_audit.rb', line 185

def required_steps
  steps = [:start, :information, :instructions]

  steps << :conflict if cpd_audit_level.conflict_of_interest?

  steps << :exemption if cpd_audit_level.can_request_exemption?

  unless exemption_requested?
    steps << :extension if cpd_audit_level.can_request_extension?
  end

  if exemption_requested? || extension_requested?
    steps += [:waiting]
  end

  steps += [:cpd, :questionnaire] + dynamic_wizard_steps.keys + [:files, :submit, :complete]

  steps
end

#resolve_conflict!Object

Admin action



238
239
240
241
242
243
244
245
246
247
# File 'app/models/effective/cpd_audit.rb', line 238

def resolve_conflict!
  wizard_steps[:conflict] = nil   # Have them complete the conflict step again.

  assign_attributes(conflict_of_interest: false, conflict_of_interest_reason: nil)
  conflicted_resolved!
  

  send_email(:cpd_audit_conflict_resolved)
  true
end

#review!Object

Called in a before_save. Intended for applicant_review to call in its submit! method



346
347
348
349
350
351
352
# File 'app/models/effective/cpd_audit.rb', line 346

def review!
  return false unless 
  return false unless cpd_audit_reviews.present? && cpd_audit_reviews.all?(&:completed?)

  reviewed!
  send_email(:cpd_audit_reviewed)
end

#send_email(email) ⇒ Object



364
365
366
367
# File 'app/models/effective/cpd_audit.rb', line 364

def send_email(email)
  EffectiveCpd.send_email(email, self, email_form_params) unless email_form_skip?
  true
end

#start!Object

Auditee wizard action



233
234
235
# File 'app/models/effective/cpd_audit.rb', line 233

def start!
  started!
end

#submit!Object

Auditee wizard action



331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'app/models/effective/cpd_audit.rb', line 331

def submit!
  if conflict_of_interest?
    conflicted!
    send_email(:cpd_audit_conflicted)
  else
    

    cpd_audit_reviews.each { |cpd_audit_review| cpd_audit_review.ready! }
    send_email(:cpd_audit_submitted)
  end

  true
end

#to_sObject



170
171
172
# File 'app/models/effective/cpd_audit.rb', line 170

def to_s
  persisted? ? "#{cpd_audit_level} Audit of #{user}" : 'audit'
end

#user_cpd_completed?Boolean

Returns:

  • (Boolean)


318
319
320
321
# File 'app/models/effective/cpd_audit.rb', line 318

def user_cpd_completed?
  return true if required_cpd_cycle.blank?
  user.cpd_statements.any? { |s| s.completed? && s.cpd_cycle_id == required_cpd_cycle.id }
end

#user_cpd_required?Boolean

Require CPD step

Returns:

  • (Boolean)


313
314
315
316
# File 'app/models/effective/cpd_audit.rb', line 313

def user_cpd_required?
  return false unless user.cpd_audit_cpd_required?
  required_cpd_cycle.present?
end

#wizard_step_title(step) ⇒ Object



205
206
207
# File 'app/models/effective/cpd_audit.rb', line 205

def wizard_step_title(step)
  WIZARD_STEPS[step] || dynamic_wizard_steps.fetch(step)
end