POST working with prefill query example for repeating fields
This commit is contained in:
parent
81df033282
commit
20ba96ba3c
10 changed files with 76 additions and 10 deletions
|
@ -2,6 +2,7 @@ class API::Public::V1::DossiersController < API::Public::V1::BaseController
|
||||||
before_action :retrieve_procedure
|
before_action :retrieve_procedure
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
byebug
|
||||||
dossier = Dossier.new(
|
dossier = Dossier.new(
|
||||||
revision: @procedure.active_revision,
|
revision: @procedure.active_revision,
|
||||||
groupe_instructeur: @procedure.defaut_groupe_instructeur_for_new_dossier,
|
groupe_instructeur: @procedure.defaut_groupe_instructeur_for_new_dossier,
|
||||||
|
|
|
@ -7,7 +7,7 @@ module DossierPrefillableConcern
|
||||||
return unless champs_public_attributes.any?
|
return unless champs_public_attributes.any?
|
||||||
|
|
||||||
attr = { prefilled: true }
|
attr = { prefilled: true }
|
||||||
attr[:champs_public_all_attributes] = champs_public_attributes.map { |h| h.merge(prefilled: true) }
|
attr[:champs_public_all_attributes] = champs_public_attributes.compact.map { |h| h.merge(prefilled: true) }
|
||||||
|
|
||||||
assign_attributes(attr)
|
assign_attributes(attr)
|
||||||
save(validate: false)
|
save(validate: false)
|
||||||
|
|
|
@ -50,7 +50,13 @@ class PrefillDescription < SimpleDelegator
|
||||||
end
|
end
|
||||||
|
|
||||||
def prefilled_champs_for_query
|
def prefilled_champs_for_query
|
||||||
prefilled_champs.map { |type_de_champ| "\"champ_#{type_de_champ.to_typed_id}\": \"#{type_de_champ.example_value}\"" } .join(', ')
|
prefilled_champs.map do |type_de_champ|
|
||||||
|
if type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:repetition)
|
||||||
|
"\"champ_#{type_de_champ.to_typed_id}\": #{type_de_champ.example_value}"
|
||||||
|
else
|
||||||
|
"\"champ_#{type_de_champ.to_typed_id}\": \"#{type_de_champ.example_value}\""
|
||||||
|
end
|
||||||
|
end.join(', ')
|
||||||
end
|
end
|
||||||
|
|
||||||
def active_fillable_public_types_de_champ
|
def active_fillable_public_types_de_champ
|
||||||
|
|
|
@ -11,6 +11,9 @@ class PrefillParams
|
||||||
private
|
private
|
||||||
|
|
||||||
def build_prefill_values
|
def build_prefill_values
|
||||||
|
byebug
|
||||||
|
# nop : "[{\"txt\":\"Texte court\", \"nb\":\"3.14\", \"Test dropdown\":\"Premier choix\", \"régio\":\"53\"}, {\"txt\":\"Texte court\", \"nb\":\"3.14\", \"Test dropdown\":\"Premier choix\", \"régio\":\"53\"}]"
|
||||||
|
# {"champ_Q2hhbXAtNDI="=>["{\"txt\":\"abc\", \"nb\":\"1,12\"}", "{\"txt\":\"def\", \"nb\":\"2,12\"}"]}
|
||||||
value_by_stable_id = @params
|
value_by_stable_id = @params
|
||||||
.map { |prefixed_typed_id, value| [stable_id_from_typed_id(prefixed_typed_id), value] }
|
.map { |prefixed_typed_id, value| [stable_id_from_typed_id(prefixed_typed_id), value] }
|
||||||
.filter { |stable_id, value| stable_id.present? && value.present? }
|
.filter { |stable_id, value| stable_id.present? && value.present? }
|
||||||
|
@ -78,11 +81,12 @@ class PrefillParams
|
||||||
value.map.with_index do |repetition, index|
|
value.map.with_index do |repetition, index|
|
||||||
row = champ.rows[index] || champ.add_row(champ.dossier_revision)
|
row = champ.rows[index] || champ.add_row(champ.dossier_revision)
|
||||||
JSON.parse(repetition).map do |key, value|
|
JSON.parse(repetition).map do |key, value|
|
||||||
id = row.find { |champ| champ.libelle == key }.id
|
id = row.find { |champ| champ.libelle == key }&.id
|
||||||
|
next unless id
|
||||||
{ id: id, value: value }
|
{ id: id, value: value }
|
||||||
end
|
end
|
||||||
rescue JSON::ParserError
|
rescue JSON::ParserError
|
||||||
end.flatten
|
end.compact
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -128,6 +128,7 @@ class TypeDeChamp < ApplicationRecord
|
||||||
has_one :procedure, through: :revision
|
has_one :procedure, through: :revision
|
||||||
|
|
||||||
delegate :estimated_fill_duration, :estimated_read_duration, :tags_for_template, :libelle_for_export, to: :dynamic_type
|
delegate :estimated_fill_duration, :estimated_read_duration, :tags_for_template, :libelle_for_export, to: :dynamic_type
|
||||||
|
delegate :active_revision, to: :procedure, prefix: true
|
||||||
|
|
||||||
class WithIndifferentAccess
|
class WithIndifferentAccess
|
||||||
def self.load(options)
|
def self.load(options)
|
||||||
|
@ -486,6 +487,12 @@ class TypeDeChamp < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def active_revision_type_de_champ
|
||||||
|
procedure_active_revision.revision_types_de_champ_public.find do |rtc|
|
||||||
|
rtc.type_de_champ_id == id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
DEFAULT_EMPTY = ['']
|
DEFAULT_EMPTY = ['']
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
|
||||||
|
include ActionView::Helpers::UrlHelper
|
||||||
|
|
||||||
|
def possible_values
|
||||||
|
prefillable_subchamps.map do |prefill_type_de_champ|
|
||||||
|
if prefill_type_de_champ.too_many_possible_values?
|
||||||
|
link = link_to "Voir toutes les valeurs possibles", Rails.application.routes.url_helpers.prefill_type_de_champ_path("piece-jointe", self)
|
||||||
|
"#{prefill_type_de_champ.libelle}: #{link}"
|
||||||
|
else
|
||||||
|
"#{prefill_type_de_champ.libelle}: #{prefill_type_de_champ.possible_values_sentence}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def possible_values_sentence
|
||||||
|
"#{I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html")}<br>#{possible_values.join("<br>")}".html_safe
|
||||||
|
end
|
||||||
|
|
||||||
|
def example_value
|
||||||
|
[row_values_format, row_values_format].map { |row| row.to_s.gsub("=>", ":") }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def row_values_format
|
||||||
|
@row_example_value ||=
|
||||||
|
prefillable_subchamps.map do |prefill_type_de_champ|
|
||||||
|
[prefill_type_de_champ.libelle, prefill_type_de_champ.example_value.to_s]
|
||||||
|
end.to_h
|
||||||
|
end
|
||||||
|
|
||||||
|
def prefillable_subchamps
|
||||||
|
return [] unless active_revision_type_de_champ
|
||||||
|
|
||||||
|
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision_type_de_champ.revision_types_de_champ.map(&:type_de_champ).filter(&:prefillable?))
|
||||||
|
end
|
||||||
|
end
|
|
@ -9,6 +9,8 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
|
||||||
TypesDeChamp::PrefillPaysTypeDeChamp.new(type_de_champ)
|
TypesDeChamp::PrefillPaysTypeDeChamp.new(type_de_champ)
|
||||||
when TypeDeChamp.type_champs.fetch(:regions)
|
when TypeDeChamp.type_champs.fetch(:regions)
|
||||||
TypesDeChamp::PrefillRegionTypeDeChamp.new(type_de_champ)
|
TypesDeChamp::PrefillRegionTypeDeChamp.new(type_de_champ)
|
||||||
|
when TypeDeChamp.type_champs.fetch(:repetition)
|
||||||
|
TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ)
|
||||||
else
|
else
|
||||||
new(type_de_champ)
|
new(type_de_champ)
|
||||||
end
|
end
|
||||||
|
@ -19,7 +21,9 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
|
||||||
end
|
end
|
||||||
|
|
||||||
def possible_values
|
def possible_values
|
||||||
[]
|
return [] unless prefillable?
|
||||||
|
|
||||||
|
[I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html")]
|
||||||
end
|
end
|
||||||
|
|
||||||
def example_value
|
def example_value
|
||||||
|
@ -31,4 +35,12 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
|
||||||
def too_many_possible_values?
|
def too_many_possible_values?
|
||||||
possible_values.count > POSSIBLE_VALUES_THRESHOLD
|
possible_values.count > POSSIBLE_VALUES_THRESHOLD
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def possible_values_sentence
|
||||||
|
if too_many_possible_values?
|
||||||
|
I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html").html_safe
|
||||||
|
else
|
||||||
|
possible_values.to_sentence
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -39,13 +39,10 @@
|
||||||
%th
|
%th
|
||||||
= t("views.prefill_descriptions.edit.possible_values.title")
|
= t("views.prefill_descriptions.edit.possible_values.title")
|
||||||
%td
|
%td
|
||||||
- if I18n.exists?("views.prefill_descriptions.edit.possible_values.#{type_de_champ.type_champ}_html")
|
= type_de_champ.possible_values_sentence
|
||||||
= t("views.prefill_descriptions.edit.possible_values.#{type_de_champ.type_champ}_html")
|
|
||||||
%br
|
%br
|
||||||
- if type_de_champ.too_many_possible_values?
|
- if type_de_champ.too_many_possible_values?
|
||||||
= link_to "Voir toutes les valeurs possibles", prefill_type_de_champ_path(prefill_description.path, type_de_champ)
|
= link_to "Voir toutes les valeurs possibles", prefill_type_de_champ_path(prefill_description.path, type_de_champ)
|
||||||
- else
|
|
||||||
= type_de_champ.possible_values.to_sentence
|
|
||||||
%tr{ class: prefillable ? "" : "fr-text-mention--grey" }
|
%tr{ class: prefillable ? "" : "fr-text-mention--grey" }
|
||||||
%th
|
%th
|
||||||
= t("views.prefill_descriptions.edit.examples.title")
|
= t("views.prefill_descriptions.edit.examples.title")
|
||||||
|
|
|
@ -128,6 +128,7 @@ en:
|
||||||
date_html: ISO8601 date
|
date_html: ISO8601 date
|
||||||
datetime_html: ISO8601 datetime
|
datetime_html: ISO8601 datetime
|
||||||
drop_down_list_other_html: Any value
|
drop_down_list_other_html: Any value
|
||||||
|
repetition_html: A array of hashes with possible values for each field of the repetition.
|
||||||
examples:
|
examples:
|
||||||
title: Example
|
title: Example
|
||||||
text: Short text
|
text: Short text
|
||||||
|
|
|
@ -119,6 +119,7 @@ fr:
|
||||||
datetime_html: Datetime au format ISO8601
|
datetime_html: Datetime au format ISO8601
|
||||||
date_html: Date au format ISO8601
|
date_html: Date au format ISO8601
|
||||||
drop_down_list_other_html: Toute valeur
|
drop_down_list_other_html: Toute valeur
|
||||||
|
repetition_html: Un array de hash avec les valeurs possibles pour chaque champ de la répétition.
|
||||||
examples:
|
examples:
|
||||||
title: Exemple
|
title: Exemple
|
||||||
text: Texte court
|
text: Texte court
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue