From 469a50f19d72dc0aa2dab917188f43168dd24c64 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Tue, 17 Oct 2023 17:38:51 +0200 Subject: [PATCH] refactor(condition): move conditions_error_component --- .../champs_conditions_component.html.haml | 2 +- .../conditions_errors_component.rb | 16 ++++++------- .../conditions_errors_component.en.yml | 0 .../conditions_errors_component.fr.yml | 0 .../conditions_errors_component.html.haml | 0 .../routing_rules_component.html.haml | 3 +++ .../conditions_errors_component_spec.rb | 24 +++++++++---------- 7 files changed, 24 insertions(+), 21 deletions(-) rename app/components/{types_de_champ_editor => conditions}/conditions_errors_component.rb (80%) rename app/components/{types_de_champ_editor => conditions}/conditions_errors_component/conditions_errors_component.en.yml (100%) rename app/components/{types_de_champ_editor => conditions}/conditions_errors_component/conditions_errors_component.fr.yml (100%) rename app/components/{types_de_champ_editor => conditions}/conditions_errors_component/conditions_errors_component.html.haml (100%) diff --git a/app/components/conditions/champs_conditions_component/champs_conditions_component.html.haml b/app/components/conditions/champs_conditions_component/champs_conditions_component.html.haml index f9e45c340..aa19e3b26 100644 --- a/app/components/conditions/champs_conditions_component/champs_conditions_component.html.haml +++ b/app/components/conditions/champs_conditions_component/champs_conditions_component.html.haml @@ -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 diff --git a/app/components/types_de_champ_editor/conditions_errors_component.rb b/app/components/conditions/conditions_errors_component.rb similarity index 80% rename from app/components/types_de_champ_editor/conditions_errors_component.rb rename to app/components/conditions/conditions_errors_component.rb index d3500ab75..335716c97 100644 --- a/app/components/types_de_champ_editor/conditions_errors_component.rb +++ b/app/components/conditions/conditions_errors_component.rb @@ -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 diff --git a/app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.en.yml b/app/components/conditions/conditions_errors_component/conditions_errors_component.en.yml similarity index 100% rename from app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.en.yml rename to app/components/conditions/conditions_errors_component/conditions_errors_component.en.yml diff --git a/app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.fr.yml b/app/components/conditions/conditions_errors_component/conditions_errors_component.fr.yml similarity index 100% rename from app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.fr.yml rename to app/components/conditions/conditions_errors_component/conditions_errors_component.fr.yml diff --git a/app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.html.haml b/app/components/conditions/conditions_errors_component/conditions_errors_component.html.haml similarity index 100% rename from app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.html.haml rename to app/components/conditions/conditions_errors_component/conditions_errors_component.html.haml diff --git a/app/components/conditions/routing_rules_component/routing_rules_component.html.haml b/app/components/conditions/routing_rules_component/routing_rules_component.html.haml index 286f7508d..9830d9864 100644 --- a/app/components/conditions/routing_rules_component/routing_rules_component.html.haml +++ b/app/components/conditions/routing_rules_component/routing_rules_component.html.haml @@ -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 diff --git a/spec/components/types_de_champ_editor/conditions_errors_component_spec.rb b/spec/components/types_de_champ_editor/conditions_errors_component_spec.rb index e829bc305..8bc2ad6e6 100644 --- a/spec/components/types_de_champ_editor/conditions_errors_component_spec.rb +++ b/spec/components/types_de_champ_editor/conditions_errors_component_spec.rb @@ -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("« n’est 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") }