From b97cbd34025e26884c410d44d2ec897a2d90c2c4 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 11 Sep 2019 16:04:42 +0200 Subject: [PATCH] Serialize champ repetition for tags closes #4272 --- app/models/champ.rb | 4 +++ app/models/champs/date_champ.rb | 4 +++ app/models/champs/datetime_champ.rb | 4 +++ .../champs/linked_drop_down_list_champ.rb | 4 +++ .../champs/multiple_drop_down_list_champ.rb | 4 +++ app/models/champs/repetition_champ.rb | 8 ++++++ app/models/champs/yes_no_champ.rb | 4 +++ .../types_de_champ/type_de_champ_base.rb | 2 +- .../concern/tags_substitution_concern_spec.rb | 27 +++++++++++++++++++ 9 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index 4c091e04f..b67164328 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -54,6 +54,10 @@ class Champ < ApplicationRecord value end + def for_tag + value.present? ? value.to_s : '' + end + def main_value_name :value end diff --git a/app/models/champs/date_champ.rb b/app/models/champs/date_champ.rb index 0fd0a2499..309091603 100644 --- a/app/models/champs/date_champ.rb +++ b/app/models/champs/date_champ.rb @@ -9,6 +9,10 @@ class Champs::DateChamp < Champ value.present? ? I18n.l(Date.parse(value)) : "" end + def for_tag + value.present? ? I18n.l(Date.parse(value)) : "" + end + private def format_before_save diff --git a/app/models/champs/datetime_champ.rb b/app/models/champs/datetime_champ.rb index c98377d15..84008894b 100644 --- a/app/models/champs/datetime_champ.rb +++ b/app/models/champs/datetime_champ.rb @@ -9,6 +9,10 @@ class Champs::DatetimeChamp < Champ value.present? ? I18n.l(Time.zone.parse(value)) : "" end + def for_tag + value.present? ? I18n.l(Time.zone.parse(value)) : "" + end + private def format_before_save diff --git a/app/models/champs/linked_drop_down_list_champ.rb b/app/models/champs/linked_drop_down_list_champ.rb index 52e5386cf..46011439c 100644 --- a/app/models/champs/linked_drop_down_list_champ.rb +++ b/app/models/champs/linked_drop_down_list_champ.rb @@ -33,6 +33,10 @@ 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(' / ') : "" + end + def for_export value.present? ? "#{primary_value || ''};#{secondary_value || ''}" : nil end diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index ef72e07b4..ff120ac23 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -13,6 +13,10 @@ class Champs::MultipleDropDownListChamp < Champ selected_options.join(', ') end + def for_tag + selected_options.join(', ') + end + def for_export value.present? ? selected_options.join(', ') : nil end diff --git a/app/models/champs/repetition_champ.rb b/app/models/champs/repetition_champ.rb index 4d95e574d..9296dbdca 100644 --- a/app/models/champs/repetition_champ.rb +++ b/app/models/champs/repetition_champ.rb @@ -19,6 +19,14 @@ class Champs::RepetitionChamp < Champ # The user cannot enter any information here so it doesn’t make much sense to search end + def for_tag + ([libelle] + rows.map do |champs| + champs.map do |champ| + "#{champ.libelle} : #{champ}" + end.join("\n") + end).join("\n\n") + end + def rows_for_export rows.each.with_index(1).map do |champs, index| Champs::RepetitionChamp::Row.new(index: index, dossier_id: dossier_id.to_s, champs: champs) diff --git a/app/models/champs/yes_no_champ.rb b/app/models/champs/yes_no_champ.rb index 4f82a406b..6ba7534b7 100644 --- a/app/models/champs/yes_no_champ.rb +++ b/app/models/champs/yes_no_champ.rb @@ -9,6 +9,10 @@ class Champs::YesNoChamp < Champ processed_value end + def for_tag + processed_value + end + def for_export processed_value 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 650f600d5..7c25fc5bf 100644 --- a/app/models/types_de_champ/type_de_champ_base.rb +++ b/app/models/types_de_champ/type_de_champ_base.rb @@ -14,7 +14,7 @@ class TypesDeChamp::TypeDeChampBase libelle: libelle, description: description, lambda: -> (champs) { - champs.find { |champ| champ.type_de_champ == tdc } + champs.find { |champ| champ.type_de_champ == tdc }&.for_tag } } ] diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index a2d3b1443..e3c379e92 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -114,6 +114,33 @@ describe TagsSubstitutionConcern, type: :model do end end + context 'when the procedure has a type de champ repetition' do + let(:template) { '--Répétition--' } + let(:types_de_champ) do + [ + create(:type_de_champ_repetition, libelle: 'Répétition', types_de_champ: [ + create(:type_de_champ_text, libelle: 'Nom'), + create(:type_de_champ_text, libelle: 'Prénom') + ]) + ] + end + + before do + repetition = dossier.champs + .find { |champ| champ.libelle == 'Répétition' } + repetition.add_row(1) + paul_champs, pierre_champs = repetition.rows + + paul_champs.first.update(value: 'Paul') + paul_champs.last.update(value: 'Chavard') + + pierre_champs.first.update(value: 'Pierre') + pierre_champs.last.update(value: 'de La Morinerie') + end + + it { is_expected.to eq("Répétition\n\nNom : Paul\nPrénom : Chavard\n\nNom : Pierre\nPrénom : de La Morinerie") } + end + context 'when the procedure has a linked drop down menus type de champ' do let(:type_de_champ) do create(:type_de_champ_linked_drop_down_list, libelle: 'libelle')