diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 2498604cff7..71834206092 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -199,6 +199,7 @@ def switch_list_order(list, index_of_first_element) end def clone(admin, from_library) + populate_champ_stable_ids procedure = self.deep_clone(include: { types_de_piece_justificative: nil, @@ -358,6 +359,12 @@ def gestionnaire_for_cron_job gestionnaire || gestionnaires.first end + def populate_champ_stable_ids + TypeDeChamp.where(procedure: self, stable_id: nil).find_each do |type_de_champ| + type_de_champ.update_column(:stable_id, type_de_champ.id) + end + end + private def claim_path_ownership!(path) diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index c2d9a9ae6ed..61bf18695f8 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -36,6 +36,7 @@ class TypeDeChamp < ApplicationRecord store :options, accessors: [:cadastres, :quartiers_prioritaires, :parcelles_agricoles] after_initialize :set_dynamic_type + after_create :populate_stable_id attr_reader :dynamic_type @@ -117,6 +118,12 @@ def self.type_champ_to_class_name(type_champ) private + def populate_stable_id + if !stable_id + update_column(:stable_id, id) + end + end + def remove_piece_justificative_template if type_champ != TypeDeChamp.type_champs.fetch(:piece_justificative) && piece_justificative_template.attached? piece_justificative_template.purge_later diff --git a/app/serializers/type_de_champ_serializer.rb b/app/serializers/type_de_champ_serializer.rb index 152e908e156..b8ef6ecb770 100644 --- a/app/serializers/type_de_champ_serializer.rb +++ b/app/serializers/type_de_champ_serializer.rb @@ -4,4 +4,8 @@ class TypeDeChampSerializer < ActiveModel::Serializer :type_champ, :order_place, :description + + def id + object.stable_id || object.id + end end diff --git a/db/migrate/20181123181020_add_stable_id_to_types_de_champ.rb b/db/migrate/20181123181020_add_stable_id_to_types_de_champ.rb new file mode 100644 index 00000000000..958eb90a2b8 --- /dev/null +++ b/db/migrate/20181123181020_add_stable_id_to_types_de_champ.rb @@ -0,0 +1,6 @@ +class AddStableIdToTypesDeChamp < ActiveRecord::Migration[5.2] + def change + add_column :types_de_champ, :stable_id, :bigint + add_index :types_de_champ, :stable_id + end +end diff --git a/db/schema.rb b/db/schema.rb index b8b98f83ebf..1d8759dfab7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -556,7 +556,9 @@ t.datetime "created_at" t.datetime "updated_at" t.jsonb "options" + t.bigint "stable_id" t.index ["private"], name: "index_types_de_champ_on_private" + t.index ["stable_id"], name: "index_types_de_champ_on_stable_id" end create_table "types_de_piece_justificative", id: :serial, force: :cascade do |t| diff --git a/lib/tasks/deployment/20181123181252_add_stable_id_to_types_de_champ.rake b/lib/tasks/deployment/20181123181252_add_stable_id_to_types_de_champ.rake new file mode 100644 index 00000000000..dbb3283dd35 --- /dev/null +++ b/lib/tasks/deployment/20181123181252_add_stable_id_to_types_de_champ.rake @@ -0,0 +1,15 @@ +namespace :after_party do + desc 'Deployment task: add_stable_id_to_types_de_champ' + task add_stable_id_to_types_de_champ: :environment do + types_de_champ = TypeDeChamp.where(stable_id: nil) + bar = RakeProgressbar.new(types_de_champ.count) + + types_de_champ.find_each do |type_de_champ| + type_de_champ.update_column(:stable_id, type_de_champ.id) + bar.inc + end + bar.finished + + AfterParty::TaskRecord.create version: '20181123181252' + end +end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index aa9934e6776..cb9f92ad886 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -424,6 +424,11 @@ expect(subject.path).to be_nil end end + + it 'should keep types_de_champ ids stable' do + expect(subject.types_de_champ_ordered.first.id).not_to eq(procedure.types_de_champ_ordered.first.id) + expect(subject.types_de_champ_ordered.first.stable_id).to eq(procedure.types_de_champ_ordered.first.id) + end end describe '#publish!' do diff --git a/spec/models/type_de_champ_shared_example.rb b/spec/models/type_de_champ_shared_example.rb index 092527bbcea..5fbceb3bddf 100644 --- a/spec/models/type_de_champ_shared_example.rb +++ b/spec/models/type_de_champ_shared_example.rb @@ -39,6 +39,15 @@ it { is_expected.to allow_value('blabla').for(:description) } end + context 'stable_id' do + it { + type_de_champ = create(:type_de_champ_text) + expect(type_de_champ.id).to eq(type_de_champ.stable_id) + cloned_type_de_champ = type_de_champ.clone + expect(cloned_type_de_champ.stable_id).to eq(type_de_champ.stable_id) + } + end + context 'remove piece_justificative_template' do context 'when the tdc is piece_justificative' do let(:template_double) { double('template', attached?: attached, purge_later: true) }