Merge pull request #5355 from tchak/clean-up-repetition
Remove repetition types_de_champ on type_champ change
This commit is contained in:
commit
8f99a0eda3
3 changed files with 62 additions and 3 deletions
|
@ -82,7 +82,12 @@ class TypeDeChamp < ApplicationRecord
|
||||||
|
|
||||||
before_validation :check_mandatory
|
before_validation :check_mandatory
|
||||||
before_save :remove_piece_justificative_template, if: -> { type_champ_changed? }
|
before_save :remove_piece_justificative_template, if: -> { type_champ_changed? }
|
||||||
before_validation :remove_drop_down_list, if: -> { type_champ_changed? }
|
before_save :remove_drop_down_list, if: -> { type_champ_changed? }
|
||||||
|
before_save :remove_repetition, if: -> { type_champ_changed? }
|
||||||
|
|
||||||
|
after_save if: -> { @remove_piece_justificative_template } do
|
||||||
|
piece_justificative_template.purge_later
|
||||||
|
end
|
||||||
|
|
||||||
def valid?(context = nil)
|
def valid?(context = nil)
|
||||||
super
|
super
|
||||||
|
@ -292,7 +297,7 @@ class TypeDeChamp < ApplicationRecord
|
||||||
|
|
||||||
def remove_piece_justificative_template
|
def remove_piece_justificative_template
|
||||||
if !piece_justificative? && piece_justificative_template.attached?
|
if !piece_justificative? && piece_justificative_template.attached?
|
||||||
piece_justificative_template.purge_later
|
@remove_piece_justificative_template = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -302,4 +307,10 @@ class TypeDeChamp < ApplicationRecord
|
||||||
self.drop_down_options = nil
|
self.drop_down_options = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def remove_repetition
|
||||||
|
if !repetition?
|
||||||
|
types_de_champ.destroy_all
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -108,6 +108,12 @@ FactoryBot.define do
|
||||||
end
|
end
|
||||||
factory :type_de_champ_repetition do
|
factory :type_de_champ_repetition do
|
||||||
type_champ { TypeDeChamp.type_champs.fetch(:repetition) }
|
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
|
end
|
||||||
|
|
||||||
trait :private do
|
trait :private do
|
||||||
|
|
|
@ -48,7 +48,7 @@ shared_examples 'type_de_champ_spec' do
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'remove piece_justificative_template' do
|
context 'changing the type_champ from a piece_justificative' do
|
||||||
context 'when the tdc is piece_justificative' do
|
context 'when the tdc is piece_justificative' do
|
||||||
let(:template_double) { double('template', attached?: attached, purge_later: true) }
|
let(:template_double) { double('template', attached?: attached, purge_later: true) }
|
||||||
let(:tdc) { create(:type_de_champ_piece_justificative) }
|
let(:tdc) { create(:type_de_champ_piece_justificative) }
|
||||||
|
@ -89,6 +89,48 @@ shared_examples 'type_de_champ_spec' do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'changing the type_champ from a 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 'removes the children types de champ' do
|
||||||
|
expect(tdc.types_de_champ).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'changing the type_champ from a drop_down_list' do
|
||||||
|
let(:tdc) { create(:type_de_champ_drop_down_list) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
tdc.update_attribute('type_champ', target_type_champ)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the target type_champ is not drop_down_list' do
|
||||||
|
let(:target_type_champ) { TypeDeChamp.type_champs.fetch(:text) }
|
||||||
|
|
||||||
|
it { expect(tdc.drop_down_options).to be_nil }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the target type_champ is linked_drop_down_list' do
|
||||||
|
let(:target_type_champ) { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) }
|
||||||
|
|
||||||
|
it { expect(tdc.drop_down_options).to be_present }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the target type_champ is multiple_drop_down_list' do
|
||||||
|
let(:target_type_champ) { TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) }
|
||||||
|
|
||||||
|
it { expect(tdc.drop_down_options).to be_present }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'delegate validation to dynamic type' do
|
context 'delegate validation to dynamic type' do
|
||||||
subject { build(:type_de_champ_text) }
|
subject { build(:type_de_champ_text) }
|
||||||
let(:dynamic_type) do
|
let(:dynamic_type) do
|
||||||
|
|
Loading…
Add table
Reference in a new issue