blob: cce24410c39b7ffce6818a72c1c4e4b706956b67 (
plain)
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
  | 
class ComplianceCheckSet < ActiveRecord::Base
  extend Enumerize
  belongs_to :referential
  belongs_to :compliance_control_set
  belongs_to :workbench
  belongs_to :parent, polymorphic: true
  has_many :compliance_check_blocks
  has_many :compliance_checks
  has_many :compliance_check_resources
  has_many :compliance_check_messages
  enumerize :status, in: %w[new pending successful warning failed running aborted canceled]
  scope :where_created_at_between, ->(period_range) do
    where('created_at BETWEEN :begin AND :end', begin: period_range.begin, end: period_range.end)
  end
  def update_status
    statuses = compliance_check_resources.map do |resource|
      case resource.status
      when 'ERROR'
        return update(status: 'failed')
      when 'WARNING'
        return update(status: 'warning')
      else
        resource.status
      end
    end
    if all_statuses_ok(statuses)
      return update(status: 'successful')
    end
  end
  private
  def all_statuses_ok(statuses)
    uniform_statuses = statuses.uniq
    uniform_statuses.length == 1 && uniform_statuses.first == 'OK'
  end
end
  |