chore(schema): add version to attestation_templates

This commit is contained in:
Colin Darie 2024-01-10 13:37:55 +01:00
parent dfa07c1c5d
commit 1123222b36
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
7 changed files with 28 additions and 9 deletions

View file

@ -76,7 +76,7 @@ module Administrateurs
end end
def retrieve_attestation_template 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 end
def editor_params def editor_params

View file

@ -14,7 +14,7 @@ module Administrateurs
end end
def update def update
@attestation_template = @procedure.attestation_template @attestation_template = @procedure.attestation_template_v1
if @attestation_template.update(activated_attestation_params) if @attestation_template.update(activated_attestation_params)
flash.notice = "Le modèle de lattestation a bien été modifié" flash.notice = "Le modèle de lattestation a bien été modifié"
@ -50,7 +50,7 @@ module Administrateurs
private private
def build_attestation_template(attributes = {}) 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.attributes = attributes
attestation_template attestation_template
end end

View file

@ -111,7 +111,8 @@ module Administrateurs
types_de_champ: [], types_de_champ: [],
revision_types_de_champ: { type_de_champ: { piece_justificative_template_attachment: :blob } } revision_types_de_champ: { type_de_champ: { piece_justificative_template_attachment: :blob } }
}, },
attestation_template: [], attestation_template_v1: [],
attestation_template_v2: [],
initiated_mail: [], initiated_mail: [],
received_mail: [], received_mail: [],
closed_mail: [], closed_mail: [],

View file

@ -2,13 +2,14 @@ class AttestationTemplate < ApplicationRecord
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
include TagsSubstitutionConcern include TagsSubstitutionConcern
belongs_to :procedure, inverse_of: :attestation_template belongs_to :procedure, inverse_of: :attestation_template_v2
has_one_attached :logo has_one_attached :logo
has_one_attached :signature has_one_attached :signature
validates :title, tags: true, if: -> { procedure.present? } validates :title, tags: true, if: -> { procedure.present? && version == 1 }
validates :body, tags: true, if: -> { procedure.present? } validates :body, tags: true, if: -> { procedure.present? && version == 1 }
validates :json_body, tags: true, if: -> { procedure.present? && version == 2 }
validates :footer, length: { maximum: 190 } validates :footer, length: { maximum: 190 }
FILE_MAX_SIZE = 1.megabytes FILE_MAX_SIZE = 1.megabytes
@ -17,6 +18,9 @@ class AttestationTemplate < ApplicationRecord
DOSSIER_STATE = Dossier.states.fetch(:accepte) DOSSIER_STATE = Dossier.states.fetch(:accepte)
scope :v1, -> { where(version: 1) }
scope :v2, -> { where(version: 2) }
def attestation_for(dossier) def attestation_for(dossier)
attestation = Attestation.new(title: replace_tags(title, dossier, escape: false)) attestation = Attestation.new(title: replace_tags(title, dossier, escape: false))
attestation.pdf.attach( attestation.pdf.attach(

View file

@ -47,7 +47,11 @@ class Procedure < ApplicationRecord
foreign_key: "replaced_by_procedure_id", dependent: :nullify foreign_key: "replaced_by_procedure_id", dependent: :nullify
has_one :module_api_carto, dependent: :destroy 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 :parent_procedure, class_name: 'Procedure', optional: true
belongs_to :canonical_procedure, class_name: 'Procedure', optional: true belongs_to :canonical_procedure, class_name: 'Procedure', optional: true

View file

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

View file

@ -156,7 +156,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_01_26_071130) do
t.integer "procedure_id" t.integer "procedure_id"
t.text "title" t.text "title"
t.datetime "updated_at", precision: nil, null: false 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 end
create_table "attestations", id: :serial, force: :cascade do |t| create_table "attestations", id: :serial, force: :cascade do |t|