refactor(type_de_champ): use children_of when serializing type_de_champ

This commit is contained in:
Paul Chavard 2022-05-20 12:27:26 +02:00
parent edd68b1a3e
commit 50aa50b047
4 changed files with 46 additions and 55 deletions

View file

@ -41,31 +41,7 @@ module Administrateurs
private private
def serialize_type_de_champ(type_de_champ) def serialize_type_de_champ(type_de_champ)
{ { type_de_champ: type_de_champ.as_json_for_editor }
type_de_champ: type_de_champ.as_json(
except: [
:created_at,
:options,
:order_place,
:parent_id,
:private,
:procedure_id,
:revision_id,
:stable_id,
:type,
:updated_at
],
methods: [
:drop_down_list_value,
:drop_down_other,
:drop_down_secondary_libelle,
:drop_down_secondary_description,
:piece_justificative_template_filename,
:piece_justificative_template_url,
:editable_options
]
)
}
end end
def type_de_champ_create_params def type_de_champ_create_params

View file

@ -34,7 +34,7 @@ module ProcedureHelper
{ {
isAnnotation: false, isAnnotation: false,
typeDeChampsTypes: TypeDeChamp.type_de_champ_types_for(procedure, current_user), typeDeChampsTypes: TypeDeChamp.type_de_champ_types_for(procedure, current_user),
typeDeChamps: (procedure.draft_revision ? procedure.draft_revision : procedure).types_de_champ_public.as_json_for_editor, typeDeChamps: procedure.draft_revision.types_de_champ_public_as_json,
baseUrl: admin_procedure_types_de_champ_path(procedure), baseUrl: admin_procedure_types_de_champ_path(procedure),
directUploadUrl: rails_direct_uploads_url, directUploadUrl: rails_direct_uploads_url,
continuerUrl: admin_procedure_path(procedure) continuerUrl: admin_procedure_path(procedure)
@ -45,7 +45,7 @@ module ProcedureHelper
{ {
isAnnotation: true, isAnnotation: true,
typeDeChampsTypes: TypeDeChamp.type_de_champ_types_for(procedure, current_user), typeDeChampsTypes: TypeDeChamp.type_de_champ_types_for(procedure, current_user),
typeDeChamps: (procedure.draft_revision ? procedure.draft_revision : procedure).types_de_champ_private.as_json_for_editor, typeDeChamps: procedure.draft_revision.types_de_champ_private_as_json,
baseUrl: admin_procedure_types_de_champ_path(procedure), baseUrl: admin_procedure_types_de_champ_path(procedure),
directUploadUrl: rails_direct_uploads_url, directUploadUrl: rails_direct_uploads_url,
continuerUrl: admin_procedure_path(procedure) continuerUrl: admin_procedure_path(procedure)

View file

@ -167,8 +167,29 @@ class ProcedureRevision < ApplicationRecord
.order("procedure_revision_types_de_champ.position") .order("procedure_revision_types_de_champ.position")
end end
def types_de_champ_public_as_json
types_de_champ = types_de_champ_public.includes(piece_justificative_template_attachment: :blob)
tdcs_as_json = types_de_champ.map(&:as_json_for_editor)
children_types_de_champ_as_json(tdcs_as_json, types_de_champ.filter(&:repetition?))
tdcs_as_json
end
def types_de_champ_private_as_json
types_de_champ = types_de_champ_private.includes(piece_justificative_template_attachment: :blob)
tdcs_as_json = types_de_champ.map(&:as_json_for_editor)
children_types_de_champ_as_json(tdcs_as_json, types_de_champ.filter(&:repetition?))
tdcs_as_json
end
private private
def children_types_de_champ_as_json(tdcs_as_json, parent_tdcs)
parent_tdcs.each do |parent_tdc|
tdc_as_json = tdcs_as_json.find { |json| json["id"] == parent_tdc.stable_id }
tdc_as_json&.merge!(types_de_champ: children_of(parent_tdc).includes(piece_justificative_template_attachment: :blob).map(&:as_json_for_editor))
end
end
def coordinate_and_tdc(stable_id) def coordinate_and_tdc(stable_id)
coordinate = revision_types_de_champ coordinate = revision_types_de_champ
.joins(:type_de_champ) .joins(:type_de_champ)

View file

@ -356,15 +356,14 @@ class TypeDeChamp < ApplicationRecord
.sort_by(&:first) .sort_by(&:first)
end end
TYPES_DE_CHAMP_BASE = { def as_json_for_editor
as_json(
except: [ except: [
:created_at, :created_at,
:options, :options,
:order_place, :order_place,
:parent_id, :parent_id,
:private, :private,
:procedure_id,
:revision_id,
:stable_id, :stable_id,
:type, :type,
:updated_at :updated_at
@ -372,18 +371,13 @@ class TypeDeChamp < ApplicationRecord
methods: [ methods: [
:drop_down_list_value, :drop_down_list_value,
:drop_down_other, :drop_down_other,
:drop_down_secondary_libelle,
:drop_down_secondary_description,
:piece_justificative_template_filename, :piece_justificative_template_filename,
:piece_justificative_template_url, :piece_justificative_template_url,
:editable_options, :editable_options
:drop_down_secondary_libelle,
:drop_down_secondary_description
] ]
} )
TYPES_DE_CHAMP = TYPES_DE_CHAMP_BASE
.merge(include: { types_de_champ: TYPES_DE_CHAMP_BASE })
def self.as_json_for_editor
includes(piece_justificative_template_attachment: :blob, types_de_champ: [piece_justificative_template_attachment: :blob]).as_json(TYPES_DE_CHAMP)
end end
def read_attribute_for_serialization(name) def read_attribute_for_serialization(name)