Merge pull request #7363 from tchak/refactor-type_de_champ-as-json

refactor(type_de_champ): use children_of when serializing type_de_champ
This commit is contained in:
Paul Chavard 2022-05-23 18:27:06 +02:00 committed by GitHub
commit bcc08507af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 55 deletions

View file

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

View file

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

View file

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

View file

@ -356,15 +356,14 @@ class TypeDeChamp < ApplicationRecord
.sort_by(&:first)
end
TYPES_DE_CHAMP_BASE = {
def as_json_for_editor
as_json(
except: [
:created_at,
:options,
:order_place,
:parent_id,
:private,
:procedure_id,
:revision_id,
:stable_id,
:type,
:updated_at
@ -372,18 +371,13 @@ class TypeDeChamp < ApplicationRecord
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,
:drop_down_secondary_libelle,
:drop_down_secondary_description
:editable_options
]
}
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
def read_attribute_for_serialization(name)