diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 32c90e489..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? @@ -357,7 +364,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/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" 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 diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index ac091cb6d..c09e823c1 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 @@ -671,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