refactor(condition): move conditions_error_component

This commit is contained in:
Eric Leroy-Terquem 2023-10-17 17:38:51 +02:00
parent cf80fd03e1
commit 469a50f19d
7 changed files with 24 additions and 21 deletions

View file

@ -6,7 +6,7 @@
Logique conditionnelle
= logic_conditionnel_button
= render TypesDeChampEditor::ConditionsErrorsComponent.new(conditions: condition_per_row, upper_tdcs: @source_tdcs)
= render Conditions::ConditionsErrorsComponent.new(conditions: condition_per_row, source_tdcs: @source_tdcs)
- if @condition.present?
%table.condition-table.mt-2.width-100

View file

@ -1,13 +1,13 @@
class TypesDeChampEditor::ConditionsErrorsComponent < ApplicationComponent
def initialize(conditions:, upper_tdcs:)
@conditions, @upper_tdcs = conditions, upper_tdcs
class Conditions::ConditionsErrorsComponent < ApplicationComponent
def initialize(conditions:, source_tdcs:)
@conditions, @source_tdcs = conditions, source_tdcs
end
private
def errors
errors = @conditions
.flat_map { |condition| condition.errors(@upper_tdcs) }
.flat_map { |condition| condition.errors(@source_tdcs) }
.uniq
# if a tdc is not available (has been removed for example)
@ -34,13 +34,13 @@ class TypesDeChampEditor::ConditionsErrorsComponent < ApplicationComponent
in { type: :incompatible, stable_id: nil }
t('not_available', scope: '.errors')
in { type: :unmanaged, stable_id: stable_id }
targeted_champ = @upper_tdcs.find { |tdc| tdc.stable_id == stable_id }
targeted_champ = @source_tdcs.find { |tdc| tdc.stable_id == stable_id }
t('unmanaged',
scope: '.errors',
libelle: targeted_champ.libelle,
type_champ: t(targeted_champ.type_champ, scope: 'activerecord.attributes.type_de_champ.type_champs')&.downcase)
in { type: :incompatible, stable_id: stable_id, right: right, operator_name: operator_name }
targeted_champ = @upper_tdcs.find { |tdc| tdc.stable_id == stable_id }
targeted_champ = @source_tdcs.find { |tdc| tdc.stable_id == stable_id }
t('incompatible', scope: '.errors',
libelle: targeted_champ.libelle,
type_champ: t(targeted_champ.type_champ, scope: 'activerecord.attributes.type_de_champ.type_champs')&.downcase,
@ -50,7 +50,7 @@ class TypesDeChampEditor::ConditionsErrorsComponent < ApplicationComponent
t('required_number', scope: '.errors',
operator: t(operator_name, scope: 'logic.operators'))
in { type: :not_included, stable_id: stable_id, right: right }
targeted_champ = @upper_tdcs.find { |tdc| tdc.stable_id == stable_id }
targeted_champ = @source_tdcs.find { |tdc| tdc.stable_id == stable_id }
t('not_included', scope: '.errors',
libelle: targeted_champ.libelle,
right: right.to_s.downcase)
@ -67,7 +67,7 @@ class TypesDeChampEditor::ConditionsErrorsComponent < ApplicationComponent
def render?
@conditions
.filter { |condition| condition.errors(@upper_tdcs).present? }
.filter { |condition| condition.errors(@source_tdcs).present? }
.present?
end
end

View file

@ -9,6 +9,9 @@
%p.fr-mb-1w.fr-badge.fr-badge--warning.fr-badge--sm règle invalide
- elsif @groupe_instructeur.non_unique_rule?
%p.fr-mb-1w.fr-badge.fr-badge--warning.fr-badge--sm règle déjà attribuée à #{@groupe_instructeur.groups_with_same_rule}
= render Conditions::ConditionsErrorsComponent.new(conditions: condition_per_row, source_tdcs: @source_tdcs)
%table.condition-table.mt-2
%thead
%tr

View file

@ -1,11 +1,11 @@
describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
describe Conditions::ConditionsErrorsComponent, type: :component do
include Logic
describe 'render' do
let(:conditions) { [] }
let(:upper_tdcs) { [] }
let(:source_tdcs) { [] }
before { render_inline(described_class.new(conditions: conditions, upper_tdcs: upper_tdcs)) }
before { render_inline(described_class.new(conditions: conditions, source_tdcs: source_tdcs)) }
context 'when there are no condition' do
it { expect(page).to have_no_css('.errors-summary') }
@ -23,7 +23,7 @@ describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
context 'when the targeted_champ is unmanaged' do
let(:tdc) { create(:type_de_champ_address) }
let(:upper_tdcs) { [tdc] }
let(:source_tdcs) { [tdc] }
let(:conditions) { [ds_eq(champ_value(tdc.stable_id), constant(1))] }
it do
@ -34,7 +34,7 @@ describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
context 'when the types mismatch' do
let(:tdc) { create(:type_de_champ_integer_number) }
let(:upper_tdcs) { [tdc] }
let(:source_tdcs) { [tdc] }
let(:conditions) { [ds_eq(champ_value(tdc.stable_id), constant('a'))] }
it { expect(page).to have_content("Le champ « #{tdc.libelle} » est de type « nombre entier ». Il ne peut pas être égal à « a ».") }
@ -42,7 +42,7 @@ describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
context 'when a number operator is applied on not a number' do
let(:tdc) { create(:type_de_champ_multiple_drop_down_list) }
let(:upper_tdcs) { [tdc] }
let(:source_tdcs) { [tdc] }
let(:conditions) { [greater_than(champ_value(tdc.stable_id), constant('a text'))] }
it { expect(page).to have_content("« Supérieur à » ne s'applique qu'à des nombres.") }
@ -50,7 +50,7 @@ describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
context 'when the include operator is applied on a list' do
let(:tdc) { create(:type_de_champ_integer_number) }
let(:upper_tdcs) { [tdc] }
let(:source_tdcs) { [tdc] }
let(:conditions) { [ds_include(champ_value(tdc.stable_id), constant('a text'))] }
it { expect(page).to have_content("Lʼopérateur « inclus » ne s'applique qu'au choix simple ou multiple.") }
@ -58,7 +58,7 @@ describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
context 'when a choice is not in a drop_down' do
let(:tdc) { create(:type_de_champ_drop_down_list) }
let(:upper_tdcs) { [tdc] }
let(:source_tdcs) { [tdc] }
let(:conditions) { [ds_eq(champ_value(tdc.stable_id), constant('another choice'))] }
it { expect(page).to have_content("« another choice » ne fait pas partie de « #{tdc.libelle} ».") }
@ -66,7 +66,7 @@ describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
context 'when a choice is not in a multiple_drop_down' do
let(:tdc) { create(:type_de_champ_multiple_drop_down_list) }
let(:upper_tdcs) { [tdc] }
let(:source_tdcs) { [tdc] }
let(:conditions) { [ds_include(champ_value(tdc.stable_id), constant('another choice'))] }
it { expect(page).to have_content("« another choice » ne fait pas partie de « #{tdc.libelle} ».") }
@ -74,7 +74,7 @@ describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
context 'when an eq operator applies to a multiple_drop_down' do
let(:tdc) { create(:type_de_champ_multiple_drop_down_list) }
let(:upper_tdcs) { [tdc] }
let(:source_tdcs) { [tdc] }
let(:conditions) { [ds_eq(champ_value(tdc.stable_id), constant(tdc.drop_down_list_enabled_non_empty_options.first))] }
it { expect(page).to have_content("« est » ne s'applique pas au choix multiple.") }
@ -82,7 +82,7 @@ describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
context 'when an not_eq operator applies to a multiple_drop_down' do
let(:tdc) { create(:type_de_champ_multiple_drop_down_list) }
let(:upper_tdcs) { [tdc] }
let(:source_tdcs) { [tdc] }
let(:conditions) { [ds_not_eq(champ_value(tdc.stable_id), constant(tdc.drop_down_list_enabled_non_empty_options.first))] }
it { expect(page).to have_content("« nest pas » ne s'applique pas au choix multiple.") }
@ -92,7 +92,7 @@ describe TypesDeChampEditor::ConditionsErrorsComponent, type: :component do
# Cf https://demarches-simplifiees.sentry.io/issues/3625488398/events/53164e105bc94d55a004d69f96d58fb2/?project=1429550
# However maybe we should not have empty at left with still a constant at right
let(:tdc) { create(:type_de_champ_integer_number) }
let(:upper_tdcs) { [tdc] }
let(:source_tdcs) { [tdc] }
let(:conditions) { [ds_eq(empty, constant('a text'))] }
it { expect(page).to have_content("Un champ cible n'est plus disponible") }