refactor(attestation_template): cleanup relationships

This commit is contained in:
Paul Chavard 2022-02-11 08:45:16 +01:00
parent 88e80e9e32
commit e269077c40
10 changed files with 36 additions and 33 deletions

View file

@ -32,7 +32,7 @@ module Instructeurs
end
def apercu_attestation
@attestation = dossier.procedure.attestation_template.render_attributes_for(dossier: dossier)
@attestation = dossier.attestation_template.render_attributes_for(dossier: dossier)
render 'administrateurs/attestation_templates/show', formats: [:pdf]
end

View file

@ -15,7 +15,6 @@ class AttestationTemplate < ApplicationRecord
include ActionView::Helpers::NumberHelper
include TagsSubstitutionConcern
belongs_to :procedure, optional: true
has_many :revisions, class_name: 'ProcedureRevision', inverse_of: :attestation_template, dependent: :nullify
has_one_attached :logo
@ -120,7 +119,7 @@ class AttestationTemplate < ApplicationRecord
end
def procedure
revisions.last&.procedure || super
revisions.last&.procedure
end
private

View file

@ -137,6 +137,7 @@ class Dossier < ApplicationRecord
belongs_to :user, optional: true
has_one :france_connect_information, through: :user
has_one :attestation_template, through: :revision
has_one :procedure, through: :revision
has_many :types_de_champ, through: :revision
has_many :types_de_champ_private, through: :revision
@ -384,7 +385,7 @@ class Dossier < ApplicationRecord
.where.not(user: users_who_submitted)
end
scope :for_api_v2, -> { includes(procedure: [:administrateurs, :attestation_template], etablissement: [], individual: [], traitement: []) }
scope :for_api_v2, -> { includes(procedure: [:administrateurs], revision: [:attestation_template], etablissement: [], individual: [], traitement: []) }
scope :with_notifications, -> do
joins(:follows)
@ -721,10 +722,6 @@ class Dossier < ApplicationRecord
{ lon: lon, lat: lat, zoom: zoom }
end
def attestation_template
revision.attestation_template
end
def unspecified_attestation_champs
if attestation_template&.activated?
attestation_template.unspecified_champs_for_dossier(self)
@ -828,7 +825,7 @@ class Dossier < ApplicationRecord
end
def attestation_activated?
termine? && procedure.attestation_template&.activated?
termine? && attestation_template&.activated?
end
def after_passer_en_construction

View file

@ -84,7 +84,7 @@ class Procedure < ApplicationRecord
has_many :experts, through: :experts_procedures
has_one :module_api_carto, dependent: :destroy
has_one :attestation_template, dependent: :destroy
has_one :legacy_attestation_template, class_name: 'AttestationTemplate', dependent: :destroy
has_many :attestation_templates, through: :revisions, source: :attestation_template
belongs_to :parent_procedure, class_name: 'Procedure', optional: true
@ -434,7 +434,6 @@ class Procedure < ApplicationRecord
populate_champ_stable_ids
include_list = {
attestation_template: [],
draft_revision: {
revision_types_de_champ: {
type_de_champ: :types_de_champ
@ -580,7 +579,7 @@ class Procedure < ApplicationRecord
touch(:whitelisted_at)
end
def active_attestation_template
def attestation_template
published_attestation_template || draft_attestation_template
end
@ -588,9 +587,9 @@ class Procedure < ApplicationRecord
# As an optimization, dont check the predefined templates (they are presumed correct)
if closed_mail.present?
tag_present = closed_mail.body.to_s.include?("--lien attestation--")
if active_attestation_template&.activated? && !tag_present
if attestation_template&.activated? && !tag_present
:missing_tag
elsif !active_attestation_template&.activated? && tag_present
elsif !attestation_template&.activated? && tag_present
:extraneous_tag
end
end

View file

@ -4,13 +4,13 @@ namespace :after_party do
rake_puts "Running deploy task 'revise_attestation_templates'"
revisions = ProcedureRevision
.joins(procedure: :attestation_template)
.joins(procedure: :legacy_attestation_template)
.where(attestation_template_id: nil)
progress = ProgressReport.new(revisions.count)
revisions.find_each do |revision|
attestation_template_id = revision.procedure.attestation_template.id
attestation_template_id = revision.procedure.legacy_attestation_template.id
revision.update_column(:attestation_template_id, attestation_template_id)
progress.inc

View file

@ -188,9 +188,8 @@ FactoryBot.define do
trait :with_attestation do
after(:build) do |dossier, _evaluator|
if dossier.procedure.attestation_template.blank?
dossier.procedure.attestation_template = build(:attestation_template)
end
dossier.revision.attestation_template ||= build(:attestation_template)
dossier.association(:attestation_template).target = dossier.revision.attestation_template
dossier.attestation = dossier.build_attestation
end
end

View file

@ -5,8 +5,8 @@ RSpec.describe Cron::DeclarativeProceduresJob, type: :job do
let(:state) { nil }
let(:procedure) { create(:procedure, :published, :for_individual, :with_instructeur, declarative_with_state: state) }
let(:nouveau_dossier1) { create(:dossier, :en_construction, :with_individual, procedure: procedure) }
let(:nouveau_dossier2) { create(:dossier, :en_construction, :with_individual, procedure: procedure) }
let(:nouveau_dossier1) { create(:dossier, :en_construction, :with_individual, :with_attestation, procedure: procedure) }
let(:nouveau_dossier2) { create(:dossier, :en_construction, :with_individual, :with_attestation, procedure: procedure) }
let(:dossier_recu) { create(:dossier, :en_instruction, :with_individual, procedure: procedure) }
let(:dossier_brouillon) { create(:dossier, procedure: procedure) }
let(:dossier_repasse_en_construction) { create(:dossier, :en_construction, :with_individual, procedure: procedure) }
@ -22,7 +22,6 @@ RSpec.describe Cron::DeclarativeProceduresJob, type: :job do
dossier_repasse_en_construction
]
create(:attestation_template, procedure: procedure)
Cron::DeclarativeProceduresJob.new.perform
dossiers.each(&:reload)

View file

@ -1,7 +1,7 @@
describe '20220112184331_revise_attestation_templates' do
let(:rake_task) { Rake::Task['after_party:revise_attestation_templates'] }
let(:procedure) { create(:procedure) }
let(:attestation_template) { create(:attestation_template, procedure: procedure) }
let(:attestation_template) { create(:attestation_template, procedure_id: procedure.id) }
subject(:run_task) do
attestation_template
@ -13,10 +13,10 @@ describe '20220112184331_revise_attestation_templates' do
describe 'revise_attestation_templates' do
it 'attaches the attestation_template to the latest revision (without removing the link between attestation_template and procedure for now)' do
expect(attestation_template.procedure.revisions.first.attestation_template_id).to be_nil
expect(procedure.revisions.first.attestation_template_id).to be_nil
run_task
expect(attestation_template.procedure_id).to eq(procedure.id)
expect(attestation_template.procedure.revisions.first.attestation_template_id).to eq(attestation_template.id)
expect(procedure.revisions.first.attestation_template_id).to eq(attestation_template.id)
end
end
end

View file

@ -95,7 +95,8 @@ describe AttestationTemplate, type: :model do
create(:procedure,
types_de_champ: types_de_champ,
types_de_champ_private: types_de_champ_private,
for_individual: for_individual)
for_individual: for_individual,
attestation_template: attestation_template)
end
let(:for_individual) { false }
let(:individual) { nil }
@ -106,7 +107,7 @@ describe AttestationTemplate, type: :model do
let(:template_title) { 'title' }
let(:template_body) { 'body' }
let(:attestation_template) do
build(:attestation_template, procedure: procedure,
build(:attestation_template,
title: template_title,
body: template_body,
logo: @logo,

View file

@ -40,7 +40,8 @@ describe Procedure do
end
describe 'closed mail template body' do
let(:procedure) { create(:procedure) }
let(:procedure) { create(:procedure, attestation_template: attestation_template) }
let(:attestation_template) { nil }
subject { procedure.closed_mail_template.rich_body.body.to_html }
@ -49,7 +50,7 @@ describe Procedure do
end
context 'for procedures with an attestation' do
before { create(:attestation_template, procedure: procedure, activated: activated) }
let(:attestation_template) { build(:attestation_template, activated: activated) }
context 'when the attestation is inactive' do
let(:activated) { false }
@ -414,7 +415,16 @@ describe Procedure do
describe 'clone' do
let(:service) { create(:service) }
let(:procedure) { create(:procedure, received_mail: received_mail, service: service, types_de_champ: [type_de_champ_0, type_de_champ_1, type_de_champ_2, type_de_champ_pj, type_de_champ_repetition], types_de_champ_private: [type_de_champ_private_0, type_de_champ_private_1, type_de_champ_private_2, type_de_champ_private_repetition], api_particulier_token: '123456789012345', api_particulier_scopes: ['cnaf_famille']) }
let(:procedure) do
create(:procedure,
received_mail: received_mail,
service: service,
attestation_template: build(:attestation_template, logo: logo, signature: signature),
types_de_champ: [type_de_champ_0, type_de_champ_1, type_de_champ_2, type_de_champ_pj, type_de_champ_repetition],
types_de_champ_private: [type_de_champ_private_0, type_de_champ_private_1, type_de_champ_private_2, type_de_champ_private_repetition],
api_particulier_token: '123456789012345',
api_particulier_scopes: ['cnaf_famille'])
end
let(:type_de_champ_0) { build(:type_de_champ, position: 0) }
let(:type_de_champ_1) { build(:type_de_champ, position: 1) }
let(:type_de_champ_2) { build(:type_de_champ_drop_down_list, position: 2) }
@ -427,6 +437,8 @@ describe Procedure do
let(:received_mail) { build(:received_mail) }
let(:from_library) { false }
let(:administrateur) { procedure.administrateurs.first }
let(:logo) { Rack::Test::UploadedFile.new('spec/fixtures/files/white.png', 'image/png') }
let(:signature) { Rack::Test::UploadedFile.new('spec/fixtures/files/black.png', 'image/png') }
let(:groupe_instructeur_1) { create(:groupe_instructeur, procedure: procedure, label: "groupe_1") }
let(:instructeur_1) { create(:instructeur) }
@ -435,9 +447,6 @@ describe Procedure do
let!(:assign_to_2) { create(:assign_to, procedure: procedure, groupe_instructeur: groupe_instructeur_1, instructeur: instructeur_2) }
before do
@logo = Rack::Test::UploadedFile.new('spec/fixtures/files/white.png', 'image/png')
@signature = Rack::Test::UploadedFile.new('spec/fixtures/files/black.png', 'image/png')
@attestation_template = create(:attestation_template, procedure: procedure, logo: @logo, signature: @signature)
@procedure = procedure.clone(administrateur, from_library)
@procedure.save
end