fix(TypesDeChamp::ConditionValidator): allow to use types_de_champ_public on condition for types_de_champ_private

This commit is contained in:
mfo 2024-06-10 09:57:34 +02:00
parent d2ccea700b
commit 27b86f9848
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
3 changed files with 45 additions and 28 deletions

View file

@ -1,16 +1,19 @@
class TypesDeChamp::ConditionValidator < ActiveModel::EachValidator class TypesDeChamp::ConditionValidator < ActiveModel::EachValidator
def validate_each(procedure, attribute, types_de_champ) def validate_each(procedure, attribute, types_de_champ)
public_tdcs = types_de_champ.to_a return if types_de_champ.empty?
.flat_map { _1.repetition? ? procedure.draft_revision.children_of(_1) : _1 }
public_tdcs tdcs = if attribute == :draft_types_de_champ_private
.map.with_index procedure.draft_revision.types_de_champ_for
.filter_map { |tdc, i| tdc.condition? ? [tdc, i] : nil } else
.map do |tdc, i| procedure.draft_revision.types_de_champ_for(scope: :public)
[tdc, tdc.condition.errors(public_tdcs.take(i))]
end end
.filter { |_tdc, errors| errors.present? }
.each do |tdc, _error_hash| tdcs.each_with_index do |tdc, i|
next unless tdc.condition?
errors = tdc.condition.errors(tdcs.take(i))
next if errors.blank?
procedure.errors.add( procedure.errors.add(
attribute, attribute,
procedure.errors.generate_message(attribute, :invalid_condition, { value: tdc.libelle }), procedure.errors.generate_message(attribute, :invalid_condition, { value: tdc.libelle }),

View file

@ -211,7 +211,7 @@ describe Procedure do
it { is_expected.to allow_value('text').on(:publication).for(:cadre_juridique) } it { is_expected.to allow_value('text').on(:publication).for(:cadre_juridique) }
context 'with deliberation' do context 'with deliberation' do
let(:procedure) { build(:procedure, cadre_juridique: nil) } let(:procedure) { build(:procedure, cadre_juridique: nil, revisions: [build(:procedure_revision)]) }
it { expect(procedure.valid?(:publication)).to eq(false) } it { expect(procedure.valid?(:publication)).to eq(false) }
@ -420,22 +420,37 @@ describe Procedure do
procedure.validate(:publication) procedure.validate(:publication)
expect(procedure.errors.full_messages_for(:draft_types_de_champ_private)).to include(invalid_drop_down_error_message) expect(procedure.errors.full_messages_for(:draft_types_de_champ_private)).to include(invalid_drop_down_error_message)
end end
it 'validates that types de champ private condition works types de champ public and private' do
end
end end
context 'when condition on champ private use public champ' do context 'when condition on champ private use public champ' do
include Logic include Logic
let(:types_de_champ_private) { [{ type: :text, condition: ds_eq(champ_value(1), constant(2)) }] } let(:types_de_champ_public) { [{ type: :decimal_number, stable_id: 1 }] }
let(:types_de_champ_public) { [{ type: :number, stable_id: 1 }] } let(:types_de_champ_private) { [{ type: :text, condition: ds_eq(champ_value(1), constant(2)), stable_id: 2 }] }
it 'validate without context' do it 'validate without context' do
expect(procedure.validate).to be_truthy procedure.validate
expect(procedure.errors.full_messages_for(:draft_types_de_champ_private)).to be_empty
end end
it 'validate with types_de_champ_private_editor' do it 'validate allows condition' do
expect(procedure.validate(:types_de_champ_private_editor)).to be_falsey procedure.validate(:types_de_champ_private_editor)
expect(procedure.errors.full_messages_for(:draft_types_de_champ_private)).to be_empty
end
end
context 'when condition on champ public use private champ' do
include Logic
let(:types_de_champ_public) { [{ type: :text, libelle: 'condition', condition: ds_eq(champ_value(1), constant(2)), stable_id: 2 }] }
let(:types_de_champ_private) { [{ type: :decimal_number, stable_id: 1 }] }
let(:error_on_condition) { "Le champ « condition » a une logique conditionnelle invalide" }
it 'validate without context' do
procedure.validate
expect(procedure.errors.full_messages_for(:draft_types_de_champ_public)).to be_empty
end
it 'validate prevent condition' do
procedure.validate(:types_de_champ_public_editor)
expect(procedure.errors.full_messages_for(:draft_types_de_champ_public)).to include(error_on_condition)
end end
end end
end end

View file

@ -39,8 +39,7 @@ describe ProcedureExportService do
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
subject subject
end end
expect(sql_count <= 62).to be_truthy
expect(sql_count <= 58).to be_truthy
dossier = dossiers.first dossier = dossiers.first