Remove repetition types_de_champ on type_champ change
This commit is contained in:
parent
134713649e
commit
3c9cc6c070
3 changed files with 27 additions and 0 deletions
|
@ -83,6 +83,7 @@ class TypeDeChamp < ApplicationRecord
|
|||
before_validation :check_mandatory
|
||||
before_save :remove_piece_justificative_template, if: -> { type_champ_changed? }
|
||||
before_validation :remove_drop_down_list, if: -> { type_champ_changed? }
|
||||
before_save :remove_repetition, if: -> { type_champ_changed? }
|
||||
|
||||
def valid?(context = nil)
|
||||
super
|
||||
|
@ -302,4 +303,10 @@ class TypeDeChamp < ApplicationRecord
|
|||
self.drop_down_options = nil
|
||||
end
|
||||
end
|
||||
|
||||
def remove_repetition
|
||||
if !repetition?
|
||||
types_de_champ.destroy_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -108,6 +108,12 @@ FactoryBot.define do
|
|||
end
|
||||
factory :type_de_champ_repetition do
|
||||
type_champ { TypeDeChamp.type_champs.fetch(:repetition) }
|
||||
|
||||
trait :with_types_de_champ do
|
||||
after(:build) do |type_de_champ, _evaluator|
|
||||
type_de_champ.types_de_champ << create(:type_de_champ, libelle: 'sub type de champ')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
trait :private do
|
||||
|
|
|
@ -89,6 +89,20 @@ shared_examples 'type_de_champ_spec' do
|
|||
end
|
||||
end
|
||||
|
||||
context 'remove repetition' do
|
||||
let(:tdc) { create(:type_de_champ_repetition, :with_types_de_champ) }
|
||||
|
||||
before do
|
||||
tdc.update_attribute('type_champ', target_type_champ)
|
||||
end
|
||||
|
||||
context 'when the target type_champ is not repetition' do
|
||||
let(:target_type_champ) { TypeDeChamp.type_champs.fetch(:text) }
|
||||
|
||||
it { expect(tdc.types_de_champ).to be_empty }
|
||||
end
|
||||
end
|
||||
|
||||
context 'delegate validation to dynamic type' do
|
||||
subject { build(:type_de_champ_text) }
|
||||
let(:dynamic_type) do
|
||||
|
|
Loading…
Add table
Reference in a new issue