From 7eb34053f8bdbc77bfbc11bb47335c2bf9b0e431 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 1 Jun 2018 10:46:59 +0200 Subject: [PATCH 1/4] Procedure: add juridique_required flag to procedure --- ...80601084546_add_juridique_required_column_to_procedure.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180601084546_add_juridique_required_column_to_procedure.rb diff --git a/db/migrate/20180601084546_add_juridique_required_column_to_procedure.rb b/db/migrate/20180601084546_add_juridique_required_column_to_procedure.rb new file mode 100644 index 000000000..3c915b158 --- /dev/null +++ b/db/migrate/20180601084546_add_juridique_required_column_to_procedure.rb @@ -0,0 +1,5 @@ +class AddJuridiqueRequiredColumnToProcedure < ActiveRecord::Migration[5.2] + def change + add_column :procedures, :juridique_required, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 92e8ce361..60d35fb86 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_05_30_095508) do +ActiveRecord::Schema.define(version: 2018_06_01_084546) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -482,6 +482,7 @@ ActiveRecord::Schema.define(version: 2018_05_30_095508) do t.integer "duree_conservation_dossiers_dans_ds" t.integer "duree_conservation_dossiers_hors_ds" t.string "cadre_juridique" + t.boolean "juridique_required", default: true t.index ["hidden_at"], name: "index_procedures_on_hidden_at" t.index ["parent_procedure_id"], name: "index_procedures_on_parent_procedure_id" t.index ["service_id"], name: "index_procedures_on_service_id" From 1f0e46a4ff85a500b3edc157e1522131a196157a Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 1 Jun 2018 10:51:04 +0200 Subject: [PATCH 2/4] Procedure: required juridique stuff only if needed --- app/models/procedure.rb | 2 +- spec/models/procedure_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 32c90e489..fe39040b3 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -357,7 +357,7 @@ class Procedure < ApplicationRecord end def check_juridique - if cadre_juridique.blank? && !deliberation.attached? + if juridique_required? && (cadre_juridique.blank? && !deliberation.attached?) errors.add(:cadre_juridique, " : veuillez remplir le texte de loi ou la délibération") end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index ac091cb6d..6658356e2 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -193,6 +193,12 @@ describe Procedure do end end end + + context 'when juridique_required is false' do + let(:procedure) { build(:procedure, juridique_required: false, cadre_juridique: nil) } + + it { expect(procedure.valid?).to eq(true) } + end end describe '#types_de_champ_ordered' do From b9984250053e3f6949cbd0cb52c6a825d2c78b99 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 1 Jun 2018 10:54:04 +0200 Subject: [PATCH 3/4] Procedure: add rake task to set juridique_required to false for legacy procedures --- .../2018_06_01_optional_juridique_for_legacy_procedures.rake | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 lib/tasks/2018_06_01_optional_juridique_for_legacy_procedures.rake diff --git a/lib/tasks/2018_06_01_optional_juridique_for_legacy_procedures.rake b/lib/tasks/2018_06_01_optional_juridique_for_legacy_procedures.rake new file mode 100644 index 000000000..dbc65d586 --- /dev/null +++ b/lib/tasks/2018_06_01_optional_juridique_for_legacy_procedures.rake @@ -0,0 +1,5 @@ +namespace :'2018_06_01_optional_juridique_for_legacy_procedures' do + task set: :environment do + Procedure.all.update_all(juridique_required: false) + end +end From c72421fb9e53041feea3f32b5291d598d89e217d Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 1 Jun 2018 11:06:12 +0200 Subject: [PATCH 4/4] Procedure: Once deliberation or cadre_juridique have been set, forbid unsetting them --- app/models/procedure.rb | 7 +++++++ spec/models/procedure_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index fe39040b3..62f7707fc 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -48,6 +48,8 @@ class Procedure < ApplicationRecord validates :description, presence: true, allow_blank: false, allow_nil: false validate :check_juridique + before_save :update_juridique_required + include AASM aasm whiny_persistence: true do @@ -343,6 +345,11 @@ class Procedure < ApplicationRecord private + def update_juridique_required + self.juridique_required ||= (cadre_juridique.present? || deliberation.attached?) + true + end + def clone_attachment(cloned_procedure, attachment_symbol) attachment = send(attachment_symbol) if attachment.attached? diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 6658356e2..c09e823c1 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -677,4 +677,30 @@ describe Procedure do it { expect(Champ.count).to eq(0) } end + + describe '#juridique_required' do + it 'automatically jumps to true once cadre_juridique or deliberation have been set' do + p = create( + :procedure, + juridique_required: false, + cadre_juridique: nil, + ) + + expect(p.juridique_required).to be_falsey + + p.update(cadre_juridique: 'cadre') + expect(p.juridique_required).to be_truthy + + p.update(cadre_juridique: nil) + expect(p.juridique_required).to be_truthy + + p.update_columns(cadre_juridique: nil, juridique_required: false) + p.reload + expect(p.juridique_required).to be_falsey + + allow(p).to receive(:deliberation).and_return(double('attached?': true)) + p.save + expect(p.juridique_required).to be_truthy + end + end end