Class: Effective::CpdScorer

Inherits:
Object
  • Object
show all
Includes:
EffectiveCpdHelper, EffectiveResourcesHelper
Defined in:
app/models/effective/cpd_scorer.rb

Instance Method Summary collapse

Methods included from EffectiveCpdHelper

#cpd_audit_label, #cpd_audit_level_label, #cpd_audit_levels_label, #cpd_audit_review_label, #cpd_audit_reviews_label, #cpd_auditee_label, #cpd_auditees_label, #cpd_audits_label, #cpd_credit_label, #cpd_credits_label, #cpd_cycle_label, #cpd_cycles_label, #cpd_name_label, #cpd_reviewer_label, #cpd_reviewers_label, #cpd_rule_formula_hint, #cpd_score, #cpd_statement_label, #cpd_statement_submit_label, #cpd_statements_label, #cpd_target_label, #cpd_targets_label, #effective_cpd_categories

Constructor Details

#initialize(user:, from: nil) ⇒ CpdScorer

Returns a new instance of CpdScorer.



9
10
11
12
13
14
15
16
17
18
# File 'app/models/effective/cpd_scorer.rb', line 9

def initialize(user:, from: nil)
  @cycles = CpdCycle.deep.sorted.all
  @statements = EffectiveCpd.CpdStatement.deep.where(user: user).sorted.all

  if from.present?
    raise('expected from to be a CpdStatement') unless from.class.respond_to?(:effective_cpd_statement?)

    @statements = @statements.where('cpd_cycle_id >= ?', from.cpd_cycle_id)
  end
end

Instance Method Details

#score!Object



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
# File 'app/models/effective/cpd_scorer.rb', line 20

def score!
  @statements.each_with_index do |statement, index|
    prev_statement = @statements[index-1] if index > 0

    Array(prev_statement&.cpd_statement_activities).each do |activity|
      if activity.marked_for_destruction? # Cascade this down the line
        statement.cpd_statement_activities.each { |a| a.mark_for_destruction if a.original == activity }
      end

      if can_carry_forward?(activity, statement.cpd_cycle)
        save_carry_forward_activity(activity, statement)
      else
        delete_carry_forward_activity(activity, statement)
      end
    end

    # An activity was deleted from a previous statement
    statement.cpd_statement_activities.each do |activity|
      activity.mark_for_destruction if activity.original_id.present? && activity.original.blank?
    end

    score_statement(statement)
  end

  save!
end