First draft repeatable

This commit is contained in:
Damien Le Thiec 2023-01-26 17:57:57 +01:00
parent fc3b7c5ec8
commit 81df033282
4 changed files with 25 additions and 7 deletions

View file

@ -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) }

View file

@ -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)

View file

@ -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,11 +55,15 @@ class PrefillParams
end
def to_h
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

View file

@ -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