2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-10-17 17:38:51 +02:00
|
|
|
class Conditions::ConditionsErrorsComponent < ApplicationComponent
|
|
|
|
def initialize(conditions:, source_tdcs:)
|
|
|
|
@conditions, @source_tdcs = conditions, source_tdcs
|
2022-07-04 12:26:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def errors
|
2022-09-27 10:18:59 +02:00
|
|
|
errors = @conditions
|
2023-10-17 17:38:51 +02:00
|
|
|
.flat_map { |condition| condition.errors(@source_tdcs) }
|
2022-07-04 12:26:14 +02:00
|
|
|
.uniq
|
2022-09-27 10:18:59 +02:00
|
|
|
|
|
|
|
# if a tdc is not available (has been removed for example)
|
|
|
|
# it causes a lot of errors (incompatible type for example)
|
|
|
|
# only the root cause is displayed
|
|
|
|
messages = if errors.include?({ type: :not_available })
|
|
|
|
[t('not_available', scope: '.errors')]
|
|
|
|
else
|
|
|
|
errors.map { |error| humanize(error) }
|
|
|
|
end
|
|
|
|
|
|
|
|
to_html_list(messages)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_html_list(messages)
|
|
|
|
messages
|
2022-07-04 12:26:14 +02:00
|
|
|
.map { |message| tag.li(message) }
|
|
|
|
.then { |lis| tag.ul(lis.reduce(&:+)) }
|
|
|
|
end
|
|
|
|
|
2022-09-14 10:43:18 +02:00
|
|
|
def humanize(error)
|
|
|
|
case error
|
|
|
|
in { type: :not_available }
|
2023-02-06 15:52:21 +01:00
|
|
|
in { type: :incompatible, stable_id: nil }
|
2022-07-06 17:19:04 +02:00
|
|
|
t('not_available', scope: '.errors')
|
2022-09-14 10:43:18 +02:00
|
|
|
in { type: :unmanaged, stable_id: stable_id }
|
2023-10-17 17:38:51 +02:00
|
|
|
targeted_champ = @source_tdcs.find { |tdc| tdc.stable_id == stable_id }
|
2022-09-14 10:43:18 +02:00
|
|
|
t('unmanaged',
|
|
|
|
scope: '.errors',
|
2022-07-06 17:19:04 +02:00
|
|
|
libelle: targeted_champ.libelle,
|
2022-07-11 22:34:40 +02:00
|
|
|
type_champ: t(targeted_champ.type_champ, scope: 'activerecord.attributes.type_de_champ.type_champs')&.downcase)
|
2022-09-14 10:43:18 +02:00
|
|
|
in { type: :incompatible, stable_id: stable_id, right: right, operator_name: operator_name }
|
2023-10-17 17:38:51 +02:00
|
|
|
targeted_champ = @source_tdcs.find { |tdc| tdc.stable_id == stable_id }
|
2022-07-06 17:19:04 +02:00
|
|
|
t('incompatible', scope: '.errors',
|
|
|
|
libelle: targeted_champ.libelle,
|
2022-07-11 22:34:40 +02:00
|
|
|
type_champ: t(targeted_champ.type_champ, scope: 'activerecord.attributes.type_de_champ.type_champs')&.downcase,
|
2022-07-06 17:19:04 +02:00
|
|
|
operator: t(operator_name, scope: 'logic.operators').downcase,
|
|
|
|
right: right.to_s.downcase)
|
2022-09-14 10:43:18 +02:00
|
|
|
in { type: :required_number, operator_name: operator_name }
|
|
|
|
t('required_number', scope: '.errors',
|
|
|
|
operator: t(operator_name, scope: 'logic.operators'))
|
|
|
|
in { type: :not_included, stable_id: stable_id, right: right }
|
2023-10-17 17:38:51 +02:00
|
|
|
targeted_champ = @source_tdcs.find { |tdc| tdc.stable_id == stable_id }
|
2022-09-14 10:43:18 +02:00
|
|
|
t('not_included', scope: '.errors',
|
|
|
|
libelle: targeted_champ.libelle,
|
|
|
|
right: right.to_s.downcase)
|
|
|
|
in { type: :required_list }
|
|
|
|
t('required_list', scope: '.errors')
|
2023-04-25 19:53:06 +02:00
|
|
|
in { type: :required_include, operator_name: "Logic::Eq" }
|
|
|
|
t("required_include.eq", scope: '.errors')
|
|
|
|
in { type: :required_include, operator_name: "Logic::NotEq" }
|
|
|
|
t("required_include.not_eq", scope: '.errors')
|
2022-09-14 10:43:18 +02:00
|
|
|
else
|
|
|
|
nil
|
2022-07-04 12:26:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render?
|
|
|
|
@conditions
|
2023-10-17 17:38:51 +02:00
|
|
|
.filter { |condition| condition.errors(@source_tdcs).present? }
|
2022-07-04 12:26:14 +02:00
|
|
|
.present?
|
|
|
|
end
|
|
|
|
end
|