demarches-normaliennes/app/components/conditions/conditions_errors_component.rb

76 lines
2.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
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
errors = @conditions
.flat_map { |condition| condition.errors(@source_tdcs) }
2022-07-04 12:26:14 +02:00
.uniq
# 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
def humanize(error)
case error
in { type: :not_available }
in { type: :incompatible, stable_id: nil }
2022-07-06 17:19:04 +02:00
t('not_available', scope: '.errors')
in { type: :unmanaged, stable_id: stable_id }
targeted_champ = @source_tdcs.find { |tdc| tdc.stable_id == stable_id }
t('unmanaged',
scope: '.errors',
2022-07-06 17:19:04 +02:00
libelle: targeted_champ.libelle,
type_champ: t(targeted_champ.type_champ, scope: 'activerecord.attributes.type_de_champ.type_champs')&.downcase)
in { type: :incompatible, stable_id: stable_id, right: right, operator_name: operator_name }
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,
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)
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 }
targeted_champ = @source_tdcs.find { |tdc| tdc.stable_id == stable_id }
t('not_included', scope: '.errors',
libelle: targeted_champ.libelle,
right: right.to_s.downcase)
in { type: :required_list }
t('required_list', scope: '.errors')
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')
else
nil
2022-07-04 12:26:14 +02:00
end
end
def render?
@conditions
.filter { |condition| condition.errors(@source_tdcs).present? }
2022-07-04 12:26:14 +02:00
.present?
end
end