From 04ad829e3f9372c20eca0c17b13c22b631c44538 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 4 Dec 2019 16:58:26 +0100 Subject: [PATCH 1/3] Add canonical_procedure_id to procedures --- ...0200114113700_add_canonical_procedure_id_to_procedures.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20200114113700_add_canonical_procedure_id_to_procedures.rb diff --git a/db/migrate/20200114113700_add_canonical_procedure_id_to_procedures.rb b/db/migrate/20200114113700_add_canonical_procedure_id_to_procedures.rb new file mode 100644 index 000000000..a2ae37565 --- /dev/null +++ b/db/migrate/20200114113700_add_canonical_procedure_id_to_procedures.rb @@ -0,0 +1,5 @@ +class AddCanonicalProcedureIdToProcedures < ActiveRecord::Migration[5.2] + def change + add_column :procedures, :canonical_procedure_id, :bigint + end +end diff --git a/db/schema.rb b/db/schema.rb index e7ad727d2..e998f6be4 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: 2019_12_18_103727) do +ActiveRecord::Schema.define(version: 2020_01_14_113700) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -502,6 +502,7 @@ ActiveRecord::Schema.define(version: 2019_12_18_103727) do t.boolean "ods_export_queued" t.datetime "closed_at" t.datetime "unpublished_at" + t.bigint "canonical_procedure_id" t.index ["declarative_with_state"], name: "index_procedures_on_declarative_with_state" t.index ["hidden_at"], name: "index_procedures_on_hidden_at" t.index ["parent_procedure_id"], name: "index_procedures_on_parent_procedure_id" From 517ab2578312e9b77c8656f6a223bb2f3ac2fbcc Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 4 Dec 2019 16:58:36 +0100 Subject: [PATCH 2/3] Register canonical procedure when demarche republished under existing path --- app/models/procedure.rb | 10 ++++--- spec/models/procedure_spec.rb | 49 +++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 8697e3d5d..a2f6126fe 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -16,6 +16,7 @@ class Procedure < ApplicationRecord has_one :attestation_template, dependent: :destroy belongs_to :parent_procedure, class_name: 'Procedure' + belongs_to :canonical_procedure, class_name: 'Procedure' belongs_to :service has_many :administrateurs_procedures @@ -129,9 +130,10 @@ class Procedure < ApplicationRecord other_procedure = other_procedure_with_path(path) if other_procedure.present? && administrateur.owns?(other_procedure) other_procedure.unpublish! + publish!(other_procedure.canonical_procedure || other_procedure) + else + publish! end - - publish! end end @@ -615,8 +617,8 @@ class Procedure < ApplicationRecord update!(closed_at: nil, unpublished_at: nil) end - def after_publish - update!(published_at: Time.zone.now) + def after_publish(canonical_procedure = nil) + update!(published_at: Time.zone.now, canonical_procedure: canonical_procedure) end def after_close diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 099dd91a6..6fdc5a0d0 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -536,38 +536,77 @@ describe Procedure do after { Timecop.return } - context "without parent procedure" do + context "without canonical procedure" do before do Timecop.freeze(now) procedure.publish! end it do + expect(procedure.canonical_procedure).to be_nil expect(procedure.closed_at).to be_nil expect(procedure.published_at).to eq(now) expect(Procedure.find_by(path: "example-path")).to eq(procedure) expect(Procedure.find_by(path: "example-path").administrateurs).to eq(procedure.administrateurs) end end + + context "with canonical procedure" do + let(:canonical_procedure) { create(:procedure, :published) } + + before do + Timecop.freeze(now) + procedure.publish!(canonical_procedure) + end + + it do + expect(procedure.canonical_procedure).to eq(canonical_procedure) + expect(procedure.closed_at).to be_nil + expect(procedure.published_at).to eq(now) + end + end end describe "#publish_or_reopen!" do - let(:published_procedure) { create(:procedure, :published) } - let(:administrateur) { published_procedure.administrateurs.first } + let(:canonical_procedure) { create(:procedure, :published) } + let(:administrateur) { canonical_procedure.administrateurs.first } let(:procedure) { create(:procedure, administrateurs: [administrateur]) } let(:now) { Time.zone.now.beginning_of_minute } - context "without parent procedure" do + context "without canonical procedure" do before do Timecop.freeze(now) - procedure.path = published_procedure.path + procedure.path = canonical_procedure.path procedure.publish_or_reopen!(administrateur) + canonical_procedure.reload end it do + expect(procedure.canonical_procedure).to eq(canonical_procedure) expect(procedure.closed_at).to be_nil expect(procedure.published_at).to eq(now) + expect(canonical_procedure.unpublished_at).to eq(now) + end + end + + context "with canonical procedure" do + let(:canonical_procedure) { create(:procedure, :closed) } + let(:parent_procedure) { create(:procedure, :published, administrateurs: [administrateur]) } + + before do + parent_procedure.update!(path: canonical_procedure.path, canonical_procedure: canonical_procedure) + Timecop.freeze(now) + procedure.path = canonical_procedure.path + procedure.publish_or_reopen!(administrateur) + parent_procedure.reload + end + + it do + expect(procedure.canonical_procedure).to eq(canonical_procedure) + expect(procedure.closed_at).to be_nil + expect(procedure.published_at).to eq(now) + expect(parent_procedure.unpublished_at).to eq(now) end end end From 67dea1ee50ef346abb93ee844b5b4c68424d1427 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 7 Jan 2020 17:44:45 +0100 Subject: [PATCH 3/3] Update spec/models/procedure_spec.rb Co-Authored-By: Pierre de La Morinerie --- spec/models/procedure_spec.rb | 69 +++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 6fdc5a0d0..36d654b99 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -534,16 +534,18 @@ describe Procedure do let(:procedure) { create(:procedure, path: 'example-path') } let(:now) { Time.zone.now.beginning_of_minute } - after { Timecop.return } - - context "without canonical procedure" do + context 'when publishing a new procedure' do before do - Timecop.freeze(now) - procedure.publish! + Timecop.freeze(now) do + procedure.publish! + end end - it do + it 'no reference to the canonical procedure on the published procedure' do expect(procedure.canonical_procedure).to be_nil + end + + it 'changes the procedure state to published' do expect(procedure.closed_at).to be_nil expect(procedure.published_at).to eq(now) expect(Procedure.find_by(path: "example-path")).to eq(procedure) @@ -551,16 +553,20 @@ describe Procedure do end end - context "with canonical procedure" do + context 'when publishing over a previous canonical procedure' do let(:canonical_procedure) { create(:procedure, :published) } before do - Timecop.freeze(now) - procedure.publish!(canonical_procedure) + Timecop.freeze(now) do + procedure.publish!(canonical_procedure) + end end - it do + it 'references the canonical procedure on the published procedure' do expect(procedure.canonical_procedure).to eq(canonical_procedure) + end + + it 'changes the procedure state to published' do expect(procedure.closed_at).to be_nil expect(procedure.published_at).to eq(now) end @@ -574,38 +580,53 @@ describe Procedure do let(:procedure) { create(:procedure, administrateurs: [administrateur]) } let(:now) { Time.zone.now.beginning_of_minute } - context "without canonical procedure" do + context 'when publishing over a previous canonical procedure' do before do - Timecop.freeze(now) procedure.path = canonical_procedure.path - procedure.publish_or_reopen!(administrateur) + Timecop.freeze(now) do + procedure.publish_or_reopen!(administrateur) + end canonical_procedure.reload end - it do + it 'references the canonical procedure on the published procedure' do expect(procedure.canonical_procedure).to eq(canonical_procedure) + end + + it 'changes the procedure state to published' do expect(procedure.closed_at).to be_nil expect(procedure.published_at).to eq(now) + end + + it 'unpublishes the canonical procedure' do expect(canonical_procedure.unpublished_at).to eq(now) end end - context "with canonical procedure" do + context 'when publishing over a previous procedure with canonical procedure' do let(:canonical_procedure) { create(:procedure, :closed) } let(:parent_procedure) { create(:procedure, :published, administrateurs: [administrateur]) } before do parent_procedure.update!(path: canonical_procedure.path, canonical_procedure: canonical_procedure) - Timecop.freeze(now) procedure.path = canonical_procedure.path - procedure.publish_or_reopen!(administrateur) + Timecop.freeze(now) do + procedure.publish_or_reopen!(administrateur) + end parent_procedure.reload end - it do + it 'references the canonical procedure on the published procedure' do + expect(procedure.canonical_procedure).to eq(canonical_procedure) + end + + it 'changes the procedure state to published' do expect(procedure.canonical_procedure).to eq(canonical_procedure) expect(procedure.closed_at).to be_nil expect(procedure.published_at).to eq(now) + end + + it 'unpublishes parent procedure' do expect(parent_procedure.unpublished_at).to eq(now) end end @@ -616,10 +637,10 @@ describe Procedure do let(:now) { Time.zone.now.beginning_of_minute } before do - Timecop.freeze(now) - procedure.unpublish! + Timecop.freeze(now) do + procedure.unpublish! + end end - after { Timecop.return } it { expect(procedure.closed_at).to eq(nil) @@ -692,11 +713,11 @@ describe Procedure do let(:procedure) { create(:procedure, :published) } let(:now) { Time.zone.now.beginning_of_minute } before do - Timecop.freeze(now) - procedure.close! + Timecop.freeze(now) do + procedure.close! + end procedure.reload end - after { Timecop.return } it { expect(procedure.close?).to be_truthy } it { expect(procedure.closed_at).to eq(now) }