a11yist/app/models/success_criterion.rb

49 lines
1.2 KiB
Ruby
Raw Normal View History

2024-09-05 22:54:38 +02:00
# frozen_string_literal: true
class SuccessCriterion < ApplicationRecord
2024-10-31 23:13:18 +01:00
belongs_to :element, touch: true
belongs_to :check, optional: true
2024-07-19 02:29:18 +02:00
2024-10-31 23:13:18 +01:00
has_rich_text :test_comment
has_rich_text :quick_criterion
has_rich_text :quick_fail
has_rich_text :quick_fix
2024-07-19 02:29:18 +02:00
2024-10-31 23:13:18 +01:00
delegate :page, :report, to: :element
enum :result, %i[passed failed not_applicable]
enum :level, %i[A AA AAA]
2024-07-26 00:59:00 +02:00
2024-11-01 03:26:46 +01:00
before_save :set_position
2024-11-11 04:04:13 +01:00
before_update :update_positions, if: :position_changed?
2024-11-01 03:26:46 +01:00
validates :result, inclusion: { in: self.results.keys + [ nil ] }
2024-07-26 00:59:00 +02:00
def level_value
return nil unless level
self.class.levels.values.index_of(level)
end
2024-10-31 23:13:18 +01:00
def header
"HEADER"
end
2024-11-01 03:26:46 +01:00
2024-11-11 04:04:13 +01:00
def number
[ page.position, element.position, position ].join(".")
end
2024-11-01 03:26:46 +01:00
private
def set_position
self.position ||= (element.success_criteria.pluck(:position).max || 0) + 1
Rails.logger.debug("set position: "+position.to_s)
end
2024-11-11 04:04:13 +01:00
def update_positions
if position_was
element.success_criteria.where("position > ?", position_was).update_all("position = position - 1")
end
element.success_criteria.where(position: position..).update_all("position = position + 1")
end
end