diff --git a/app/models/champ.rb b/app/models/champ.rb index e11dfeb20..fc81892ce 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -110,8 +110,8 @@ class Champ < ApplicationRecord value.present? ? value.to_s : '' end - def for_export - value.presence + def for_export(path = :value) + path == :value ? value.presence : nil end def for_api @@ -122,8 +122,8 @@ class Champ < ApplicationRecord to_s end - def for_tag - value.present? ? value.to_s : '' + def for_tag(path = :value) + path == :value && value.present? ? value.to_s : '' end def main_value_name diff --git a/app/models/champs/address_champ.rb b/app/models/champs/address_champ.rb index 487f472e7..169180bcf 100644 --- a/app/models/champs/address_champ.rb +++ b/app/models/champs/address_champ.rb @@ -42,12 +42,26 @@ class Champs::AddressChamp < Champs::TextChamp address_label.presence || '' end - def for_tag - address_label + def for_tag(path = :value) + case path + when :value + address_label + when :departement + departement_code_and_name || '' + when :commune + commune_name || '' + end end - def for_export - value.present? ? address_label : nil + def for_export(path = :value) + case path + when :value + value.present? ? address_label : nil + when :departement + departement_code_and_name + when :commune + commune_name + end end def for_api diff --git a/app/models/champs/boolean_champ.rb b/app/models/champs/boolean_champ.rb index 6aac3e9fd..03d304bea 100644 --- a/app/models/champs/boolean_champ.rb +++ b/app/models/champs/boolean_champ.rb @@ -25,11 +25,11 @@ class Champs::BooleanChamp < Champ processed_value end - def for_tag + def for_tag(path = :value) processed_value end - def for_export + def for_export(path = :value) processed_value end diff --git a/app/models/champs/carte_champ.rb b/app/models/champs/carte_champ.rb index c3114388e..81f71956f 100644 --- a/app/models/champs/carte_champ.rb +++ b/app/models/champs/carte_champ.rb @@ -79,7 +79,7 @@ class Champs::CarteChamp < Champ nil end - def for_export + def for_export(path = :value) geo_areas.map(&:label).join("\n") end diff --git a/app/models/champs/checkbox_champ.rb b/app/models/champs/checkbox_champ.rb index 577588c54..f58bc76d0 100644 --- a/app/models/champs/checkbox_champ.rb +++ b/app/models/champs/checkbox_champ.rb @@ -1,5 +1,5 @@ class Champs::CheckboxChamp < Champs::BooleanChamp - def for_export + def for_export(path = :value) true? ? 'on' : 'off' end diff --git a/app/models/champs/commune_champ.rb b/app/models/champs/commune_champ.rb index f542fc49f..d7977c295 100644 --- a/app/models/champs/commune_champ.rb +++ b/app/models/champs/commune_champ.rb @@ -2,8 +2,26 @@ class Champs::CommuneChamp < Champs::TextChamp store_accessor :value_json, :code_departement, :code_postal, :code_region before_save :on_codes_change, if: :should_refresh_after_code_change? - def for_export - [to_s, code? ? code : '', departement? ? departement_code_and_name : ''] + def for_export(path = :value) + case path + when :value + to_s + when :departement + departement_code_and_name || '' + when :code + code || '' + end + end + + def for_tag(path = :value) + case path + when :value + to_s + when :departement + departement_code_and_name || '' + when :code + code || '' + end end def departement_name diff --git a/app/models/champs/date_champ.rb b/app/models/champs/date_champ.rb index d372eecea..4a9d1a215 100644 --- a/app/models/champs/date_champ.rb +++ b/app/models/champs/date_champ.rb @@ -12,7 +12,9 @@ class Champs::DateChamp < Champ value.presence || "" # old dossiers can have not parseable dates end - alias for_tag to_s + def for_tag(path = :value) + to_s if path == :value + end private diff --git a/app/models/champs/datetime_champ.rb b/app/models/champs/datetime_champ.rb index fe1c2ecd7..93983b5dd 100644 --- a/app/models/champs/datetime_champ.rb +++ b/app/models/champs/datetime_champ.rb @@ -10,7 +10,7 @@ class Champs::DatetimeChamp < Champ value.present? ? I18n.l(Time.zone.parse(value)) : "" end - def for_tag + def for_tag(path = :value) value.present? ? I18n.l(Time.zone.parse(value)) : "" end diff --git a/app/models/champs/decimal_number_champ.rb b/app/models/champs/decimal_number_champ.rb index f6fa77eec..704ec372e 100644 --- a/app/models/champs/decimal_number_champ.rb +++ b/app/models/champs/decimal_number_champ.rb @@ -16,7 +16,7 @@ class Champs::DecimalNumberChamp < Champ } }, if: -> { validate_champ_value? || validation_context == :prefill } - def for_export + def for_export(path = :value) processed_value end diff --git a/app/models/champs/departement_champ.rb b/app/models/champs/departement_champ.rb index c353f1fc3..cde37b697 100644 --- a/app/models/champs/departement_champ.rb +++ b/app/models/champs/departement_champ.rb @@ -5,16 +5,26 @@ class Champs::DepartementChamp < Champs::TextChamp validate :external_id_in_departement_codes, unless: -> { external_id.nil? } before_save :store_code_region - def for_export - [name, code] + def for_export(path = :value) + case path + when :code + code + when :value + name + end end def to_s formatted_value end - def for_tag - formatted_value + def for_tag(path = :value) + case path + when :code + code + when :value + formatted_value + end end def for_api diff --git a/app/models/champs/epci_champ.rb b/app/models/champs/epci_champ.rb index 8d61e762c..0b65e976a 100644 --- a/app/models/champs/epci_champ.rb +++ b/app/models/champs/epci_champ.rb @@ -8,8 +8,26 @@ class Champs::EpciChamp < Champs::TextChamp validate :external_id_in_departement_epci_codes, unless: -> { code_departement.nil? || external_id.nil? } validate :value_in_departement_epci_names, unless: -> { code_departement.nil? || external_id.nil? || value.nil? } - def for_export - [value, code, "#{code_departement} – #{departement_name}"] + def for_export(path = :value) + case path + when :value + value + when :code + code + when :departement + departement_code_and_name + end + end + + def for_tag(path = :value) + case path + when :value + value + when :code + code + when :departement + departement_code_and_name + end end def departement_name @@ -62,6 +80,12 @@ class Champs::EpciChamp < Champs::TextChamp end end + def departement_code_and_name + if departement? + "#{code_departement} – #{departement_name}" + end + end + def code_departement_input_id "#{input_id}-code_departement" end diff --git a/app/models/champs/integer_number_champ.rb b/app/models/champs/integer_number_champ.rb index b913cf9c6..d571fdfbe 100644 --- a/app/models/champs/integer_number_champ.rb +++ b/app/models/champs/integer_number_champ.rb @@ -9,7 +9,7 @@ class Champs::IntegerNumberChamp < Champ } }, if: -> { validate_champ_value? || validation_context == :prefill } - def for_export + def for_export(path = :value) processed_value end diff --git a/app/models/champs/linked_drop_down_list_champ.rb b/app/models/champs/linked_drop_down_list_champ.rb index ee2d3fb6e..6a1edcaef 100644 --- a/app/models/champs/linked_drop_down_list_champ.rb +++ b/app/models/champs/linked_drop_down_list_champ.rb @@ -41,12 +41,26 @@ class Champs::LinkedDropDownListChamp < Champ value.present? ? [primary_value, secondary_value].filter(&:present?).join(' / ') : "" end - def for_tag - value.present? ? [primary_value, secondary_value].filter(&:present?).join(' / ') : "" + def for_tag(path = :value) + case path + when :primary + primary_value + when :secondary + secondary_value + when :value + value.present? ? [primary_value, secondary_value].filter(&:present?).join(' / ') : "" + end end - def for_export - value.present? ? "#{primary_value || ''};#{secondary_value || ''}" : nil + def for_export(path = :value) + case path + when :primary + primary_value + when :secondary + secondary_value + when :value + value.present? ? "#{primary_value || ''};#{secondary_value || ''}" : nil + end end def for_api diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index 1291512b5..a6cc75266 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -24,11 +24,11 @@ class Champs::MultipleDropDownListChamp < Champ selected_options.join(', ') end - def for_tag + def for_tag(path = :value) selected_options.join(', ') end - def for_export + def for_export(path = :value) value.present? ? selected_options.join(', ') : nil end diff --git a/app/models/champs/pays_champ.rb b/app/models/champs/pays_champ.rb index 9318a5ed6..4dc5c0a6d 100644 --- a/app/models/champs/pays_champ.rb +++ b/app/models/champs/pays_champ.rb @@ -11,16 +11,26 @@ class Champs::PaysChamp < Champs::TextChamp validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: false, allow_blank: false end - def for_export - [name, code] + def for_export(path = :value) + case path + when :code + code + when :value + name + end end def to_s name end - def for_tag - name + def for_tag(path = :value) + case path + when :code + code + when :value + name + end end def selected diff --git a/app/models/champs/piece_justificative_champ.rb b/app/models/champs/piece_justificative_champ.rb index f26e2ea06..0708460e2 100644 --- a/app/models/champs/piece_justificative_champ.rb +++ b/app/models/champs/piece_justificative_champ.rb @@ -26,7 +26,7 @@ class Champs::PieceJustificativeChamp < Champ piece_justificative_file.blank? end - def for_export + def for_export(path = :value) piece_justificative_file.map { _1.filename.to_s }.join(', ') end diff --git a/app/models/champs/region_champ.rb b/app/models/champs/region_champ.rb index 1765fdf1c..b37802056 100644 --- a/app/models/champs/region_champ.rb +++ b/app/models/champs/region_champ.rb @@ -3,8 +3,22 @@ class Champs::RegionChamp < Champs::TextChamp validate :value_in_region_names, unless: -> { value.nil? } validate :external_id_in_region_codes, unless: -> { external_id.nil? } - def for_export - [name, code] + def for_export(path = :value) + case path + when :value + name + when :code + code + end + end + + def for_tag(path = :value) + case path + when :value + name + when :code + code + end end def selected diff --git a/app/models/champs/repetition_champ.rb b/app/models/champs/repetition_champ.rb index 2f34e2c45..6bb189c49 100644 --- a/app/models/champs/repetition_champ.rb +++ b/app/models/champs/repetition_champ.rb @@ -1,6 +1,5 @@ class Champs::RepetitionChamp < Champ accepts_nested_attributes_for :champs - delegate :libelle_for_export, to: :type_de_champ def rows dossier @@ -34,7 +33,7 @@ class Champs::RepetitionChamp < Champ # The user cannot enter any information here so it doesn’t make much sense to search end - def for_tag + def for_tag(path = :value) ([libelle] + rows.map do |champs| champs.map do |champ| "#{champ.libelle} : #{champ}" diff --git a/app/models/champs/rna_champ.rb b/app/models/champs/rna_champ.rb index 430526d8a..5e1bbe1c8 100644 --- a/app/models/champs/rna_champ.rb +++ b/app/models/champs/rna_champ.rb @@ -16,7 +16,7 @@ class Champs::RNAChamp < Champ title.present? ? "#{value} (#{title})" : value end - def for_export + def for_export(path = :value) identifier end diff --git a/app/models/champs/rnf_champ.rb b/app/models/champs/rnf_champ.rb index 823c0b92c..cb0de4e8d 100644 --- a/app/models/champs/rnf_champ.rb +++ b/app/models/champs/rnf_champ.rb @@ -25,11 +25,33 @@ class Champs::RNFChamp < Champ rnf_id.blank? end - def for_export - if address.present? - [rnf_id, title, address['label'], address['cityCode'], departement_code_and_name] - else - [rnf_id, nil, nil, nil, nil] + def for_export(path = :value) + case path + when :value + rnf_id + when :departement + departement_code_and_name + when :code_insee + commune&.fetch(:code) + when :address + full_address + when :nom + title + end + end + + def for_tag(path = :value) + case path + when :value + rnf_id + when :departement + departement_code_and_name || '' + when :code_insee + commune&.fetch(:code) || '' + when :address + full_address || '' + when :nom + title || '' end end @@ -59,7 +81,7 @@ class Champs::RNFChamp < Champ def commune_name if departement? - "#{APIGeoService.commune_name(department_code, address['cityCode'])} (#{address['postalCode']})" + "#{APIGeoService.commune_name(code_departement, address['cityCode'])} (#{address['postalCode']})" end end @@ -69,8 +91,8 @@ class Champs::RNFChamp < Champ city_name = address['cityName'] postal_code = address['postalCode'] - commune_name = APIGeoService.commune_name(department_code, city_code) - commune_code = APIGeoService.commune_code(department_code, city_name) + commune_name = APIGeoService.commune_name(code_departement, city_code) + commune_code = APIGeoService.commune_code(code_departement, city_name) if commune_name.present? { diff --git a/app/models/champs/textarea_champ.rb b/app/models/champs/textarea_champ.rb index 289289c57..abcb2644e 100644 --- a/app/models/champs/textarea_champ.rb +++ b/app/models/champs/textarea_champ.rb @@ -1,5 +1,5 @@ class Champs::TextareaChamp < Champs::TextChamp - def for_export + def for_export(path = :value) value.present? ? ActionView::Base.full_sanitizer.sanitize(value) : nil end diff --git a/app/models/champs/titre_identite_champ.rb b/app/models/champs/titre_identite_champ.rb index 3778b295e..ae57491bd 100644 --- a/app/models/champs/titre_identite_champ.rb +++ b/app/models/champs/titre_identite_champ.rb @@ -20,7 +20,7 @@ class Champs::TitreIdentiteChamp < Champ piece_justificative_file.blank? end - def for_export + def for_export(path = :value) piece_justificative_file.attached? ? "présent" : "absent" end diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index d1d4df5df..8bdc51705 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -451,8 +451,8 @@ module TagsSubstitutionConcern def tags_and_datas_list(dossier) [ - [champ_public_tags(dossier:), dossier.champs_public], - [champ_private_tags(dossier:), dossier.champs_private], + [champ_public_tags(dossier:), dossier], + [champ_private_tags(dossier:), dossier], [dossier_tags, dossier], [ROUTAGE_TAGS, dossier], [INDIVIDUAL_TAGS, dossier.individual], diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 1c234d77b..6b5d9c3ca 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1047,14 +1047,8 @@ class Dossier < ApplicationRecord def champs_for_export(types_de_champ, row_id = nil) types_de_champ.flat_map do |type_de_champ| champ = champ_for_export(type_de_champ, row_id) - - # nil => [nil] - # text => [text] - # [commune, insee, departement] => [commune, insee, departement] - wrapped_exported_values = [champ.for_export].flatten - - wrapped_exported_values.map.with_index do |champ_value, index| - [type_de_champ.libelle_for_export(index), champ_value] + type_de_champ.libelles_for_export.map do |(libelle, path)| + [libelle, champ&.for_export(path)] end end end @@ -1184,11 +1178,8 @@ class Dossier < ApplicationRecord def champ_for_export(type_de_champ, row_id) champ = champs_by_public_id[type_de_champ.public_id(row_id)] - if champ.nil? || !champ.visible? - # some champs export multiple columns - # ex: commune.for_export => [commune, insee, departement] - # so we build a fake champ to have the right export - type_de_champ.build_champ(dossier: self, row_id:) + if champ.blank? || !champ.visible? + nil else champ end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 1c76bbaae..b4ff3ac51 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -145,7 +145,7 @@ class TypeDeChamp < ApplicationRecord has_one :revision, through: :revision_type_de_champ has_one :procedure, through: :revision - delegate :estimated_fill_duration, :estimated_read_duration, :tags_for_template, :libelle_for_export, :primary_options, :secondary_options, to: :dynamic_type + delegate :estimated_fill_duration, :estimated_read_duration, :tags_for_template, :libelles_for_export, :libelle_for_export, :primary_options, :secondary_options, to: :dynamic_type delegate :used_by_routing_rules?, to: :revision_type_de_champ class WithIndifferentAccess diff --git a/app/models/types_de_champ/address_type_de_champ.rb b/app/models/types_de_champ/address_type_de_champ.rb index d5b2e614e..710ca5f96 100644 --- a/app/models/types_de_champ/address_type_de_champ.rb +++ b/app/models/types_de_champ/address_type_de_champ.rb @@ -1,21 +1,25 @@ class TypesDeChamp::AddressTypeDeChamp < TypesDeChamp::TextTypeDeChamp - def tags_for_template - tags = super - stable_id = @type_de_champ.stable_id - tags.push( + def libelles_for_export + path = paths.first + [[path[:libelle], path[:path]]] + end + + private + + def paths + paths = super + paths.push( { - libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Département)", - id: "tdc#{stable_id}/departement", - description: "#{description} (Département)", - lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.departement_code_and_name } + libelle: "#{libelle} (Département)", + path: :departement, + description: "#{description} (Département)" }, { - libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Commune)", - id: "tdc#{stable_id}/commune", - description: "#{description} (Commune)", - lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.commune_name } + libelle: "#{libelle} (Commune)", + path: :commune, + description: "#{description} (Commune)" } ) - tags + paths end end diff --git a/app/models/types_de_champ/commune_type_de_champ.rb b/app/models/types_de_champ/commune_type_de_champ.rb index f6983ee85..69be05959 100644 --- a/app/models/types_de_champ/commune_type_de_champ.rb +++ b/app/models/types_de_champ/commune_type_de_champ.rb @@ -1,19 +1,20 @@ class TypesDeChamp::CommuneTypeDeChamp < TypesDeChamp::TypeDeChampBase - def libelle_for_export(index) - [libelle, "#{libelle} (Code insee)", "#{libelle} (Département)"][index] - end + private - def tags_for_template - tags = super - stable_id = @type_de_champ.stable_id - tags.push( - { - libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Département)", - id: "tdc#{stable_id}/departement", - description: "#{description} (Département)", - lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.departement_code_and_name } - } - ) - tags + def paths + paths = super + paths.push({ + libelle: "#{libelle} (Code INSEE)", + description: "#{description} (Code INSEE)", + path: :code, + maybe_null: public? && !mandatory? + }) + paths.push({ + libelle: "#{libelle} (Département)", + description: "#{description} (Département)", + path: :departement, + maybe_null: public? && !mandatory? + }) + paths end end diff --git a/app/models/types_de_champ/departement_type_de_champ.rb b/app/models/types_de_champ/departement_type_de_champ.rb index 8494e14f0..334286f78 100644 --- a/app/models/types_de_champ/departement_type_de_champ.rb +++ b/app/models/types_de_champ/departement_type_de_champ.rb @@ -1,9 +1,18 @@ class TypesDeChamp::DepartementTypeDeChamp < TypesDeChamp::TextTypeDeChamp - def libelle_for_export(index) - [libelle, "#{libelle} (Code)"][index] - end - def filter_to_human(filter_value) APIGeoService.departement_name(filter_value).presence || filter_value end + + private + + def paths + paths = super + paths.push({ + libelle: "#{libelle} (Code)", + description: "#{description} (Code)", + path: :code, + maybe_null: public? && !mandatory? + }) + paths + end end diff --git a/app/models/types_de_champ/epci_type_de_champ.rb b/app/models/types_de_champ/epci_type_de_champ.rb index e4b90e67a..1ef14d66e 100644 --- a/app/models/types_de_champ/epci_type_de_champ.rb +++ b/app/models/types_de_champ/epci_type_de_champ.rb @@ -1,5 +1,20 @@ class TypesDeChamp::EpciTypeDeChamp < TypesDeChamp::TextTypeDeChamp - def libelle_for_export(index) - [libelle, "#{libelle} (Code)", "#{libelle} (Département)"][index] + private + + def paths + paths = super + paths.push({ + libelle: "#{libelle} (Code)", + description: "#{description} (Code)", + path: :code, + maybe_null: public? && !mandatory? + }) + paths.push({ + libelle: "#{libelle} (Département)", + description: "#{description} (Département)", + path: :departement, + maybe_null: public? && !mandatory? + }) + paths end end diff --git a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb index 58ecb6a92..e5b0b56a3 100644 --- a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb @@ -4,30 +4,9 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas delegate :drop_down_list_options, to: :@type_de_champ validate :check_presence_of_primary_options - def tags_for_template - tags = super - stable_id = @type_de_champ.stable_id - tags.push( - { - libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)}/primaire", - id: "tdc#{stable_id}/primaire", - description: "#{description} (menu primaire)", - lambda: -> (champs) { - champs.find { |champ| champ.stable_id == stable_id }&.primary_value - } - } - ) - tags.push( - { - libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)}/secondaire", - id: "tdc#{stable_id}/secondaire", - description: "#{description} (menu secondaire)", - lambda: -> (champs) { - champs.find { |champ| champ.stable_id == stable_id }&.secondary_value - } - } - ) - tags + def libelles_for_export + path = paths.first + [[path[:libelle], path[:path]]] end def add_blank_option_when_not_mandatory(options) @@ -53,6 +32,23 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas private + def paths + paths = super + paths.push({ + libelle: "#{libelle}/primaire", + description: "#{description} (Primaire)", + path: :primary, + maybe_null: public? && !mandatory? + }) + paths.push({ + libelle: "#{libelle}/secondaire", + description: "#{description} (Secondaire)", + path: :secondary, + maybe_null: public? && !mandatory? + }) + paths + end + def unpack_options _, *options = drop_down_list_options chunked = options.slice_before(PRIMARY_PATTERN) diff --git a/app/models/types_de_champ/pays_type_de_champ.rb b/app/models/types_de_champ/pays_type_de_champ.rb index 4067f22d0..43a041e76 100644 --- a/app/models/types_de_champ/pays_type_de_champ.rb +++ b/app/models/types_de_champ/pays_type_de_champ.rb @@ -1,5 +1,14 @@ class TypesDeChamp::PaysTypeDeChamp < TypesDeChamp::TextTypeDeChamp - def libelle_for_export(index) - [libelle, "#{libelle} (Code)"][index] + private + + def paths + paths = super + paths.push({ + libelle: "#{libelle} (Code)", + description: "#{description} (Code)", + path: :code, + maybe_null: public? && !mandatory? + }) + paths end end diff --git a/app/models/types_de_champ/region_type_de_champ.rb b/app/models/types_de_champ/region_type_de_champ.rb index 41e1d04ab..015614aa9 100644 --- a/app/models/types_de_champ/region_type_de_champ.rb +++ b/app/models/types_de_champ/region_type_de_champ.rb @@ -1,9 +1,18 @@ class TypesDeChamp::RegionTypeDeChamp < TypesDeChamp::TextTypeDeChamp - def libelle_for_export(index) - [libelle, "#{libelle} (Code)"][index] - end - def filter_to_human(filter_value) APIGeoService.region_name(filter_value).presence || filter_value end + + private + + def paths + paths = super + paths.push({ + libelle: "#{libelle} (Code)", + description: "#{description} (Code)", + path: :code, + maybe_null: public? && !mandatory? + }) + paths + end end diff --git a/app/models/types_de_champ/repetition_type_de_champ.rb b/app/models/types_de_champ/repetition_type_de_champ.rb index 3580d6193..b6184011d 100644 --- a/app/models/types_de_champ/repetition_type_de_champ.rb +++ b/app/models/types_de_champ/repetition_type_de_champ.rb @@ -12,7 +12,7 @@ class TypesDeChamp::RepetitionTypeDeChamp < TypesDeChamp::TypeDeChampBase end # We have to truncate the label here as spreadsheets have a (30 char) limit on length. - def libelle_for_export(index = 0) + def libelle_for_export str = "(#{stable_id}) #{libelle}" # /\*?[] are invalid Excel worksheet characters ActiveStorage::Filename.new(str.delete('[]*?')).sanitized diff --git a/app/models/types_de_champ/rnf_type_de_champ.rb b/app/models/types_de_champ/rnf_type_de_champ.rb index 05a466f08..d25e02a46 100644 --- a/app/models/types_de_champ/rnf_type_de_champ.rb +++ b/app/models/types_de_champ/rnf_type_de_champ.rb @@ -1,25 +1,32 @@ class TypesDeChamp::RNFTypeDeChamp < TypesDeChamp::TextTypeDeChamp - def libelle_for_export(index) - [libelle, "#{libelle} (Nom)", "#{libelle} (Adresse)", "#{libelle} (Code insee Ville)", "#{libelle} (Département)"][index] - end + private - def tags_for_template - tags = super - stable_id = @type_de_champ.stable_id - tags.push( - { - libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Département)", - id: "tdc#{stable_id}/departement", - description: "#{description} (Département)", - lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.departement_code_and_name } - }, - { - libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Commune)", - id: "tdc#{stable_id}/commune", - description: "#{description} (Commune)", - lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.commune_name } - } - ) - tags + def paths + paths = super + paths.push({ + libelle: "#{libelle} (Nom)", + description: "#{description} (Nom)", + path: :nom, + maybe_null: public? && !mandatory? + }) + paths.push({ + libelle: "#{libelle} (Adresse)", + description: "#{description} (Adresse)", + path: :address, + maybe_null: public? && !mandatory? + }) + paths.push({ + libelle: "#{libelle} (Code INSEE Ville)", + description: "#{description} (Code INSEE Ville)", + path: :code_insee, + maybe_null: public? && !mandatory? + }) + paths.push({ + libelle: "#{libelle} (Département)", + description: "#{description} (Département)", + path: :departement, + maybe_null: public? && !mandatory? + }) + paths end end diff --git a/app/models/types_de_champ/type_de_champ_base.rb b/app/models/types_de_champ/type_de_champ_base.rb index 7efc30f85..db0d38d9b 100644 --- a/app/models/types_de_champ/type_de_champ_base.rb +++ b/app/models/types_de_champ/type_de_champ_base.rb @@ -13,22 +13,18 @@ class TypesDeChamp::TypeDeChampBase end def tags_for_template - stable_id = self.stable_id - [ - { - libelle: TagsSubstitutionConcern::TagsParser.normalize(libelle), - id: "tdc#{stable_id}", - description: description, - maybe_null: public? && !mandatory?, - lambda: -> (champs) { - champs.find { |champ| champ.stable_id == stable_id }&.for_tag - } - } - ] + tdc = @type_de_champ + paths.map do |path| + path.merge( + libelle: TagsSubstitutionConcern::TagsParser.normalize(path[:libelle]), + id: path[:path] == :value ? "tdc#{stable_id}" : "tdc#{stable_id}/#{path[:path]}", + lambda: -> (dossier) { dossier.project_champ(tdc, nil).for_tag(path[:path]) } + ) + end end - def libelle_for_export(index = 0) - libelle + def libelles_for_export + paths.map { [_1[:libelle], _1[:path]] } end # Default estimated duration to fill the champ in a form, in seconds. @@ -59,4 +55,17 @@ class TypesDeChamp::TypeDeChampBase def human_to_filter(human_value) human_value end + + private + + def paths + [ + { + libelle:, + path: :value, + description:, + maybe_null: public? && !mandatory? + } + ] + end end diff --git a/spec/models/champs/commune_champ_spec.rb b/spec/models/champs/commune_champ_spec.rb index e74ba5750..76051c0c3 100644 --- a/spec/models/champs/commune_champ_spec.rb +++ b/spec/models/champs/commune_champ_spec.rb @@ -12,7 +12,9 @@ describe Champs::CommuneChamp do expect(champ.code).to eq(code_insee) expect(champ.code_departement).to eq(code_departement) expect(champ.code_postal).to eq(code_postal) - expect(champ.for_export).to eq(['Châteldon (63290)', '63102', '63 – Puy-de-Dôme']) + expect(champ.for_export(:value)).to eq 'Châteldon (63290)' + expect(champ.for_export(:code)).to eq '63102' + expect(champ.for_export(:departement)).to eq '63 – Puy-de-Dôme' expect(champ.communes.size).to eq(8) end end diff --git a/spec/models/champs/rnf_champ_spec.rb b/spec/models/champs/rnf_champ_spec.rb index 0f58e01bb..510269609 100644 --- a/spec/models/champs/rnf_champ_spec.rb +++ b/spec/models/champs/rnf_champ_spec.rb @@ -86,7 +86,11 @@ describe Champs::RNFChamp, type: :model do describe 'for_export' do let(:champ) { build(:champ_rnf, external_id:, data: JSON.parse(body)) } it do - expect(champ.for_export).to eq(['075-FDD-00003-01', 'Fondation SFR', '16 Rue du Général de Boissieu 75015 Paris', '75115', '75 – Paris']) + expect(champ.for_export(:value)).to eq '075-FDD-00003-01' + expect(champ.for_export(:nom)).to eq 'Fondation SFR' + expect(champ.for_export(:address)).to eq '16 Rue du Général de Boissieu 75015 Paris' + expect(champ.for_export(:code_insee)).to eq '75115' + expect(champ.for_export(:departement)).to eq '75 – Paris' end end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 6e2bd6d80..8a05debe4 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1907,7 +1907,6 @@ describe Dossier, type: :model do let(:dossier_second_revision) { create(:dossier, procedure: procedure) } let(:dossier_champs_for_export) { dossier.champs_for_export(procedure.types_de_champ_for_procedure_presentation.not_repetition) } let(:dossier_second_revision_champs_for_export) { dossier_second_revision.champs_for_export(procedure.types_de_champ_for_procedure_presentation.not_repetition) } - let(:repetition_second_revision_champs_for_export) { dossier.champs_for_export(procedure.types_de_champ_for_procedure_presentation.repetition) } context "when procedure published" do before do @@ -1926,10 +1925,8 @@ describe Dossier, type: :model do it "should have champs from all revisions" do expect(dossier.types_de_champ.map(&:libelle)).to eq([text_type_de_champ.libelle, datetime_type_de_champ.libelle, "Yes/no", explication_type_de_champ.libelle, commune_type_de_champ.libelle, repetition_type_de_champ.libelle]) expect(dossier_second_revision.types_de_champ.map(&:libelle)).to eq([datetime_type_de_champ.libelle, "Updated yes/no", explication_type_de_champ.libelle, 'Commune de naissance', "Repetition", "New text field"]) - expect(dossier_champs_for_export.map { |(libelle)| libelle }).to eq([datetime_type_de_champ.libelle, text_type_de_champ.libelle, "Updated yes/no", "Commune de naissance", "Commune de naissance (Code insee)", "Commune de naissance (Département)", "New text field"]) + expect(dossier_champs_for_export.map { |(libelle)| libelle }).to eq([datetime_type_de_champ.libelle, text_type_de_champ.libelle, "Updated yes/no", "Commune de naissance", "Commune de naissance (Code INSEE)", "Commune de naissance (Département)", "New text field"]) expect(dossier_champs_for_export).to eq(dossier_second_revision_champs_for_export) - expect(repetition_second_revision_champs_for_export.map { |(libelle)| libelle }).to eq(procedure.types_de_champ_for_procedure_presentation.repetition.map(&:libelle_for_export)) - expect(repetition_second_revision_champs_for_export.first.size).to eq(2) end context 'within a repetition having a type de champs commune (multiple values for export)' do @@ -1999,9 +1996,9 @@ describe Dossier, type: :model do [ [yes_no_tdc.libelle, "Oui"], [text_tdc.libelle, "text"], - ["commune", ''], - ["commune (Code insee)", ''], - ["commune (Département)", ""] + ["commune", nil], + ["commune (Code INSEE)", nil], + ["commune (Département)", nil] ] end diff --git a/spec/models/types_de_champ/commune_type_de_champ_spec.rb b/spec/models/types_de_champ/commune_type_de_champ_spec.rb index 8acd652de..c701166fa 100644 --- a/spec/models/types_de_champ/commune_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/commune_type_de_champ_spec.rb @@ -1,6 +1,5 @@ describe TypesDeChamp::CommuneTypeDeChamp do let(:subject) { create(:type_de_champ_communes, libelle: 'Ma commune') } - it { expect(subject.libelle_for_export(0)).to eq('Ma commune') } - it { expect(subject.libelle_for_export(1)).to eq('Ma commune (Code insee)') } + it { expect(subject.libelles_for_export).to match_array([['Ma commune', :value], ['Ma commune (Code INSEE)', :code], ['Ma commune (Département)', :departement]]) } end diff --git a/spec/services/procedure_export_service_spec.rb b/spec/services/procedure_export_service_spec.rb index 5645be1a7..7f783eafd 100644 --- a/spec/services/procedure_export_service_spec.rb +++ b/spec/services/procedure_export_service_spec.rb @@ -71,7 +71,7 @@ describe ProcedureExportService do "multiple_drop_down_list", "linked_drop_down_list", "communes", - "communes (Code insee)", + "communes (Code INSEE)", "communes (Département)", "departements", "departements (Code)", @@ -100,7 +100,7 @@ describe ProcedureExportService do "rnf", "rnf (Nom)", "rnf (Adresse)", - "rnf (Code insee Ville)", + "rnf (Code INSEE Ville)", "rnf (Département)", "engagement_juridique" ] @@ -206,7 +206,7 @@ describe ProcedureExportService do "multiple_drop_down_list", "linked_drop_down_list", "communes", - "communes (Code insee)", + "communes (Code INSEE)", "communes (Département)", "departements", "departements (Code)", @@ -235,7 +235,7 @@ describe ProcedureExportService do "rnf", "rnf (Nom)", "rnf (Adresse)", - "rnf (Code insee Ville)", + "rnf (Code INSEE Ville)", "rnf (Département)", "engagement_juridique" ] @@ -309,7 +309,7 @@ describe ProcedureExportService do "multiple_drop_down_list", "linked_drop_down_list", "communes", - "communes (Code insee)", + "communes (Code INSEE)", "communes (Département)", "departements", "departements (Code)", @@ -338,7 +338,7 @@ describe ProcedureExportService do "rnf", "rnf (Nom)", "rnf (Adresse)", - "rnf (Code insee Ville)", + "rnf (Code INSEE Ville)", "rnf (Département)", "engagement_juridique" ] @@ -433,7 +433,7 @@ describe ProcedureExportService do let(:champ_repetition) { dossiers.first.champs_public.find { |champ| champ.type_champ == 'repetition' } } it 'should have sheets' do - expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', champ_repetition.libelle_for_export]) + expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', champ_repetition.type_de_champ.libelle_for_export]) end context 'with cloned procedure' do @@ -489,7 +489,7 @@ describe ProcedureExportService do let!(:another_champ_repetition) { create(:champ_repetition, type_de_champ: type_de_champ_repetition, dossier: dossier) } it 'should have sheets' do - expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', another_champ_repetition.libelle_for_export, champ_repetition.libelle_for_export]) + expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', another_champ_repetition.type_de_champ.libelle_for_export, champ_repetition.type_de_champ.libelle_for_export]) end end