2024-09-05 22:54:38 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-07-16 20:22:59 +02:00
|
|
|
class Element < ApplicationRecord
|
2024-10-31 23:13:18 +01:00
|
|
|
has_rich_text :description
|
2024-07-16 20:22:59 +02:00
|
|
|
|
2024-10-31 23:13:18 +01:00
|
|
|
belongs_to :page, touch: true
|
2024-11-11 05:00:51 +01:00
|
|
|
has_many :success_criteria, -> { order(:position) }, dependent: :destroy
|
2024-07-20 16:52:12 +02:00
|
|
|
|
2024-10-31 23:13:18 +01:00
|
|
|
delegate :report, to: :page
|
2024-07-26 00:59:00 +02:00
|
|
|
|
2024-11-11 04:04:13 +01:00
|
|
|
before_validation :set_position
|
|
|
|
|
before_update :update_positions, if: :position_changed?
|
2024-11-01 03:26:46 +01:00
|
|
|
|
2024-11-12 22:43:59 +01:00
|
|
|
has_one_attached :screenshot do |attachable|
|
|
|
|
|
attachable.variant :thumbnail, resize_to_limit: [ 200, 200 ]
|
|
|
|
|
end
|
|
|
|
|
|
2024-11-23 21:11:01 +01:00
|
|
|
scope :failed, -> { where(SuccessCriterion.where(result: SuccessCriterion.results[:failed]).arel.exists) }
|
|
|
|
|
|
2024-07-26 00:59:00 +02:00
|
|
|
# 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)
|
2024-11-03 21:58:25 +01:00
|
|
|
element
|
2024-07-26 00:59:00 +02:00
|
|
|
min_failed = success_criteria.select(&:failed?).map(&:level).min
|
|
|
|
|
possible_levels = success_criteria.select(&:passed?).map(&:level).uniq
|
|
|
|
|
|
|
|
|
|
return nil if possible_levels.empty?
|
|
|
|
|
|
2024-09-05 22:54:38 +02:00
|
|
|
Rails.logger.debug possible_levels.inspect
|
|
|
|
|
Rails.logger.debug min_failed
|
2024-07-26 00:59:00 +02:00
|
|
|
possible_levels[possible_levels.find_index(min_failed) - 1]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def max_level
|
|
|
|
|
@max_level ||= success_criteria.reject(&:not_applicable?).max(&:level)
|
|
|
|
|
end
|
2024-11-01 03:26:46 +01:00
|
|
|
|
2024-11-11 04:04:13 +01:00
|
|
|
def number
|
|
|
|
|
"#{page.position}.#{position}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
2024-11-01 03:26:46 +01:00
|
|
|
def set_position
|
|
|
|
|
Rails.logger.debug("element: position #{position}")
|
|
|
|
|
self.position ||= (page.elements.pluck(:position).max || 0) + 1
|
|
|
|
|
end
|
2024-11-03 21:58:25 +01:00
|
|
|
|
2024-11-11 04:04:13 +01:00
|
|
|
def update_positions
|
|
|
|
|
if position_was
|
|
|
|
|
page.elements.where("position > ?", position_was).update_all("position = position - 1")
|
|
|
|
|
end
|
|
|
|
|
page.elements.where(position: position..).update_all("position = position + 1")
|
2024-11-03 21:58:25 +01:00
|
|
|
end
|
2024-07-16 20:22:59 +02:00
|
|
|
end
|