2023-09-19 11:07:50 +02:00
# frozen_string_literal: true
class Dossiers :: ErrorsFullMessagesComponent < ApplicationComponent
ErrorDescriptor = Data . define ( :anchor , :label , :error_message )
2024-06-05 18:00:19 +02:00
def initialize ( dossier : )
2023-09-19 11:07:50 +02:00
@dossier = dossier
end
def dedup_and_partitioned_errors
2024-06-05 18:00:19 +02:00
@dossier . errors . to_enum # ActiveModel::Errors.to_a is an alias to full_messages, we don't want that
2023-09-19 11:07:50 +02:00
. to_a # but enum.to_a gives back an array
. uniq { | error | [ error . inner_error . base ] } # dedup cumulated errors from dossier.champs, dossier.champs_public, dossier.champs_private which run the validator one time per association
2023-11-22 12:06:21 +01:00
. map { | error | to_error_descriptor ( error ) }
2023-09-19 11:07:50 +02:00
end
2023-11-22 12:06:21 +01:00
def to_error_descriptor ( error )
model = error . inner_error . base
if model . respond_to? ( :libelle ) # a Champ or something acting as a Champ
ErrorDescriptor . new ( " # #{ model . labelledby_id } " , model . libelle . truncate ( 200 ) , error . message )
else
ErrorDescriptor . new ( " # #{ model . model_name . singular } _ #{ error . attribute } " , model . class . human_attribute_name ( error . attribute ) , error . message )
end
2023-09-19 11:07:50 +02:00
end
def render?
2024-06-05 18:00:19 +02:00
! @dossier . errors . empty?
2023-09-19 11:07:50 +02:00
end
end