From 1123222b364fda4b7b3f2d853962fe515a9db69c Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 10 Jan 2024 13:37:55 +0100 Subject: [PATCH] chore(schema): add version to attestation_templates --- .../attestation_template_v2s_controller.rb | 2 +- .../attestation_templates_controller.rb | 4 ++-- .../administrateurs/procedures_controller.rb | 3 ++- app/models/attestation_template.rb | 10 +++++++--- app/models/procedure.rb | 6 +++++- ...40110122422_add_version_to_attestation_templates.rb | 9 +++++++++ db/schema.rb | 3 ++- 7 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20240110122422_add_version_to_attestation_templates.rb diff --git a/app/controllers/administrateurs/attestation_template_v2s_controller.rb b/app/controllers/administrateurs/attestation_template_v2s_controller.rb index 249b7824f..7c96575cc 100644 --- a/app/controllers/administrateurs/attestation_template_v2s_controller.rb +++ b/app/controllers/administrateurs/attestation_template_v2s_controller.rb @@ -76,7 +76,7 @@ module Administrateurs end def retrieve_attestation_template - @attestation_template = @procedure.attestation_template || @procedure.build_attestation_template + @attestation_template = @procedure.attestation_template_v2 || @procedure.build_attestation_template_v2 end def editor_params diff --git a/app/controllers/administrateurs/attestation_templates_controller.rb b/app/controllers/administrateurs/attestation_templates_controller.rb index 27daeac21..f8720f750 100644 --- a/app/controllers/administrateurs/attestation_templates_controller.rb +++ b/app/controllers/administrateurs/attestation_templates_controller.rb @@ -14,7 +14,7 @@ module Administrateurs end def update - @attestation_template = @procedure.attestation_template + @attestation_template = @procedure.attestation_template_v1 if @attestation_template.update(activated_attestation_params) flash.notice = "Le modèle de l’attestation a bien été modifié" @@ -50,7 +50,7 @@ module Administrateurs private def build_attestation_template(attributes = {}) - attestation_template = @procedure.attestation_template || @procedure.build_attestation_template + attestation_template = @procedure.attestation_template_v1 || @procedure.build_attestation_template_v1 attestation_template.attributes = attributes attestation_template end diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index e48fe675d..ae0ab979b 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -111,7 +111,8 @@ module Administrateurs types_de_champ: [], revision_types_de_champ: { type_de_champ: { piece_justificative_template_attachment: :blob } } }, - attestation_template: [], + attestation_template_v1: [], + attestation_template_v2: [], initiated_mail: [], received_mail: [], closed_mail: [], diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index 056587eed..5fde87bd9 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -2,13 +2,14 @@ class AttestationTemplate < ApplicationRecord include ActionView::Helpers::NumberHelper include TagsSubstitutionConcern - belongs_to :procedure, inverse_of: :attestation_template + belongs_to :procedure, inverse_of: :attestation_template_v2 has_one_attached :logo has_one_attached :signature - validates :title, tags: true, if: -> { procedure.present? } - validates :body, tags: true, if: -> { procedure.present? } + validates :title, tags: true, if: -> { procedure.present? && version == 1 } + validates :body, tags: true, if: -> { procedure.present? && version == 1 } + validates :json_body, tags: true, if: -> { procedure.present? && version == 2 } validates :footer, length: { maximum: 190 } FILE_MAX_SIZE = 1.megabytes @@ -17,6 +18,9 @@ class AttestationTemplate < ApplicationRecord DOSSIER_STATE = Dossier.states.fetch(:accepte) + scope :v1, -> { where(version: 1) } + scope :v2, -> { where(version: 2) } + def attestation_for(dossier) attestation = Attestation.new(title: replace_tags(title, dossier, escape: false)) attestation.pdf.attach( diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 3cf8b82f9..f9bd0dfdd 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -47,7 +47,11 @@ class Procedure < ApplicationRecord foreign_key: "replaced_by_procedure_id", dependent: :nullify has_one :module_api_carto, dependent: :destroy - has_one :attestation_template, dependent: :destroy + has_many :attestation_templates, dependent: :destroy + has_one :attestation_template_v1, -> { AttestationTemplate.v1 }, dependent: :destroy, class_name: "AttestationTemplate", inverse_of: :procedure + has_one :attestation_template_v2, -> { AttestationTemplate.v2 }, dependent: :destroy, class_name: "AttestationTemplate", inverse_of: :procedure + + has_one :attestation_template, -> { AttestationTemplate.v1.or(AttestationTemplate.v2) }, dependent: :destroy, inverse_of: :procedure belongs_to :parent_procedure, class_name: 'Procedure', optional: true belongs_to :canonical_procedure, class_name: 'Procedure', optional: true diff --git a/db/migrate/20240110122422_add_version_to_attestation_templates.rb b/db/migrate/20240110122422_add_version_to_attestation_templates.rb new file mode 100644 index 000000000..e3e57bd36 --- /dev/null +++ b/db/migrate/20240110122422_add_version_to_attestation_templates.rb @@ -0,0 +1,9 @@ +class AddVersionToAttestationTemplates < ActiveRecord::Migration[7.0] + disable_ddl_transaction! + + def change + add_column :attestation_templates, :version, :integer, default: 1, null: false + add_index :attestation_templates, [:procedure_id, :version], unique: true, algorithm: :concurrently + remove_index :attestation_templates, :procedure_id, unique: true, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index 4bcc7e8d6..74acdcff9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -156,7 +156,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_01_26_071130) do t.integer "procedure_id" t.text "title" t.datetime "updated_at", precision: nil, null: false - t.index ["procedure_id"], name: "index_attestation_templates_on_procedure_id", unique: true + t.integer "version", default: 1, null: false + t.index ["procedure_id", "version"], name: "index_attestation_templates_on_procedure_id_and_version", unique: true end create_table "attestations", id: :serial, force: :cascade do |t|