Merge pull request #7561 from betagouv/restrict_conditional

fix(conditional): limite le conditionnel aux champs parents
This commit is contained in:
LeSim 2022-07-12 14:35:03 +02:00 committed by GitHub
commit 5b19aa9fe5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 38 additions and 32 deletions

View file

@ -77,4 +77,5 @@
= 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))
- if Flipper.enabled?(:conditional, controller.current_user) && !type_de_champ.private? && !coordinate.child?
= render(TypesDeChampEditor::ConditionsComponent.new(tdc: type_de_champ, upper_tdcs: @upper_coordinates.map(&:type_de_champ), procedure_id: procedure.id))

View file

@ -201,11 +201,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
def render?
if Flipper.enabled?(:conditional, controller.current_user)
@condition.present? || available_targets_for_select.any?
else
false
end
@condition.present? || available_targets_for_select.any?
end
def input_name_for(name)

View file

@ -55,9 +55,7 @@ module Administrateurs
def retrieve_coordinate_and_uppers
@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)
@upper_tdcs = @coordinate.upper_siblings.map(&:type_de_champ)
end
def draft_revision

View file

@ -8,7 +8,7 @@ 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)
@morphed = champ_components_starting_at(@coordinate, 1)
reset_procedure
flash.notice = "Formulaire enregistré"
@ -22,7 +22,7 @@ module Administrateurs
if type_de_champ.update(type_de_champ_update_params)
@coordinate = draft.coordinate_for(type_de_champ)
@morphed = champ_components_starting_at(@coordinate.position)
@morphed = champ_components_starting_at(@coordinate)
reset_procedure
flash.notice = "Formulaire enregistré"
@ -42,7 +42,7 @@ module Administrateurs
@destroyed = @coordinate
@created = champ_component_from(@coordinate)
# update the one component below
@morphed = champ_components_starting_at(@coordinate.position + 1).take(1)
@morphed = champ_components_starting_at(@coordinate, 1).take(1)
end
def move_down
@ -51,7 +51,7 @@ module Administrateurs
@destroyed = @coordinate
@created = champ_component_from(@coordinate)
# update the one component above
@morphed = champ_components_starting_at(@coordinate.position - 1).take(1)
@morphed = champ_components_starting_at(@coordinate, - 1).take(1)
end
def destroy
@ -60,14 +60,14 @@ module Administrateurs
flash.notice = "Formulaire enregistré"
@destroyed = @coordinate
@morphed = champ_components_starting_at(@coordinate.position)
@morphed = champ_components_starting_at(@coordinate)
end
private
def champ_components_starting_at(position)
draft
.coordinates_starting_at(position)
def champ_components_starting_at(coordinate, offset = 0)
coordinate
.siblings_starting_at(offset)
.lazy
.map { |c| champ_component_from(c) }
end
@ -75,7 +75,7 @@ module Administrateurs
def champ_component_from(coordinate, focused: false)
TypesDeChampEditor::ChampComponent.new(
coordinate: coordinate,
upper_coordinates: draft.upper_coordinates(coordinate.position),
upper_coordinates: coordinate.upper_siblings,
focused: focused
)
end

View file

@ -197,14 +197,6 @@ 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

View file

@ -47,6 +47,14 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
end
end
def upper_siblings
siblings.filter { |s| s.position < position }
end
def siblings_starting_at(offset)
siblings.filter { |s| (position + offset) <= s.position }
end
def previous_sibling
index = siblings.index(self)
if index > 0

View file

@ -1,12 +1,6 @@
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 }

View file

@ -0,0 +1,17 @@
describe 'As an administrateur I can edit annotation', js: true do
let(:administrateur) { procedure.administrateurs.first }
let(:procedure) { create(:procedure) }
before do
login_as administrateur.user, scope: :user
visit annotations_admin_procedure_path(procedure)
end
scenario "adding a new champ" do
click_on 'Ajouter une annotation'
select('Carte', from: 'Type de champ')
# ensure UI update is ok
check 'Cadastres'
end
end