From 3eadff9111146f7d5394bafdd3b989fd2bee7b73 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 7 Jul 2022 15:22:23 +0200 Subject: [PATCH 01/13] refactor spec --- .../types_de_champ_controller_spec.rb | 67 +++++++------------ 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/spec/controllers/administrateurs/types_de_champ_controller_spec.rb b/spec/controllers/administrateurs/types_de_champ_controller_spec.rb index f4b95fae5..ecad35a6c 100644 --- a/spec/controllers/administrateurs/types_de_champ_controller_spec.rb +++ b/spec/controllers/administrateurs/types_de_champ_controller_spec.rb @@ -1,65 +1,48 @@ describe Administrateurs::TypesDeChampController, type: :controller do - let(:admin) { create(:administrateur) } + let(:procedure) { create(:procedure) } - describe '#types_de_champs editor' do - let(:procedure) { create(:procedure) } + before { sign_in(procedure.administrateurs.first.user) } - before do - admin.procedures << procedure - sign_in(admin.user) + describe '#create' do + let(:params) { default_params } + + let(:default_params) do + { + procedure_id: procedure.id, + type_de_champ: { + type_champ: type_champ, + libelle: 'Nouveau champ', + private: false, + placeholder: "custom placeholder" + } + } end - let(:type_champ) { TypeDeChamp.type_champs.fetch(:text) } + subject { post :create, params: params, format: :turbo_stream } context "create type_de_champ text" do - before do - post :create, params: { - procedure_id: procedure.id, - type_de_champ: { - type_champ: type_champ, - libelle: 'Nouveau champ', - private: false, - placeholder: "custom placeholder" - } - }, format: :turbo_stream - end + let(:type_champ) { TypeDeChamp.type_champs.fetch(:text) } - it { expect(response).to have_http_status(:ok) } + it { is_expected.to have_http_status(:ok) } end context "validate type_de_champ linked_drop_down_list" do let(:type_champ) { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) } - before do - post :create, params: { - procedure_id: procedure.id, - type_de_champ: { - type_champ: type_champ, - libelle: 'Nouveau champ', - private: false - } - }, format: :turbo_stream + it do + is_expected.to have_http_status(:ok) + expect(flash.alert).to eq(nil) end - - it { expect(response).to have_http_status(:ok) } end context "create type_de_champ linked_drop_down_list" do let(:type_champ) { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) } + let(:params) { default_params.deep_merge(type_de_champ: { drop_down_list_value: '--value--' }) } - before do - post :create, params: { - procedure_id: procedure.id, - type_de_champ: { - type_champ: type_champ, - libelle: 'Nouveau champ', - drop_down_list_value: '--value--', - private: false - } - }, format: :turbo_stream + it do + is_expected.to have_http_status(:ok) + expect(flash.alert).to eq(nil) end - - it { expect(response).to have_http_status(:ok) } end end end From 256d4e4fdabfe94c35c91f9735a628901bc9e272 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 11 Jul 2022 10:14:18 +0200 Subject: [PATCH 02/13] refactor: reorder -> renumber --- app/models/procedure_revision.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index e244ace8c..5dca08db6 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -59,7 +59,7 @@ class ProcedureRevision < ApplicationRecord h = { type_de_champ: tdc, parent_id: parent_id, position: position } coordinate = revision_types_de_champ.create!(h) - reorder(coordinate.reload.siblings) + renumber(coordinate.reload.siblings) end # they are not aware of the addition @@ -88,7 +88,7 @@ class ProcedureRevision < ApplicationRecord siblings.insert(position, siblings.delete_at(siblings.index(coordinate))) - reorder(siblings) + renumber(siblings) coordinate.reload coordinate @@ -107,7 +107,7 @@ class ProcedureRevision < ApplicationRecord types_de_champ_public.reset types_de_champ_private.reset - reorder(coordinate.siblings) + renumber(coordinate.siblings) coordinate end @@ -224,7 +224,7 @@ class ProcedureRevision < ApplicationRecord [coordinate, coordinate.type_de_champ] end - def reorder(siblings) + def renumber(siblings) siblings.to_a.compact.each.with_index do |sibling, position| sibling.update_column(:position, position) end From e9cf41a00d5e50ceaa1e7807eb3818fbbaa81953 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 29 Jun 2022 12:48:33 +0200 Subject: [PATCH 03/13] add system spec --- spec/system/administrateurs/condition_spec.rb | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 spec/system/administrateurs/condition_spec.rb diff --git a/spec/system/administrateurs/condition_spec.rb b/spec/system/administrateurs/condition_spec.rb new file mode 100644 index 000000000..698c6d03e --- /dev/null +++ b/spec/system/administrateurs/condition_spec.rb @@ -0,0 +1,127 @@ +describe 'As an administrateur I can edit types de champ condition', js: true do + include Logic + + let(:administrateur) { procedure.administrateurs.first } + let(:procedure) do + create(:procedure).tap do |p| + p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'age') + # private + p.draft_revision.add_type_de_champ(type_champ: :boolean, libelle: 'bon dossier', private: true) + + p.draft_revision.add_type_de_champ(type_champ: :text, libelle: 'nom du parent') + end + end + + let(:first_tdc) { procedure.draft_revision.types_de_champ.first } + let(:second_tdc) { procedure.draft_revision.types_de_champ.second } + + before do + login_as administrateur.user, scope: :user + visit champs_admin_procedure_path(procedure) + end + + scenario "adding a new condition" do + within '.type-de-champ:nth-child(2)' do + click_on 'cliquer pour activer' + + within '.condition-table tbody tr:nth-child(1)' do + expect(page).to have_select('type_de_champ[condition_form][rows][][targeted_champ]', options: ['Sélectionner', 'age']) + + within('.target') { select('age') } + within('.operator') { select('Supérieur ou égal à') } + within('.value') { fill_in with: 18 } + end + end + + expected_condition = greater_than_eq(champ_value(first_tdc.stable_id), constant(18)) + wait_until { second_tdc.reload.condition == expected_condition } + end + + scenario "the first type de champ is removed" do + within '.type-de-champ:nth-child(1)' do + click_on 'Supprimer' + page.accept_alert + end + + # the condition table is deleted + expect(page).to have_no_content('Logique conditionnelle') + end + + context 'with a preexisting condition' do + before do + second_tdc.update(condition: greater_than_eq(champ_value(first_tdc.stable_id), constant(18))) + + page.refresh + end + + scenario "removing all conditions" do + within '.type-de-champ:nth-child(2)' do + click_on 'cliquer pour désactiver' + page.accept_alert + + # the condition table is deleted + expect(page).to have_no_table + end + end + + scenario "removing a condition" do + within '.type-de-champ:nth-child(2)' do + within '.condition-table tbody tr:nth-child(1)' do + within('.delete-column') { click_on 'Supprimer la ligne' } + end + + # the condition table is deleted + expect(page).to have_no_table + end + end + + scenario "adding a second row" do + within '.type-de-champ:nth-child(2)' do + click_on 'Ajouter une condition' + + # the condition table has 2 rows + within '.condition-table tbody' do + expect(page).to have_selector('tr', count: 2) + end + end + end + + scenario "changing target champ to a not managed type" do + expect(page).to have_no_selector('.condition-error') + + within '.type-de-champ:nth-child(1)' do + select('Départements', from: 'Type de champ') + end + + within '.type-de-champ:nth-child(2)' do + expect(page).to have_selector('.condition-error') + end + end + + scenario "moving a target champ below the condition" do + expect(page).to have_no_selector('.condition-error') + + within '.type-de-champ:nth-child(1)' do + click_on 'Déplacer le champ vers le bas' + end + + # the now first champ has an error + within '.type-de-champ:nth-child(1)' do + expect(page).to have_selector('.condition-error') + end + end + + scenario "moving the condition champ above the target" do + expect(page).to have_no_selector('.condition-error') + + within '.type-de-champ:nth-child(2)' do + click_on 'Déplacer le champ vers le haut' + end + + # the now first champ has an error + within '.type-de-champ:nth-child(1)' do + expect(page).to have_selector('.condition-error') + end + end + end +end From 2df9d8ba9891836113ef65295a16d7cf74ecf1fc Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 8 Jul 2022 11:04:58 +0200 Subject: [PATCH 04/13] use condition_condition_component in editor --- .../block_component/block_component.html.haml | 4 ++-- app/components/types_de_champ_editor/champ_component.rb | 3 ++- .../champ_component/champ_component.html.haml | 2 ++ .../types_de_champ/_insert.turbo_stream.haml | 9 +++++++-- .../types_de_champ/create.turbo_stream.haml | 2 +- .../types_de_champ/destroy.turbo_stream.haml | 5 +++++ .../types_de_champ/move_down.turbo_stream.haml | 2 +- .../types_de_champ/move_up.turbo_stream.haml | 2 +- .../types_de_champ/update.turbo_stream.haml | 7 ++++++- 9 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/components/types_de_champ_editor/block_component/block_component.html.haml b/app/components/types_de_champ_editor/block_component/block_component.html.haml index a1b9a7eed..59d07c80a 100644 --- a/app/components/types_de_champ_editor/block_component/block_component.html.haml +++ b/app/components/types_de_champ_editor/block_component/block_component.html.haml @@ -1,3 +1,3 @@ %ul.types-de-champ-block{ id: block_id, data: sortable_options } - - @coordinates.each do |coordinate| - = render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate) + - @coordinates.each.with_index do |coordinate, i| + = render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate, upper_coordinates: @coordinates.take(i)) diff --git a/app/components/types_de_champ_editor/champ_component.rb b/app/components/types_de_champ_editor/champ_component.rb index 6cd02e2ee..0dd631504 100644 --- a/app/components/types_de_champ_editor/champ_component.rb +++ b/app/components/types_de_champ_editor/champ_component.rb @@ -1,7 +1,8 @@ class TypesDeChampEditor::ChampComponent < ApplicationComponent - def initialize(coordinate:, focused: false) + def initialize(coordinate:, upper_coordinates:, focused: false) @coordinate = coordinate @focused = focused + @upper_coordinates = upper_coordinates end private diff --git a/app/components/types_de_champ_editor/champ_component/champ_component.html.haml b/app/components/types_de_champ_editor/champ_component/champ_component.html.haml index 5d455e87d..ec2aaff5d 100644 --- a/app/components/types_de_champ_editor/champ_component/champ_component.html.haml +++ b/app/components/types_de_champ_editor/champ_component/champ_component.html.haml @@ -83,3 +83,5 @@ .editor-block.flex-grow.cell = render TypesDeChampEditor::BlockComponent.new(block: coordinate, coordinates: coordinate.revision_types_de_champ) = render TypesDeChampEditor::AddChampButtonComponent.new(revision: coordinate.revision, parent: coordinate, is_annotation: coordinate.private?) + + = render(TypesDeChampEditor::ConditionsComponent.new(tdc: type_de_champ, upper_tdcs: @upper_coordinates.map(&:type_de_champ), procedure_id: procedure.id)) diff --git a/app/views/administrateurs/types_de_champ/_insert.turbo_stream.haml b/app/views/administrateurs/types_de_champ/_insert.turbo_stream.haml index d6c3e2276..dcfd571ce 100644 --- a/app/views/administrateurs/types_de_champ/_insert.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/_insert.turbo_stream.haml @@ -2,8 +2,13 @@ - if sibling.present? = turbo_stream.after dom_id(sibling, :type_de_champ_editor) do - = render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate, focused: true) + = render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate, upper_coordinates: upper_coordinates, focused: true) - else = turbo_stream.prepend dom_id(coordinate.block, :types_de_champ_editor_block) do - = render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate, focused: true) + = render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate, upper_coordinates: upper_coordinates, focused: true) = turbo_stream.dispatch 'sortable:sort' + +- if @lower_coordinates_and_dep.present? + - @lower_coordinates_and_dep.each do |other_coordinate, other_upper_coordinates| + = turbo_stream.morph dom_id(other_coordinate, :type_de_champ_editor) do + = render TypesDeChampEditor::ChampComponent.new(coordinate: other_coordinate, upper_coordinates: other_upper_coordinates) diff --git a/app/views/administrateurs/types_de_champ/create.turbo_stream.haml b/app/views/administrateurs/types_de_champ/create.turbo_stream.haml index 9f08077a1..958466e7f 100644 --- a/app/views/administrateurs/types_de_champ/create.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/create.turbo_stream.haml @@ -1,2 +1,2 @@ - if @coordinate&.type_de_champ&.valid? - = render partial: 'insert', locals: { coordinate: @coordinate } + = render partial: 'insert', locals: { coordinate: @coordinate, upper_coordinates: @upper_coordinates } diff --git a/app/views/administrateurs/types_de_champ/destroy.turbo_stream.haml b/app/views/administrateurs/types_de_champ/destroy.turbo_stream.haml index 88038a9a9..e8c8de1fc 100644 --- a/app/views/administrateurs/types_de_champ/destroy.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/destroy.turbo_stream.haml @@ -2,3 +2,8 @@ = turbo_stream.dispatch 'sortable:sort' = turbo_stream.morph dom_id(@coordinate.revision, :estimated_fill_duration) do = render TypesDeChampEditor::EstimatedFillDurationComponent.new(revision: @coordinate.revision, is_annotation: @coordinate.private?) + +- if @lower_coordinates_and_dep.present? + - @lower_coordinates_and_dep.each do |other_coordinate, other_upper_coordinates| + = turbo_stream.morph dom_id(other_coordinate, :type_de_champ_editor) do + = render TypesDeChampEditor::ChampComponent.new(coordinate: other_coordinate, upper_coordinates: other_upper_coordinates) diff --git a/app/views/administrateurs/types_de_champ/move_down.turbo_stream.haml b/app/views/administrateurs/types_de_champ/move_down.turbo_stream.haml index e17f946fc..893fb25d8 100644 --- a/app/views/administrateurs/types_de_champ/move_down.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/move_down.turbo_stream.haml @@ -1,2 +1,2 @@ = turbo_stream.remove dom_id(@coordinate, :type_de_champ_editor) -= render partial: 'insert', locals: { coordinate: @coordinate } += render partial: 'insert', locals: { coordinate: @coordinate, upper_coordinates: @upper_coordinates } diff --git a/app/views/administrateurs/types_de_champ/move_up.turbo_stream.haml b/app/views/administrateurs/types_de_champ/move_up.turbo_stream.haml index e17f946fc..893fb25d8 100644 --- a/app/views/administrateurs/types_de_champ/move_up.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/move_up.turbo_stream.haml @@ -1,2 +1,2 @@ = turbo_stream.remove dom_id(@coordinate, :type_de_champ_editor) -= render partial: 'insert', locals: { coordinate: @coordinate } += render partial: 'insert', locals: { coordinate: @coordinate, upper_coordinates: @upper_coordinates } diff --git a/app/views/administrateurs/types_de_champ/update.turbo_stream.haml b/app/views/administrateurs/types_de_champ/update.turbo_stream.haml index 5809cd5f2..1f7de5784 100644 --- a/app/views/administrateurs/types_de_champ/update.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/update.turbo_stream.haml @@ -1,5 +1,10 @@ - if @coordinate&.type_de_champ&.valid? = turbo_stream.morph dom_id(@coordinate, :type_de_champ_editor) do - = render TypesDeChampEditor::ChampComponent.new(coordinate: @coordinate) + = render TypesDeChampEditor::ChampComponent.new(coordinate: @coordinate, upper_coordinates: @upper_coordinates) = turbo_stream.morph dom_id(@coordinate.revision, :estimated_fill_duration) do = render TypesDeChampEditor::EstimatedFillDurationComponent.new(revision: @coordinate.revision, is_annotation: @coordinate.private?) + +- if @lower_coordinates_and_dep.present? + - @lower_coordinates_and_dep.each do |other_coordinate, other_upper_coordinates| + = turbo_stream.morph dom_id(other_coordinate, :type_de_champ_editor) do + = render TypesDeChampEditor::ChampComponent.new(coordinate: other_coordinate, upper_coordinates: other_upper_coordinates) From 5bee34cc34c4e74f821ed5012e5a6ce3abe8efc2 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 11 Jul 2022 17:24:39 +0200 Subject: [PATCH 05/13] refactor: before_action --- app/controllers/administrateurs/types_de_champ_controller.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index bfe2b3f9f..93a4ad744 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -1,7 +1,6 @@ module Administrateurs class TypesDeChampController < AdministrateurController - before_action :retrieve_procedure, only: [:create, :update, :move, :move_up, :move_down, :destroy] - before_action :procedure_revisable?, only: [:create, :update, :move, :move_up, :move_down, :destroy] + before_action :retrieve_procedure, :procedure_revisable? def create type_de_champ = @procedure.draft_revision.add_type_de_champ(type_de_champ_create_params) From c80a820054419afbd34ef8a6b731b34d40417392 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 11 Jul 2022 21:15:00 +0200 Subject: [PATCH 06/13] expose properties after_all --- app/components/types_de_champ_editor/champ_component.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/components/types_de_champ_editor/champ_component.rb b/app/components/types_de_champ_editor/champ_component.rb index 0dd631504..40dd87827 100644 --- a/app/components/types_de_champ_editor/champ_component.rb +++ b/app/components/types_de_champ_editor/champ_component.rb @@ -1,4 +1,6 @@ class TypesDeChampEditor::ChampComponent < ApplicationComponent + attr_reader :coordinate, :upper_coordinates + def initialize(coordinate:, upper_coordinates:, focused: false) @coordinate = coordinate @focused = focused @@ -7,7 +9,6 @@ class TypesDeChampEditor::ChampComponent < ApplicationComponent private - attr_reader :coordinate delegate :type_de_champ, :revision, :procedure, to: :coordinate def can_be_mandatory? From 2da0aa6525ea21291f7296e7d98e3c746ca2fc59 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 11 Jul 2022 21:17:43 +0200 Subject: [PATCH 07/13] refactor: add draft helper --- .../types_de_champ_controller.rb | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index 93a4ad744..ede6f15cb 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -3,10 +3,10 @@ module Administrateurs before_action :retrieve_procedure, :procedure_revisable? def create - type_de_champ = @procedure.draft_revision.add_type_de_champ(type_de_champ_create_params) + type_de_champ = draft.add_type_de_champ(type_de_champ_create_params) if type_de_champ.valid? - @coordinate = @procedure.draft_revision.coordinate_for(type_de_champ) + @coordinate = draft.coordinate_for(type_de_champ) reset_procedure flash.notice = "Formulaire enregistré" else @@ -15,11 +15,11 @@ module Administrateurs end def update - type_de_champ = @procedure.draft_revision.find_and_ensure_exclusive_use(params[:stable_id]) + type_de_champ = draft.find_and_ensure_exclusive_use(params[:stable_id]) if type_de_champ.update(type_de_champ_update_params) if params[:should_render] - @coordinate = @procedure.draft_revision.coordinate_for(type_de_champ) + @coordinate = draft.coordinate_for(type_de_champ) end reset_procedure flash.notice = "Formulaire enregistré" @@ -30,21 +30,21 @@ module Administrateurs def move flash.notice = "Formulaire enregistré" - @procedure.draft_revision.move_type_de_champ(params[:stable_id], params[:position].to_i) + draft.move_type_de_champ(params[:stable_id], params[:position].to_i) end def move_up flash.notice = "Formulaire enregistré" - @coordinate = @procedure.draft_revision.move_up_type_de_champ(params[:stable_id]) + @coordinate = draft.move_up_type_de_champ(params[:stable_id]) end def move_down flash.notice = "Formulaire enregistré" - @coordinate = @procedure.draft_revision.move_down_type_de_champ(params[:stable_id]) + @coordinate = draft.move_down_type_de_champ(params[:stable_id]) end def destroy - @coordinate = @procedure.draft_revision.remove_type_de_champ(params[:stable_id]) + @coordinate = draft.remove_type_de_champ(params[:stable_id]) reset_procedure flash.notice = "Formulaire enregistré" end @@ -81,5 +81,9 @@ module Administrateurs :znieff ]) end + + def draft + @procedure.draft_revision + end end end From 2fee0ddfd4de740c845231f0b52ca65afc0d6fb4 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 11 Jul 2022 21:18:08 +0200 Subject: [PATCH 08/13] feature: type_de_champ_controller --- .../types_de_champ_controller.rb | 35 ++++- app/models/procedure_revision.rb | 13 +- .../types_de_champ/_insert.turbo_stream.haml | 29 +++-- .../types_de_champ/create.turbo_stream.haml | 3 +- .../types_de_champ/destroy.turbo_stream.haml | 10 +- .../move_down.turbo_stream.haml | 3 +- .../types_de_champ/move_up.turbo_stream.haml | 3 +- .../types_de_champ/update.turbo_stream.haml | 11 +- .../types_de_champ_controller_spec.rb | 121 +++++++++++++++++- 9 files changed, 181 insertions(+), 47 deletions(-) diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index ede6f15cb..d0618d821 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -7,6 +7,9 @@ module Administrateurs if type_de_champ.valid? @coordinate = draft.coordinate_for(type_de_champ) + @created = champ_component_from(@coordinate, focused: true) + @morphed = champ_components_starting_at(@coordinate.position + 1) + reset_procedure flash.notice = "Formulaire enregistré" else @@ -18,9 +21,9 @@ module Administrateurs type_de_champ = draft.find_and_ensure_exclusive_use(params[:stable_id]) if type_de_champ.update(type_de_champ_update_params) - if params[:should_render] - @coordinate = draft.coordinate_for(type_de_champ) - end + @coordinate = draft.coordinate_for(type_de_champ) + @morphed = champ_components_starting_at(@coordinate.position) + reset_procedure flash.notice = "Formulaire enregistré" else @@ -36,21 +39,47 @@ module Administrateurs def move_up flash.notice = "Formulaire enregistré" @coordinate = draft.move_up_type_de_champ(params[:stable_id]) + @destroyed = @coordinate + @created = champ_component_from(@coordinate) + # update the one component below + @morphed = champ_components_starting_at(@coordinate.position + 1).take(1) end def move_down flash.notice = "Formulaire enregistré" @coordinate = draft.move_down_type_de_champ(params[:stable_id]) + @destroyed = @coordinate + @created = champ_component_from(@coordinate) + # update the one component above + @morphed = champ_components_starting_at(@coordinate.position - 1).take(1) end def destroy @coordinate = draft.remove_type_de_champ(params[:stable_id]) reset_procedure flash.notice = "Formulaire enregistré" + + @destroyed = @coordinate + @morphed = champ_components_starting_at(@coordinate.position) end private + def champ_components_starting_at(position) + draft + .coordinates_starting_at(position) + .lazy + .map { |c| champ_component_from(c) } + end + + def champ_component_from(coordinate, focused: false) + TypesDeChampEditor::ChampComponent.new( + coordinate: coordinate, + upper_coordinates: draft.upper_coordinates(coordinate.position), + focused: focused + ) + end + def type_de_champ_create_params params .required(:type_de_champ) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 5dca08db6..1721f046c 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -197,6 +197,14 @@ class ProcedureRevision < ApplicationRecord revision_types_de_champ.find_by!(type_de_champ: tdc) end + def upper_coordinates(position) + revision_types_de_champ_public.filter { |c| c.position < position } + end + + def coordinates_starting_at(position) + revision_types_de_champ_public.reload.filter { |c| position <= c.position } + end + private def compute_estimated_fill_duration @@ -482,7 +490,8 @@ class ProcedureRevision < ApplicationRecord types_de_champ_public .map.with_index .filter_map { |tdc, i| tdc.condition.present? ? [tdc, i] : nil } - .flat_map { |tdc, i| tdc.condition.errors(stable_ids.take(i)) } - .each { |message| errors.add(:condition, message) } + .map { |tdc, i| [tdc, tdc.condition.errors(stable_ids.take(i))] } + .filter { |_tdc, errors| errors.present? } + .each { |tdc, message| errors.add(:condition, message, type_de_champ: tdc) } end end diff --git a/app/views/administrateurs/types_de_champ/_insert.turbo_stream.haml b/app/views/administrateurs/types_de_champ/_insert.turbo_stream.haml index dcfd571ce..9001c602b 100644 --- a/app/views/administrateurs/types_de_champ/_insert.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/_insert.turbo_stream.haml @@ -1,14 +1,19 @@ -- sibling = coordinate.previous_sibling +- if @destroyed.present? + = turbo_stream.remove dom_id(@destroyed, :type_de_champ_editor) + +- if @created.present? + - if @created.coordinate.previous_sibling.present? + = turbo_stream.after dom_id(@created.coordinate.previous_sibling, :type_de_champ_editor) do + = render @created + - else + = turbo_stream.prepend dom_id(@created.coordinate.block, :types_de_champ_editor_block) do + = render @created + +- @morphed&.each do |champ_component| + = turbo_stream.morph dom_id(champ_component.coordinate, :type_de_champ_editor) do + = render champ_component + += turbo_stream.morph dom_id(@coordinate.revision, :estimated_fill_duration) do + = render TypesDeChampEditor::EstimatedFillDurationComponent.new(revision: @coordinate.revision, is_annotation: @coordinate.private?) -- if sibling.present? - = turbo_stream.after dom_id(sibling, :type_de_champ_editor) do - = render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate, upper_coordinates: upper_coordinates, focused: true) -- else - = turbo_stream.prepend dom_id(coordinate.block, :types_de_champ_editor_block) do - = render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate, upper_coordinates: upper_coordinates, focused: true) = turbo_stream.dispatch 'sortable:sort' - -- if @lower_coordinates_and_dep.present? - - @lower_coordinates_and_dep.each do |other_coordinate, other_upper_coordinates| - = turbo_stream.morph dom_id(other_coordinate, :type_de_champ_editor) do - = render TypesDeChampEditor::ChampComponent.new(coordinate: other_coordinate, upper_coordinates: other_upper_coordinates) diff --git a/app/views/administrateurs/types_de_champ/create.turbo_stream.haml b/app/views/administrateurs/types_de_champ/create.turbo_stream.haml index 958466e7f..500899d89 100644 --- a/app/views/administrateurs/types_de_champ/create.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/create.turbo_stream.haml @@ -1,2 +1 @@ -- if @coordinate&.type_de_champ&.valid? - = render partial: 'insert', locals: { coordinate: @coordinate, upper_coordinates: @upper_coordinates } += render partial: 'insert' diff --git a/app/views/administrateurs/types_de_champ/destroy.turbo_stream.haml b/app/views/administrateurs/types_de_champ/destroy.turbo_stream.haml index e8c8de1fc..500899d89 100644 --- a/app/views/administrateurs/types_de_champ/destroy.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/destroy.turbo_stream.haml @@ -1,9 +1 @@ -= turbo_stream.remove dom_id(@coordinate, :type_de_champ_editor) -= turbo_stream.dispatch 'sortable:sort' -= turbo_stream.morph dom_id(@coordinate.revision, :estimated_fill_duration) do - = render TypesDeChampEditor::EstimatedFillDurationComponent.new(revision: @coordinate.revision, is_annotation: @coordinate.private?) - -- if @lower_coordinates_and_dep.present? - - @lower_coordinates_and_dep.each do |other_coordinate, other_upper_coordinates| - = turbo_stream.morph dom_id(other_coordinate, :type_de_champ_editor) do - = render TypesDeChampEditor::ChampComponent.new(coordinate: other_coordinate, upper_coordinates: other_upper_coordinates) += render partial: 'insert' diff --git a/app/views/administrateurs/types_de_champ/move_down.turbo_stream.haml b/app/views/administrateurs/types_de_champ/move_down.turbo_stream.haml index 893fb25d8..500899d89 100644 --- a/app/views/administrateurs/types_de_champ/move_down.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/move_down.turbo_stream.haml @@ -1,2 +1 @@ -= turbo_stream.remove dom_id(@coordinate, :type_de_champ_editor) -= render partial: 'insert', locals: { coordinate: @coordinate, upper_coordinates: @upper_coordinates } += render partial: 'insert' diff --git a/app/views/administrateurs/types_de_champ/move_up.turbo_stream.haml b/app/views/administrateurs/types_de_champ/move_up.turbo_stream.haml index 893fb25d8..500899d89 100644 --- a/app/views/administrateurs/types_de_champ/move_up.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/move_up.turbo_stream.haml @@ -1,2 +1 @@ -= turbo_stream.remove dom_id(@coordinate, :type_de_champ_editor) -= render partial: 'insert', locals: { coordinate: @coordinate, upper_coordinates: @upper_coordinates } += render partial: 'insert' diff --git a/app/views/administrateurs/types_de_champ/update.turbo_stream.haml b/app/views/administrateurs/types_de_champ/update.turbo_stream.haml index 1f7de5784..500899d89 100644 --- a/app/views/administrateurs/types_de_champ/update.turbo_stream.haml +++ b/app/views/administrateurs/types_de_champ/update.turbo_stream.haml @@ -1,10 +1 @@ -- if @coordinate&.type_de_champ&.valid? - = turbo_stream.morph dom_id(@coordinate, :type_de_champ_editor) do - = render TypesDeChampEditor::ChampComponent.new(coordinate: @coordinate, upper_coordinates: @upper_coordinates) - = turbo_stream.morph dom_id(@coordinate.revision, :estimated_fill_duration) do - = render TypesDeChampEditor::EstimatedFillDurationComponent.new(revision: @coordinate.revision, is_annotation: @coordinate.private?) - -- if @lower_coordinates_and_dep.present? - - @lower_coordinates_and_dep.each do |other_coordinate, other_upper_coordinates| - = turbo_stream.morph dom_id(other_coordinate, :type_de_champ_editor) do - = render TypesDeChampEditor::ChampComponent.new(coordinate: other_coordinate, upper_coordinates: other_upper_coordinates) += render partial: 'insert' diff --git a/spec/controllers/administrateurs/types_de_champ_controller_spec.rb b/spec/controllers/administrateurs/types_de_champ_controller_spec.rb index ecad35a6c..50822051f 100644 --- a/spec/controllers/administrateurs/types_de_champ_controller_spec.rb +++ b/spec/controllers/administrateurs/types_de_champ_controller_spec.rb @@ -1,5 +1,23 @@ describe Administrateurs::TypesDeChampController, type: :controller do - let(:procedure) { create(:procedure) } + let(:procedure) do + create(:procedure).tap do |p| + p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l1') + p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l2') + p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l3') + p.draft_revision.add_type_de_champ(type_champ: :yes_no, libelle: 'bon dossier', private: true) + end + end + + def first_coordinate = procedure.draft_revision.revision_types_de_champ_public.first + def second_coordinate = procedure.draft_revision.revision_types_de_champ_public.reload.second + def third_coordinate = procedure.draft_revision.revision_types_de_champ_public.reload.third + + def extract_libelle(champ_component) = [champ_component.coordinate.libelle, champ_component.upper_coordinates.map(&:libelle)] + + def morpheds + assigns(:morphed) + .map { |component| extract_libelle(component) }.to_a + end before { sign_in(procedure.administrateurs.first.user) } @@ -11,9 +29,9 @@ describe Administrateurs::TypesDeChampController, type: :controller do procedure_id: procedure.id, type_de_champ: { type_champ: type_champ, - libelle: 'Nouveau champ', - private: false, - placeholder: "custom placeholder" + libelle: 'l1.5', + placeholder: "custom placeholder", + after_stable_id: first_coordinate.stable_id } } end @@ -23,7 +41,15 @@ describe Administrateurs::TypesDeChampController, type: :controller do context "create type_de_champ text" do let(:type_champ) { TypeDeChamp.type_champs.fetch(:text) } - it { is_expected.to have_http_status(:ok) } + # l1, l2, l3 => l1, l1.5, l2, l3 + # created: (l1.5, [l1]), morphed: (l2, [l1, l1.5]), (l3, [l1, l1.5, l2]) + it do + is_expected.to have_http_status(:ok) + expect(flash.alert).to eq(nil) + expect(assigns(:coordinate)).to eq(second_coordinate) + expect(extract_libelle(assigns(:created))).to eq(['l1.5', ['l1']]) + expect(morpheds).to eq([['l2', ['l1', 'l1.5']], ['l3', ['l1', 'l1.5', 'l2']]]) + end end context "validate type_de_champ linked_drop_down_list" do @@ -45,4 +71,89 @@ describe Administrateurs::TypesDeChampController, type: :controller do end end end + + describe '#update' do + let(:params) do + { + procedure_id: procedure.id, + stable_id: second_coordinate.stable_id, + type_de_champ: { + libelle: 'updated' + } + } + end + + subject { post :update, params: params, format: :turbo_stream } + + # l1, l2, l3 => l1, updated, l3 + # morphed: (updated, [l1]), (l3, [l1, updated]) + it do + is_expected.to have_http_status(:ok) + expect(flash.alert).to eq(nil) + expect(second_coordinate.libelle).to eq('updated') + + expect(assigns(:coordinate)).to eq(second_coordinate) + expect(morpheds).to eq([['updated', ['l1']], ['l3', ['l1', 'updated']]]) + end + end + + # l1, l2, l3 => l1, l3, l2 + # destroyed: l3, created: (l3, [l1]), morphed: (l2, [l1, l3]) + describe '#move_up' do + let(:params) do + { procedure_id: procedure.id, stable_id: third_coordinate.stable_id } + end + + subject { patch :move_up, params: params, format: :turbo_stream } + + it do + is_expected.to have_http_status(:ok) + expect(flash.alert).to eq(nil) + expect(second_coordinate.libelle).to eq('l3') + expect(assigns(:coordinate)).to eq(second_coordinate) + expect(assigns(:destroyed).libelle).to eq('l3') + expect(extract_libelle(assigns(:created))).to eq(['l3', ['l1']]) + expect(morpheds).to eq([['l2', ['l1', 'l3']]]) + end + end + + # l1, l2, l3 => l2, l1, l3 + # destroyed: l1, created: (l1, [l2]), morphed: (l2, []) + describe '#move_down' do + let(:params) do + { procedure_id: procedure.id, stable_id: first_coordinate.stable_id } + end + + subject { patch :move_down, params: params, format: :turbo_stream } + + it do + is_expected.to have_http_status(:ok) + expect(flash.alert).to eq(nil) + + expect(assigns(:coordinate)).to eq(second_coordinate) + expect(assigns(:destroyed).libelle).to eq('l1') + expect(extract_libelle(assigns(:created))).to eq(['l1', ['l2']]) + expect(morpheds).to eq([['l2', []]]) + end + end + + # l1, l2, l3 => l1, l3 + # destroyed: l2, morphed: (l3, [l1]) + describe '#destroy' do + let(:params) do + { procedure_id: procedure.id, stable_id: second_coordinate.stable_id } + end + + subject { delete :destroy, params: params, format: :turbo_stream } + + it do + used_to_be_second_coordinate = second_coordinate + + is_expected.to have_http_status(:ok) + expect(flash.alert).to eq(nil) + expect(assigns(:coordinate)).to eq(used_to_be_second_coordinate) + expect(assigns(:destroyed).libelle).to eq('l2') + expect(morpheds).to eq([['l3', ['l1']]]) + end + end end From faa2797d80afaadbb41997402fd3f04dfc4a8225 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 11 Jul 2022 21:31:06 +0200 Subject: [PATCH 09/13] fix: champ_value for tdc destroyed --- app/models/logic/champ_value.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/logic/champ_value.rb b/app/models/logic/champ_value.rb index ae8c71555..e9ca6e0c8 100644 --- a/app/models/logic/champ_value.rb +++ b/app/models/logic/champ_value.rb @@ -33,10 +33,10 @@ class Logic::ChampValue < Logic::Term end end - def to_s = "#{type_de_champ.libelle} Nº#{stable_id}" + def to_s = "#{type_de_champ&.libelle} Nº#{stable_id}" # TODO: gerer le cas ou un tdc est supprimé def type - case type_de_champ.type_champ + case type_de_champ&.type_champ # TODO: gerer le cas ou un tdc est supprimé when MANAGED_TYPE_DE_CHAMP.fetch(:yes_no), MANAGED_TYPE_DE_CHAMP.fetch(:checkbox) CHAMP_VALUE_TYPE.fetch(:boolean) From 0179a0a82652e6d9452e7fb519750e7995b96d97 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 11 Jul 2022 21:57:43 +0200 Subject: [PATCH 10/13] update conditions_controller --- .../administrateurs/conditions_controller.rb | 44 ++++++++++--------- .../conditions/_update.turbo_stream.haml | 8 ++++ .../conditions/add_row.turbo_stream.haml | 1 + .../change_targeted_champ.turbo_stream.haml | 1 + .../conditions/delete_row.turbo_stream.haml | 1 + .../conditions/destroy.turbo_stream.haml | 1 + .../conditions/update.turbo_stream.haml | 1 + .../conditions_controller_spec.rb | 30 +++++-------- 8 files changed, 48 insertions(+), 39 deletions(-) create mode 100644 app/views/administrateurs/conditions/_update.turbo_stream.haml create mode 100644 app/views/administrateurs/conditions/add_row.turbo_stream.haml create mode 100644 app/views/administrateurs/conditions/change_targeted_champ.turbo_stream.haml create mode 100644 app/views/administrateurs/conditions/delete_row.turbo_stream.haml create mode 100644 app/views/administrateurs/conditions/destroy.turbo_stream.haml create mode 100644 app/views/administrateurs/conditions/update.turbo_stream.haml diff --git a/app/controllers/administrateurs/conditions_controller.rb b/app/controllers/administrateurs/conditions_controller.rb index ba0c5ea45..e517b40bb 100644 --- a/app/controllers/administrateurs/conditions_controller.rb +++ b/app/controllers/administrateurs/conditions_controller.rb @@ -6,54 +6,58 @@ module Administrateurs def update condition = condition_form.to_condition - tdc.update!(condition: condition) + @tdc.update!(condition: condition) - render 'administrateurs/types_de_champ/update.turbo_stream.haml' + @condition_component = build_condition_component end def add_row - condition = Logic.add_empty_condition_to(tdc.condition) - tdc.update!(condition: condition) + condition = Logic.add_empty_condition_to(@tdc.condition) + @tdc.update!(condition: condition) - render 'administrateurs/types_de_champ/update.turbo_stream.haml' + @condition_component = build_condition_component end def delete_row condition = condition_form.delete_row(row_index).to_condition - tdc.update!(condition: condition) + @tdc.update!(condition: condition) - render 'administrateurs/types_de_champ/update.turbo_stream.haml' + @condition_component = build_condition_component end def destroy - tdc.update!(condition: nil) + @tdc.update!(condition: nil) - render 'administrateurs/types_de_champ/update.turbo_stream.haml' + @condition_component = build_condition_component end def change_targeted_champ condition = condition_form.change_champ(row_index).to_condition - tdc.update!(condition: condition) + @tdc.update!(condition: condition) - render 'administrateurs/types_de_champ/update.turbo_stream.haml' + @condition_component = build_condition_component end private + def build_condition_component + TypesDeChampEditor::ConditionsComponent.new( + tdc: @tdc, + upper_tdcs: @upper_tdcs, + procedure_id: @procedure.id + ) + end + def condition_form ConditionForm.new(condition_params) end def retrieve_coordinate_and_uppers - @coordinate = draft_revision.coordinate_for(tdc) - @upper_coordinates = draft_revision - .revision_types_de_champ_public - .includes(:type_de_champ) - .take_while { |c| c != @coordinate } - end - - def tdc - @tdc ||= draft_revision.find_and_ensure_exclusive_use(params[:stable_id]) + @tdc = draft_revision.find_and_ensure_exclusive_use(params[:stable_id]) + @coordinate = draft_revision.coordinate_for(@tdc) + @upper_tdcs = draft_revision + .upper_coordinates(@coordinate.position) + .map(&:type_de_champ) end def draft_revision diff --git a/app/views/administrateurs/conditions/_update.turbo_stream.haml b/app/views/administrateurs/conditions/_update.turbo_stream.haml new file mode 100644 index 000000000..d5dcb27a6 --- /dev/null +++ b/app/views/administrateurs/conditions/_update.turbo_stream.haml @@ -0,0 +1,8 @@ +- rendered = render @condition_component + +- if rendered.present? + = turbo_stream.morph dom_id(@tdc, :conditions) do + - rendered + end +- else + = turbo_stream.remove dom_id(@tdc, :conditions) diff --git a/app/views/administrateurs/conditions/add_row.turbo_stream.haml b/app/views/administrateurs/conditions/add_row.turbo_stream.haml new file mode 100644 index 000000000..8f9900e50 --- /dev/null +++ b/app/views/administrateurs/conditions/add_row.turbo_stream.haml @@ -0,0 +1 @@ += render partial: 'update' diff --git a/app/views/administrateurs/conditions/change_targeted_champ.turbo_stream.haml b/app/views/administrateurs/conditions/change_targeted_champ.turbo_stream.haml new file mode 100644 index 000000000..8f9900e50 --- /dev/null +++ b/app/views/administrateurs/conditions/change_targeted_champ.turbo_stream.haml @@ -0,0 +1 @@ += render partial: 'update' diff --git a/app/views/administrateurs/conditions/delete_row.turbo_stream.haml b/app/views/administrateurs/conditions/delete_row.turbo_stream.haml new file mode 100644 index 000000000..8f9900e50 --- /dev/null +++ b/app/views/administrateurs/conditions/delete_row.turbo_stream.haml @@ -0,0 +1 @@ += render partial: 'update' diff --git a/app/views/administrateurs/conditions/destroy.turbo_stream.haml b/app/views/administrateurs/conditions/destroy.turbo_stream.haml new file mode 100644 index 000000000..8f9900e50 --- /dev/null +++ b/app/views/administrateurs/conditions/destroy.turbo_stream.haml @@ -0,0 +1 @@ += render partial: 'update' diff --git a/app/views/administrateurs/conditions/update.turbo_stream.haml b/app/views/administrateurs/conditions/update.turbo_stream.haml new file mode 100644 index 000000000..8f9900e50 --- /dev/null +++ b/app/views/administrateurs/conditions/update.turbo_stream.haml @@ -0,0 +1 @@ += render partial: 'update' diff --git a/spec/controllers/administrateurs/conditions_controller_spec.rb b/spec/controllers/administrateurs/conditions_controller_spec.rb index a037714ea..60dd5d295 100644 --- a/spec/controllers/administrateurs/conditions_controller_spec.rb +++ b/spec/controllers/administrateurs/conditions_controller_spec.rb @@ -5,9 +5,7 @@ describe Administrateurs::ConditionsController, type: :controller do let(:first_coordinate) { procedure.draft_revision.revision_types_de_champ.first } let(:second_tdc) { procedure.draft_revision.types_de_champ.second } - before do - sign_in(procedure.administrateurs.first.user) - end + before { sign_in(procedure.administrateurs.first.user) } let(:default_params) do { @@ -17,9 +15,7 @@ describe Administrateurs::ConditionsController, type: :controller do end describe '#update' do - before do - post :update, params: params - end + before { post :update, params: params, format: :turbo_stream } let(:params) { default_params.merge(type_de_champ: { condition_form: condition_form }) } @@ -38,26 +34,22 @@ describe Administrateurs::ConditionsController, type: :controller do it do expect(second_tdc.reload.condition).to eq(ds_eq(champ_value(1), constant(2))) expect(assigns(:coordinate)).to eq(procedure.draft_revision.coordinate_for(second_tdc)) - expect(assigns(:upper_coordinates)).to eq([first_coordinate]) + expect(assigns(:upper_tdcs)).to eq([first_coordinate.type_de_champ]) end end describe '#add_row' do - before do - post :add_row, params: default_params - end + before { post :add_row, params: default_params, format: :turbo_stream } it do expect(second_tdc.reload.condition).to eq(empty_operator(empty, empty)) expect(assigns(:coordinate)).to eq(procedure.draft_revision.coordinate_for(second_tdc)) - expect(assigns(:upper_coordinates)).to eq([first_coordinate]) + expect(assigns(:upper_tdcs)).to eq([first_coordinate.type_de_champ]) end end describe '#delete_row' do - before do - delete :delete_row, params: params.merge(row_index: 0) - end + before { delete :delete_row, params: params.merge(row_index: 0), format: :turbo_stream } let(:params) { default_params.merge(type_de_champ: { condition_form: condition_form }) } @@ -76,20 +68,20 @@ describe Administrateurs::ConditionsController, type: :controller do it do expect(second_tdc.reload.condition).to eq(nil) expect(assigns(:coordinate)).to eq(procedure.draft_revision.coordinate_for(second_tdc)) - expect(assigns(:upper_coordinates)).to eq([first_coordinate]) + expect(assigns(:upper_tdcs)).to eq([first_coordinate.type_de_champ]) end end describe '#destroy' do before do second_tdc.update(condition: empty_operator(empty, empty)) - delete :destroy, params: default_params + delete :destroy, params: default_params, format: :turbo_stream end it do expect(second_tdc.reload.condition).to eq(nil) expect(assigns(:coordinate)).to eq(procedure.draft_revision.coordinate_for(second_tdc)) - expect(assigns(:upper_coordinates)).to eq([first_coordinate]) + expect(assigns(:upper_tdcs)).to eq([first_coordinate.type_de_champ]) end end @@ -98,7 +90,7 @@ describe Administrateurs::ConditionsController, type: :controller do before do second_tdc.update(condition: empty_operator(empty, empty)) - patch :change_targeted_champ, params: params + patch :change_targeted_champ, params: params, format: :turbo_stream end let(:params) { default_params.merge(type_de_champ: { condition_form: condition_form }) } @@ -118,7 +110,7 @@ describe Administrateurs::ConditionsController, type: :controller do it do expect(second_tdc.reload.condition).to eq(ds_eq(champ_value(number_tdc.stable_id), constant(0))) expect(assigns(:coordinate)).to eq(procedure.draft_revision.coordinate_for(second_tdc)) - expect(assigns(:upper_coordinates)).to eq([first_coordinate]) + expect(assigns(:upper_tdcs)).to eq([first_coordinate.type_de_champ]) end end end From bd708a12a0f3f3711449955c6294c70119381022 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 11 Jul 2022 22:34:40 +0200 Subject: [PATCH 11/13] fix: missing traduction in conditions errors --- .../conditions_errors_component.rb | 4 ++-- .../conditions_errors_component.fr.yml | 10 ++-------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/app/components/types_de_champ_editor/conditions_errors_component.rb b/app/components/types_de_champ_editor/conditions_errors_component.rb index fd7263274..7880e7d74 100644 --- a/app/components/types_de_champ_editor/conditions_errors_component.rb +++ b/app/components/types_de_champ_editor/conditions_errors_component.rb @@ -22,11 +22,11 @@ class TypesDeChampEditor::ConditionsErrorsComponent < ApplicationComponent elsif left.type == :unmanaged t('unmanaged', scope: '.errors', libelle: targeted_champ.libelle, - type_champ: t(targeted_champ.type_champ, scope: '.type')) + type_champ: t(targeted_champ.type_champ, scope: 'activerecord.attributes.type_de_champ.type_champs')&.downcase) else t('incompatible', scope: '.errors', libelle: targeted_champ.libelle, - type_champ: t(targeted_champ.type_champ, scope: '.type'), + type_champ: t(targeted_champ.type_champ, scope: 'activerecord.attributes.type_de_champ.type_champs')&.downcase, operator: t(operator_name, scope: 'logic.operators').downcase, right: right.to_s.downcase) end diff --git a/app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.fr.yml b/app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.fr.yml index aecf81d2e..5ee5690a3 100644 --- a/app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.fr.yml +++ b/app/components/types_de_champ_editor/conditions_errors_component/conditions_errors_component.fr.yml @@ -1,12 +1,6 @@ --- fr: - type: - number: un nombre - string: un texte - boolean: soit oui, soit non - yes_no: soit oui, soit non - address: une adresse errors: not_available: "Un champ cible n'est plus disponible. Il est soit supprimé, soit déplacé en dessous de ce champ." - unmanaged: "Le champ « %{libelle} » est %{type_champ} et ne peut pas être utilisé comme champ cible." - incompatible: "Le champ « %{libelle} » est %{type_champ}. Il ne peut pas être %{operator} %{right}." + unmanaged: "Le champ « %{libelle} » est de type « %{type_champ} » et ne peut pas être utilisé comme champ cible." + incompatible: "Le champ « %{libelle} » est de type « %{type_champ} ». Il ne peut pas être %{operator} %{right}." From c4c0c275e112b313c870949cbfdfdba67ff49af9 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 11 Jul 2022 22:20:51 +0200 Subject: [PATCH 12/13] fix: coordinate_and_tdc return nil if not found --- app/models/procedure_revision.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 1721f046c..1ef377909 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -229,7 +229,7 @@ class ProcedureRevision < ApplicationRecord .joins(:type_de_champ) .find_by(type_de_champ: { stable_id: stable_id }) - [coordinate, coordinate.type_de_champ] + [coordinate, coordinate&.type_de_champ] end def renumber(siblings) From 9bd1d97ed71ab9646771a43ac3a1b9f437fdf3bf Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 12 Jul 2022 10:43:40 +0200 Subject: [PATCH 13/13] add feature flag --- .../types_de_champ_editor/conditions_component.rb | 6 +++++- .../types_de_champ_editor/conditions_component_spec.rb | 6 ++++++ spec/system/administrateurs/condition_spec.rb | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/components/types_de_champ_editor/conditions_component.rb b/app/components/types_de_champ_editor/conditions_component.rb index 3a68ee59b..f1fee3dbc 100644 --- a/app/components/types_de_champ_editor/conditions_component.rb +++ b/app/components/types_de_champ_editor/conditions_component.rb @@ -201,7 +201,11 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent end def render? - @condition.present? || available_targets_for_select.any? + if Flipper.enabled?(:conditional, controller.current_user) + @condition.present? || available_targets_for_select.any? + else + false + end end def input_name_for(name) diff --git a/spec/components/types_de_champ_editor/conditions_component_spec.rb b/spec/components/types_de_champ_editor/conditions_component_spec.rb index e4831f87b..c28559e70 100644 --- a/spec/components/types_de_champ_editor/conditions_component_spec.rb +++ b/spec/components/types_de_champ_editor/conditions_component_spec.rb @@ -1,6 +1,12 @@ describe TypesDeChampEditor::ConditionsComponent, type: :component do include Logic + # TODO: remove along with feature flipping + before do + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(nil) + allow(Flipper).to receive(:enabled?).with(:conditional, anything).and_return(true) + end + describe 'render' do let(:tdc) { create(:type_de_champ, condition: condition) } let(:condition) { nil } diff --git a/spec/system/administrateurs/condition_spec.rb b/spec/system/administrateurs/condition_spec.rb index 698c6d03e..5b2a5cabe 100644 --- a/spec/system/administrateurs/condition_spec.rb +++ b/spec/system/administrateurs/condition_spec.rb @@ -17,6 +17,8 @@ describe 'As an administrateur I can edit types de champ condition', js: true do before do login_as administrateur.user, scope: :user + # TODO: remove along with feature flipping + allow(Flipper).to receive(:enabled?).with(:conditional, anything).and_return(true) visit champs_admin_procedure_path(procedure) end