From 81df0332828e69209f5a058ae8878dedf64b53ca Mon Sep 17 00:00:00 2001 From: Damien Le Thiec Date: Thu, 26 Jan 2023 17:57:57 +0100 Subject: [PATCH] First draft repeatable --- app/models/champ.rb | 2 ++ .../concerns/dossier_prefillable_concern.rb | 2 +- app/models/prefill_params.rb | 25 +++++++++++++++---- app/models/type_de_champ.rb | 3 ++- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index 21e170978..b53e0e710 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -72,6 +72,8 @@ class Champ < ApplicationRecord :refresh_after_update?, to: :type_de_champ + delegate :revision, to: :dossier, prefix: true + scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) } scope :public_only, -> { where(private: false) } scope :private_only, -> { where(private: true) } diff --git a/app/models/concerns/dossier_prefillable_concern.rb b/app/models/concerns/dossier_prefillable_concern.rb index 245527e6f..3ecbe7a04 100644 --- a/app/models/concerns/dossier_prefillable_concern.rb +++ b/app/models/concerns/dossier_prefillable_concern.rb @@ -7,7 +7,7 @@ module DossierPrefillableConcern return unless champs_public_attributes.any? attr = { prefilled: true } - attr[:champs_public_attributes] = champs_public_attributes.map { |h| h.merge(prefilled: true) } + attr[:champs_public_all_attributes] = champs_public_attributes.map { |h| h.merge(prefilled: true) } assign_attributes(attr) save(validate: false) diff --git a/app/models/prefill_params.rb b/app/models/prefill_params.rb index e4ded1975..68f717df2 100644 --- a/app/models/prefill_params.rb +++ b/app/models/prefill_params.rb @@ -5,7 +5,7 @@ class PrefillParams end def to_a - build_prefill_values.filter(&:prefillable?).map(&:to_h) + build_prefill_values.filter(&:prefillable?).map(&:to_h).flatten end private @@ -55,10 +55,14 @@ class PrefillParams end def to_h - { - id: champ.id, - value: value - } + if champ.type_champ == TypeDeChamp.type_champs.fetch(:repetition) + repeatable_hashes + else + { + id: champ.id, + value: value + } + end end private @@ -69,5 +73,16 @@ class PrefillParams champ.value = value champ.valid?(:prefill) end + + def repeatable_hashes + value.map.with_index do |repetition, index| + row = champ.rows[index] || champ.add_row(champ.dossier_revision) + JSON.parse(repetition).map do |key, value| + id = row.find { |champ| champ.libelle == key }.id + { id: id, value: value } + end + rescue JSON::ParserError + end.flatten + end end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 4c1a7cb71..16bfb5cbf 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -269,7 +269,8 @@ class TypeDeChamp < ApplicationRecord TypeDeChamp.type_champs.fetch(:yes_no), TypeDeChamp.type_champs.fetch(:checkbox), TypeDeChamp.type_champs.fetch(:drop_down_list), - TypeDeChamp.type_champs.fetch(:regions) + TypeDeChamp.type_champs.fetch(:regions), + TypeDeChamp.type_champs.fetch(:repetition), ]) end