tech(amelioration.chorus_configuration): deplace les methodes liées au rendu du model ChorusConfiguration dans le ChorusFormComponent

This commit is contained in:
Martin 2023-10-20 16:29:53 +02:00
parent a6cf22edb8
commit 39abf283d9
3 changed files with 30 additions and 29 deletions

View file

@ -3,6 +3,7 @@ class Procedure::ChorusFormComponent < ApplicationComponent
def initialize(procedure:)
@procedure = procedure
@chorus_configuration = @procedure.chorus_configuration
end
def map_attribute_to_autocomplete_endpoint
@ -12,4 +13,30 @@ class Procedure::ChorusFormComponent < ApplicationComponent
referentiel_de_programmation: data_sources_search_ref_programmation_path
}
end
def format_displayed_value(attribute_name)
case attribute_name
when :centre_de_coup
ChorusConfiguration.format_centre_de_coup_label(@chorus_configuration.centre_de_coup)
when :domaine_fonctionnel
ChorusConfiguration.format_domaine_fonctionnel_label(@chorus_configuration.domaine_fonctionnel)
when :referentiel_de_programmation
ChorusConfiguration.format_ref_programmation_label(@chorus_configuration.referentiel_de_programmation)
else
raise 'unknown attribute_name'
end
end
def format_hidden_value(attribute_name)
case attribute_name
when :centre_de_coup
@chorus_configuration.centre_de_coup.to_json
when :domaine_fonctionnel
@chorus_configuration.domaine_fonctionnel.to_json
when :referentiel_de_programmation
@chorus_configuration.referentiel_de_programmation.to_json
else
raise 'unknown attribute_name'
end
end
end

View file

@ -1,9 +1,9 @@
= form_for([procedure, procedure.chorus_configuration],url: admin_procedure_chorus_path(procedure), method: :put) do |f|
= form_for([procedure, @chorus_configuration],url: admin_procedure_chorus_path(procedure), method: :put) do |f|
- map_attribute_to_autocomplete_endpoint.map do |chorus_configuration_attribute, datasource_endpoint|
- label_class_name = "#{chorus_configuration_attribute}-label"
.fr-select-group
= f.label chorus_configuration_attribute, class: 'fr-label', id: label_class_name
= render Dsfr::ComboboxComponent.new form: f, name: :chorus_configuration_attribute, url: datasource_endpoint, selected: procedure.chorus_configuration.format_displayed_value(chorus_configuration_attribute), id: chorus_configuration_attribute, class: 'fr-select', describedby: label_class_name do
= f.hidden_field chorus_configuration_attribute, data: { value_slot: 'data' }, value: procedure.chorus_configuration.format_hidden_value(chorus_configuration_attribute)
= render Dsfr::ComboboxComponent.new form: f, name: :chorus_configuration_attribute, url: datasource_endpoint, selected: format_displayed_value(chorus_configuration_attribute), id: chorus_configuration_attribute, class: 'fr-select', describedby: label_class_name do
= f.hidden_field chorus_configuration_attribute, data: { value_slot: 'data' }, value: format_hidden_value(chorus_configuration_attribute)
= f.submit "Enregister", class: 'fr-btn'

View file

@ -6,32 +6,6 @@ class ChorusConfiguration
attribute :domaine_fonctionnel, :simple_json, default: '{}'
attribute :referentiel_de_programmation, :simple_json, default: '{}'
def format_displayed_value(attribute_name)
case attribute_name
when :centre_de_coup
ChorusConfiguration.format_centre_de_coup_label(centre_de_coup)
when :domaine_fonctionnel
ChorusConfiguration.format_domaine_fonctionnel_label(domaine_fonctionnel)
when :referentiel_de_programmation
ChorusConfiguration.format_ref_programmation_label(referentiel_de_programmation)
else
raise 'unknown attribute_name'
end
end
def format_hidden_value(attribute_name)
case attribute_name
when :centre_de_coup
centre_de_coup.to_json
when :domaine_fonctionnel
domaine_fonctionnel.to_json
when :referentiel_de_programmation
referentiel_de_programmation.to_json
else
raise 'unknown attribute_name'
end
end
def self.format_centre_de_coup_label(api_result)
return "" if api_result.blank?
api_result = api_result.symbolize_keys