Skip to content

Commit

Permalink
fix(TypesDeChamp::ConditionValidator): allow to use types_de_champ_pu…
Browse files Browse the repository at this point in the history
…blic on condition for types_de_champ_private
  • Loading branch information
mfo committed Jun 10, 2024
1 parent d2ccea7 commit b0428bd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
11 changes: 7 additions & 4 deletions app/validators/types_de_champ/condition_validator.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
class TypesDeChamp::ConditionValidator < ActiveModel::EachValidator
def validate_each(procedure, attribute, types_de_champ)
public_tdcs = types_de_champ.to_a
.flat_map { _1.repetition? ? procedure.draft_revision.children_of(_1) : _1 }
tdcs = if attribute == :draft_types_de_champ_private
procedure.draft_revision.types_de_champ_for
else
procedure.draft_revision.types_de_champ_for(scope: :public)
end

public_tdcs
tdcs
.map.with_index
.filter_map { |tdc, i| tdc.condition? ? [tdc, i] : nil }
.map do |tdc, i|
[tdc, tdc.condition.errors(public_tdcs.take(i))]
[tdc, tdc.condition.errors(tdcs.take(i))]

Check warning on line 13 in app/validators/types_de_champ/condition_validator.rb

View check run for this annotation

Codecov / codecov/patch

app/validators/types_de_champ/condition_validator.rb#L13

Added line #L13 was not covered by tests
end
.filter { |_tdc, errors| errors.present? }
.each do |tdc, _error_hash|
Expand Down
28 changes: 23 additions & 5 deletions spec/models/procedure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,33 @@

context 'when condition on champ private use public champ' do
include Logic
let(:types_de_champ_private) { [{ type: :text, condition: ds_eq(champ_value(1), constant(2)) }] }
let(:types_de_champ_public) { [{ type: :number, stable_id: 1 }] }
let(:types_de_champ_public) { [{ type: :decimal_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
procedure.validate
expect(procedure.errors.full_messages_for(:draft_types_de_champ_private)).to be_empty
end

it 'validate allows condition' do
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
expect(procedure.validate).to be_truthy
procedure.validate
expect(procedure.errors.full_messages_for(:draft_types_de_champ_public)).to be_empty
end

it 'validate with types_de_champ_private_editor' do
expect(procedure.validate(:types_de_champ_private_editor)).to be_falsey
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
Expand Down

0 comments on commit b0428bd

Please sign in to comment.