Add stable ids to types_de_champ after every update
This commit is contained in:
parent
fb57a07927
commit
1883c57c87
4 changed files with 28 additions and 0 deletions
|
@ -199,6 +199,7 @@ class Procedure < ApplicationRecord
|
|||
end
|
||||
|
||||
def clone(admin, from_library)
|
||||
populate_champ_stable_ids
|
||||
procedure = self.deep_clone(include:
|
||||
{
|
||||
types_de_piece_justificative: nil,
|
||||
|
@ -358,6 +359,12 @@ class Procedure < ApplicationRecord
|
|||
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)
|
||||
|
|
|
@ -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 @@ class TypeDeChamp < ApplicationRecord
|
|||
|
||||
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
|
||||
|
|
|
@ -424,6 +424,11 @@ describe Procedure do
|
|||
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
|
||||
|
|
|
@ -39,6 +39,15 @@ shared_examples 'type_de_champ_spec' do
|
|||
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) }
|
||||
|
|
Loading…
Reference in a new issue