From 27c8e9dcb8cd8db3b6b7745be1b26938770ce9cf Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 28 Mar 2019 17:17:29 +0100 Subject: [PATCH 1/4] Use the same clone_attachment method for all attributes on procedure.clone --- app/models/procedure.rb | 42 +++++++++++++++++------------------ spec/models/procedure_spec.rb | 16 +++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 62b1c7a34..752179f10 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -193,16 +193,6 @@ class Procedure < ApplicationRecord end end - def clone_attachments(original, kopy) - if original.is_a?(TypeDeChamp) && original.piece_justificative_template.attached? - kopy.piece_justificative_template.attach({ - io: StringIO.new(original.piece_justificative_template.download), - filename: original.piece_justificative_template.blob.filename, - content_type: original.piece_justificative_template.blob.content_type - }) - end - end - def clone(admin, from_library) is_different_admin = !admin.owns?(self) @@ -222,8 +212,6 @@ class Procedure < ApplicationRecord procedure.remote_logo_url = self.logo_url procedure.lien_notice = nil - [:notice, :deliberation].each { |attachment| clone_attachment(procedure, attachment) } - procedure.types_de_champ += PiecesJustificativesService.types_pj_as_types_de_champ(self) if is_different_admin || from_library procedure.types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) } @@ -255,6 +243,26 @@ class Procedure < ApplicationRecord procedure end + def clone_attachments(original, kopy) + if original.is_a?(TypeDeChamp) + clone_attachment(:piece_justificative_template, original, kopy) + elsif original.is_a?(Procedure) + clone_attachment(:notice, original, kopy) + clone_attachment(:deliberation, original, kopy) + end + end + + def clone_attachment(attribute, original, kopy) + original_attachment = original.send(attribute) + if original_attachment.attached? + kopy.send(attribute).attach({ + io: StringIO.new(original_attachment.download), + filename: original_attachment.blob.filename, + content_type: original_attachment.blob.content_type + }) + end + end + def whitelisted? whitelisted_at.present? end @@ -455,16 +463,6 @@ class Procedure < ApplicationRecord true end - def clone_attachment(cloned_procedure, attachment_symbol) - attachment = send(attachment_symbol) - if attachment.attached? - cloned_procedure.send(attachment_symbol).attach( - io: StringIO.new(attachment.download), - filename: attachment.filename - ) - end - end - def check_juridique if juridique_required? && (cadre_juridique.blank? && !deliberation.attached?) errors.add(:cadre_juridique, " : veuillez remplir le texte de loi ou la délibération") diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 3148b3d76..1d70c8c29 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -489,6 +489,22 @@ describe Procedure do it 'should duplicate piece_justificative_template on a type_de_champ' do expect(subject.types_de_champ.where(type_champ: "piece_justificative").first.piece_justificative_template.attached?).to be true end + + context 'with a notice attached' do + let(:procedure) { create(:procedure, :with_notice, received_mail: received_mail, service: service) } + + it 'should duplicate notice' do + expect(subject.notice.attached?).to be true + end + end + + context 'with a deliberation attached' do + let(:procedure) { create(:procedure, :with_deliberation, received_mail: received_mail, service: service) } + + it 'should duplicate deliberation' do + expect(subject.deliberation.attached?).to be true + end + end end describe '#publish!' do From c5f8aa2e0d79aad070c313d559173e859bf9e297 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Thu, 28 Mar 2019 17:45:07 +0100 Subject: [PATCH 2/4] [#2180] Fix updated_at for migration PJ champs --- ...e_justificative_to_champ_piece_jointe_migration_service.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb b/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb index 5d2ace62e..0778454f3 100644 --- a/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb +++ b/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb @@ -37,12 +37,12 @@ class PieceJustificativeToChampPieceJointeMigrationService pj = dossier.retrieve_last_piece_justificative_by_type(type_pj_id) if pj.present? + convert_pj_to_champ!(pj, champ) + champ.update( updated_at: pj.updated_at, created_at: pj.created_at ) - - convert_pj_to_champ!(pj, champ) else champ.update( updated_at: dossier.updated_at, From 7ed1e5ac82618a085366a0a1717190406d2a5b20 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Wed, 27 Mar 2019 18:56:18 +0100 Subject: [PATCH 3/4] [Fix #3692] Add HTML5 validation to ensure SIRET length is 14 --- app/views/shared/dossiers/editable_champs/_siret.html.haml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/views/shared/dossiers/editable_champs/_siret.html.haml b/app/views/shared/dossiers/editable_champs/_siret.html.haml index ca5bbac72..5dbb2520e 100644 --- a/app/views/shared/dossiers/editable_champs/_siret.html.haml +++ b/app/views/shared/dossiers/editable_champs/_siret.html.haml @@ -2,7 +2,9 @@ placeholder: champ.libelle, class: 'small-margin', data: { remote: true, debounce: true, url: champs_siret_path(form.index), params: { champ_id: champ&.id }.to_query, spinner: true }, - required: champ.mandatory? + required: champ.mandatory?, + pattern: "[0-9]{14}", + title: "Le numéro de SIRET doit comporter exactement 14 chiffres" .spinner.right-spinner.hidden %div{ class: "siret-info-#{form.index}" } - if champ.etablissement.present? From 71e0969bc00a3fbe34f9336d94c5469b5a13bf8a Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 28 Mar 2019 11:28:27 +0100 Subject: [PATCH 4/4] [Fix #3692] Ensure SIRET is correct on server --- app/models/champs/siret_champ.rb | 4 ++++ spec/models/dossier_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/models/champs/siret_champ.rb b/app/models/champs/siret_champ.rb index d968c85b7..5f9656385 100644 --- a/app/models/champs/siret_champ.rb +++ b/app/models/champs/siret_champ.rb @@ -2,4 +2,8 @@ class Champs::SiretChamp < Champ def search_terms etablissement.present? ? etablissement.search_terms : [value] end + + def mandatory_and_blank? + mandatory? && Siret.new(siret: value).invalid? + end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index dc9382add..0ab8763f5 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -858,6 +858,33 @@ describe Dossier do end end + context "with mandatory SIRET champ" do + let(:type_de_champ) { create(:type_de_champ_siret, mandatory: true) } + let(:champ_siret) { create(:champ_siret, type_de_champ: type_de_champ) } + + before do + dossier.champs << champ_siret + end + + it 'should not have errors' do + errors = dossier.check_mandatory_champs + expect(errors).to be_empty + end + + context "and invalid SIRET" do + before do + champ_siret.update(value: "1234") + dossier.reload + end + + it 'should have errors' do + errors = dossier.check_mandatory_champs + expect(errors).not_to be_empty + expect(errors.first).to eq("Le champ #{champ_siret.libelle} doit être rempli.") + end + end + end + context "with champ repetition" do let(:procedure) { create(:procedure) } let(:type_de_champ_repetition) { create(:type_de_champ_repetition, mandatory: true) }