Merge pull request #4619 from tchak/add-canonical-procedure

Register canonical procedure when démarche republished under existing path
This commit is contained in:
Paul Chavard 2020-01-28 13:00:06 +01:00 committed by GitHub
commit 81fd91ce41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 24 deletions

View file

@ -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

View file

@ -0,0 +1,5 @@
class AddCanonicalProcedureIdToProcedures < ActiveRecord::Migration[5.2]
def change
add_column :procedures, :canonical_procedure_id, :bigint
end
end

View file

@ -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"

View file

@ -534,41 +534,101 @@ describe Procedure do
let(:procedure) { create(:procedure, path: 'example-path') }
let(:now) { Time.zone.now.beginning_of_minute }
after { Timecop.return }
context "without parent 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)
expect(Procedure.find_by(path: "example-path").administrateurs).to eq(procedure.administrateurs)
end
end
context 'when publishing over a previous canonical procedure' do
let(:canonical_procedure) { create(:procedure, :published) }
before do
Timecop.freeze(now) do
procedure.publish!(canonical_procedure)
end
end
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
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 'when publishing over a previous canonical procedure' do
before do
Timecop.freeze(now)
procedure.path = published_procedure.path
procedure.publish_or_reopen!(administrateur)
procedure.path = canonical_procedure.path
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 '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)
procedure.path = canonical_procedure.path
Timecop.freeze(now) do
procedure.publish_or_reopen!(administrateur)
end
parent_procedure.reload
end
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
end
@ -577,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)
@ -653,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) }