From 50aa50b0474c16b0e7b80239a02b9d473454bc92 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 20 May 2022 12:27:26 +0200 Subject: [PATCH] refactor(type_de_champ): use children_of when serializing type_de_champ --- .../types_de_champ_controller.rb | 26 +--------- app/helpers/procedure_helper.rb | 4 +- app/models/procedure_revision.rb | 21 ++++++++ app/models/type_de_champ.rb | 50 ++++++++----------- 4 files changed, 46 insertions(+), 55 deletions(-) diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index 499164d5e..9a9d10d8a 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -41,31 +41,7 @@ module Administrateurs private def serialize_type_de_champ(type_de_champ) - { - 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 - ] - ) - } + { type_de_champ: type_de_champ.as_json_for_editor } end def type_de_champ_create_params diff --git a/app/helpers/procedure_helper.rb b/app/helpers/procedure_helper.rb index 684299b30..6d40ebcd2 100644 --- a/app/helpers/procedure_helper.rb +++ b/app/helpers/procedure_helper.rb @@ -34,7 +34,7 @@ module ProcedureHelper { isAnnotation: false, 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), directUploadUrl: rails_direct_uploads_url, continuerUrl: admin_procedure_path(procedure) @@ -45,7 +45,7 @@ module ProcedureHelper { isAnnotation: true, 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), directUploadUrl: rails_direct_uploads_url, continuerUrl: admin_procedure_path(procedure) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index c14c7a93f..a95add156 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -167,8 +167,29 @@ class ProcedureRevision < ApplicationRecord .order("procedure_revision_types_de_champ.position") 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 + 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) coordinate = revision_types_de_champ .joins(:type_de_champ) diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 37cf3f14f..d85808e25 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -356,34 +356,28 @@ class TypeDeChamp < ApplicationRecord .sort_by(&:first) end - TYPES_DE_CHAMP_BASE = { - 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, - :piece_justificative_template_filename, - :piece_justificative_template_url, - :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) + def as_json_for_editor + as_json( + except: [ + :created_at, + :options, + :order_place, + :parent_id, + :private, + :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 def read_attribute_for_serialization(name)