a11yist/app/models/element.rb
david 729ed13521
Some checks failed
/ Run tests (push) Failing after 1m19s
/ Run system tests (push) Failing after 1m18s
/ Build, push and deploy image (push) Has been skipped
start of iteration 2
2024-10-31 23:13:18 +01:00

33 lines
971 B
Ruby

# frozen_string_literal: true
class Element < ApplicationRecord
has_rich_text :description
belongs_to :page, touch: true
has_many :success_criteria, dependent: :destroy
delegate :report, to: :page
# Calculate actual conformity level:
# - if a success_criterion has result :failed -> the confirmity_level
# of that success_criterion is not reached.
# - the resulting level is the highest readched level
# abeying rule above.
def level
return nil
return nil unless success_criteria.all(&:result)
min_failed = success_criteria.select(&:failed?).map(&:level).min
possible_levels = success_criteria.select(&:passed?).map(&:level).uniq
return nil if possible_levels.empty?
Rails.logger.debug possible_levels.inspect
Rails.logger.debug min_failed
possible_levels[possible_levels.find_index(min_failed) - 1]
end
def max_level
@max_level ||= success_criteria.reject(&:not_applicable?).max(&:level)
end
end