a11yist/app/models/check.rb

133 lines
4.9 KiB
Ruby
Raw Permalink Normal View History

2024-09-05 22:54:38 +02:00
# frozen_string_literal: true
class Check < ApplicationRecord
2024-09-05 22:54:38 +02:00
belongs_to :principle
has_and_belongs_to_many :links
has_and_belongs_to_many :standards
accepts_nested_attributes_for :links, allow_destroy: true
enum :conformity_level, %i[A AA AAA]
enum :priority, %i[highest high normal low]
2024-10-27 18:25:25 +01:00
has_rich_text :annotation_de
2024-09-05 22:54:38 +02:00
has_rich_text :conformity_notice_de
has_rich_text :conformity_notice_en
has_rich_text :quick_criterion_de
has_rich_text :quick_criterion_en
has_rich_text :quick_fail_de
has_rich_text :quick_fail_en
has_rich_text :quick_fix_de
has_rich_text :quick_fix_en
has_rich_text :criterion_de
has_rich_text :criterion_en
has_rich_text :criterion_details_de
has_rich_text :criterion_details_en
has_rich_text :example_de
has_rich_text :example_en
has_rich_text :exemption_details_de
has_rich_text :exemption_details_en
has_rich_text :standard_text_de
has_rich_text :standard_text_en
has_rich_text :test_instructions
has_rich_text :powerpoint_text_de
has_rich_text :powerpoint_text_en
has_rich_text :comment
translates_attributes :name,
:conformity_notice,
:quick_criterion,
:quick_fail,
:quick_fix,
:criterion,
:criterion_details,
:example,
:exemption_details,
:standard_text,
:powerpoint_text
2024-09-12 01:18:04 +02:00
validates :number, uniqueness: true, presence: true
before_validation { self.number = self.class.maximum(:id).to_i + 1 unless self.number.present? }
2024-09-11 22:04:55 +02:00
2024-10-27 02:14:52 +02:00
scope(:search, lambda { |terms|
2024-09-05 22:54:38 +02:00
# TODO: Search only fields for current locale.
2024-09-12 00:50:38 +02:00
joined = joins("INNER JOIN action_text_rich_texts ON action_text_rich_texts.record_id = checks.id AND record_type = 'Check'")
2024-10-27 02:14:52 +02:00
terms.split(" ").uniq.each_with_index do |term, i|
joined = joined.where("checks.name_de LIKE :term OR action_text_rich_texts.body LIKE :term", term: "%#{term}%")
2024-09-12 00:50:38 +02:00
end
2024-10-27 02:14:52 +02:00
joined.distinct
2024-09-12 00:50:38 +02:00
})
scope(:filter_by, lambda { |filter|
relation = where(nil)
2024-10-27 02:14:52 +02:00
filter.slice(:s, :principle_id, :priorities, :standard_ids, :conformity_level, :target_disabilities).each do |key, value|
2024-09-12 00:50:38 +02:00
next unless value.present?
case key
when "s"
relation = relation.search(value)
when "standard_ids"
relation = relation.joins(:standards).where(standards: { id: value })
2024-10-27 02:14:52 +02:00
when "target_disabilities"
cond = String.new
2024-11-03 21:58:25 +01:00
value.intersection(%w[auditory visual physical cognitive]).each_with_index do |v, i|
2024-10-27 02:14:52 +02:00
if i == 0
cond << "checks.#{v} = 1"
else
cond << " OR checks.#{v} = 1"
end
end
relation = relation.where("(#{cond})")
2024-09-12 00:50:38 +02:00
else
relation = relation.where(key => value)
end
end
relation
})
2024-07-19 02:29:18 +02:00
2024-09-11 22:04:55 +02:00
def self.ransackable_attributes(auth_object = nil)
[ "applicable_to_app", "applicable_to_web", "auditory", "cognitive", "conformity_level", "created_at", "external_number", "external_url", "id", "level", "manual_test", "name", "name_de", "name_en", "number", "physical", "position", "principle_id", "priority", "test_url", "updated_at", "visual" ]
end
def self.ransackable_associations(auth_object = nil)
[ "links", "principle", "rich_text_comment", "rich_text_conformity_notice_de", "rich_text_conformity_notice_en", "rich_text_criterion_de", "rich_text_criterion_details_de", "rich_text_criterion_details_en", "rich_text_criterion_en", "rich_text_example_de", "rich_text_example_en", "rich_text_exemption_details_de", "rich_text_exemption_details_en", "rich_text_powerpoint_text_de", "rich_text_powerpoint_text_en", "rich_text_quick_criterion_de", "rich_text_quick_criterion_en", "rich_text_quick_fail_de", "rich_text_quick_fail_en", "rich_text_quick_fix_de", "rich_text_quick_fix_en", "rich_text_standard_text_de", "rich_text_standard_text_en", "rich_text_test_instructions", "standards" ]
end
2024-10-27 02:14:52 +02:00
def self.target_disabilities
%i[visual auditory physical cognitive].index_with { self }
end
2024-09-05 22:54:38 +02:00
def display_target_disabilities
2024-10-27 22:37:11 +01:00
%i[visual auditory physical cognitive].select { |d| send(:"#{d}?") }
.map { |d| I18n.t("disability.#{d}") }
.sort_by(&:downcase)
2024-10-27 22:37:11 +01:00
.join(", ")
2024-09-05 22:54:38 +02:00
end
2024-07-21 00:33:38 +02:00
2024-09-05 22:54:38 +02:00
def display_applicabilities
2024-11-03 21:58:25 +01:00
%i[applicable_to_analogue
applicable_to_app
applicable_to_document
applicable_to_non_web
2024-10-27 22:37:11 +01:00
applicable_to_web].select { |a| send(:"#{a}?") }
.map { |a| I18n.t("applicability.#{a}") }
.sort_by(&:downcase)
2024-10-27 22:37:11 +01:00
.join(", ")
2024-09-05 22:54:38 +02:00
end
2024-10-26 03:17:51 +02:00
def display_label
2024-11-03 21:58:25 +01:00
[ external_number, name_de ].compact_blank.join(" ")
end
def external_number
[ external_number_1, external_number_2, external_number_3 ].compact_blank.join(".")
2024-10-26 03:17:51 +02:00
end
end