Compare commits

...

5 commits

Author SHA1 Message Date
Eric Leroy-Terquem
74772112cf chore(routing): update routing_rule validations 2023-10-13 17:31:48 +02:00
Eric Leroy-Terquem
32894db8e7 chore(conditions): update views 2023-10-13 17:31:48 +02:00
Eric Leroy-Terquem
76185a113d chore(conditions): update controllers and routes 2023-10-13 17:31:48 +02:00
Eric Leroy-Terquem
d4562706c2 chore(conditions): update condition_form 2023-10-13 17:31:48 +02:00
Eric Leroy-Terquem
c8396f92f5 chore(conditions): champs_conditions and routing_rule components inherit from conditions_component 2023-10-13 17:31:48 +02:00
22 changed files with 343 additions and 135 deletions

View file

@ -0,0 +1,44 @@
class Conditions::ChampsConditionsComponent < Conditions::ConditionsComponent
def initialize(tdc:, upper_tdcs:, procedure_id:)
@tdc, @condition, @tdcs = tdc, tdc.condition, upper_tdcs
@procedure_id = procedure_id
end
private
def logic_conditionnel_button
html_class = 'fr-btn fr-btn--tertiary fr-btn--sm'
if @condition.nil?
submit_tag(
t('.enable_conditionnel'),
formaction: add_condition_path,
class: html_class
)
else
submit_tag(
t('.disable_conditionnel'),
formmethod: 'delete',
formnovalidate: true,
data: { confirm: t('.disable_conditionnel_alert') },
class: html_class
)
end
end
def add_condition_path
add_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id)
end
def delete_condition_path(row_index)
delete_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id, row_index: row_index)
end
def input_id_for(name, row_index)
"#{@tdc.stable_id}-#{name}-#{row_index}"
end
def input_prefix
'type_de_champ[condition_form]'
end
end

View file

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

View file

@ -1,11 +1,6 @@
class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
class Conditions::ConditionsComponent < ApplicationComponent
include Logic
def initialize(tdc:, upper_tdcs:, procedure_id:)
@tdc, @condition, @upper_tdcs = tdc, tdc.condition, upper_tdcs
@procedure_id = procedure_id
end
private
def rows
@ -20,26 +15,6 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
end
def logic_conditionnel_button
html_class = 'fr-btn fr-btn--tertiary fr-btn--sm'
if @condition.nil?
submit_tag(
t('.enable_conditionnel'),
formaction: add_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id),
class: html_class
)
else
submit_tag(
t('.disable_conditionnel'),
formmethod: 'delete',
formnovalidate: true,
data: { confirm: t('.disable_conditionnel_alert') },
class: html_class
)
end
end
def far_left_tag(row_number)
if row_number == 0
t('.display_if')
@ -85,7 +60,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
def available_targets_for_select
@upper_tdcs
@tdcs
.filter { |tdc| ChampValue::MANAGED_TYPE_DE_CHAMP.values.include?(tdc.type_champ) }
.map { |tdc| [tdc.libelle, champ_value(tdc.stable_id).to_json] }
end
@ -108,7 +83,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
def compatibles_operators_for_select(left)
case left.type(@upper_tdcs)
case left.type(@tdcs)
when ChampValue::CHAMP_VALUE_TYPE.fetch(:boolean)
[
[t('is', scope: 'logic'), Eq.name]
@ -138,7 +113,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def right_operand_tag(left, right, row_index)
right_invalid = !current_right_valid?(left, right)
case left.type(@upper_tdcs)
case left.type(@tdcs)
when :boolean
booleans_for_select = [[t('utils.yes'), constant(true).to_json], [t('utils.no'), constant(false).to_json]]
@ -160,7 +135,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
class: 'fr-select'
)
when :enum, :enums
enums_for_select = left.options(@upper_tdcs)
enums_for_select = left.options(@tdcs)
if right_invalid
enums_for_select = empty_target_for_select + enums_for_select
@ -186,13 +161,13 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
def current_right_valid?(left, right)
Logic.compatible_type?(left, right, @upper_tdcs)
Logic.compatible_type?(left, right, @tdcs)
end
def add_condition_tag
tag.button(
t('.add_condition'),
formaction: add_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id),
formaction: add_condition_path,
formnovalidate: true,
class: 'fr-btn fr-btn--secondary fr-btn--sm fr-icon-add-circle-line fr-btn--icon-left'
)
@ -201,7 +176,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def delete_condition_tag(row_index)
tag.button(
tag.span('', class: 'icon delete') + tag.span(t('.remove_a_row'), class: 'sr-only'),
formaction: delete_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id, row_index: row_index),
formaction: delete_condition_path(row_index),
formmethod: 'delete',
formnovalidate: true
)
@ -214,12 +189,4 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def input_name_for(name)
"#{input_prefix}[rows][][#{name}]"
end
def input_id_for(name, row_index)
"#{@tdc.stable_id}-#{name}-#{row_index}"
end
def input_prefix
'type_de_champ[condition_form]'
end
end

View file

@ -0,0 +1,26 @@
class Conditions::RoutingRulesComponent < Conditions::ConditionsComponent
def initialize(groupe_instructeur:)
@groupe_instructeur = groupe_instructeur
@condition = groupe_instructeur.routing_rule
@procedure_id = groupe_instructeur.procedure_id
@tdcs = groupe_instructeur.procedure.active_revision.types_de_champ
end
private
def add_condition_path
add_row_admin_procedure_routing_rule_path(@procedure_id, @groupe_instructeur.id)
end
def delete_condition_path(row_index)
delete_row_admin_procedure_routing_rule_path(@procedure_id, @groupe_instructeur.id, row_index: row_index)
end
def input_id_for(name, row_index)
"#{@groupe_instructeur.id}-#{name}-#{row_index}"
end
def input_prefix
'groupe_instructeur[condition_form]'
end
end

View file

@ -0,0 +1,9 @@
---
fr:
display_if: Router si
enable_conditionnel: cliquer pour activer
disable_conditionnel: cliquer pour désactiver
disable_conditionnel_alert: "La logique conditionnelle appliquée à ce champ sera désactivée.\nVoulez-vous continuer ?"
select: Sélectionner
add_condition: Ajouter une règle de routage
remove_a_row: Supprimer la ligne

View file

@ -0,0 +1,32 @@
.flex.justify-start.section{ id: dom_id(@groupe_instructeur, :routing_rule) }
= form_tag admin_procedure_routing_rule_path(@procedure_id, @groupe_instructeur.id),
method: :patch,
data: { turbo: true, controller: 'autosave' },
class: 'form width-100' do
.conditionnel.mt-2.width-100
%table.condition-table.mt-2
%thead
%tr
%th.far-left
%th.target Champ Cible
%th.operator Opérateur
%th.value Valeur
%th.delete-column
%tbody
- if rows.blank?
%tr
%td.far-left= far_left_tag(0)
%td.target= left_operand_tag(empty, 0)
%td.operator= operator_tag(EmptyOperator.name, empty, 0)
%td.value= right_operand_tag(empty, empty, 0)
%td.delete-column= delete_condition_tag(0)
- rows.each.with_index do |(targeted_champ, operator_name, value), row_index|
%tr
%td.far-left= far_left_tag(row_index)
%td.target= left_operand_tag(targeted_champ, row_index)
%td.operator= operator_tag(operator_name, targeted_champ, row_index)
%td.value= right_operand_tag(targeted_champ, value, row_index)
%td.delete-column= delete_condition_tag(row_index)
.flex.justify-end.mt-2= add_condition_tag

View file

@ -22,34 +22,14 @@
Groupe inactif
%span.fr-hint-text Si cette option est activée, les usagers ne pourront plus sélectionner ce groupe dinstructeurs
= form_tag admin_procedure_routing_rules_path(@procedure),
method: :post,
data: { controller: 'autosave' },
class: 'fr-mb-3w' do
= render Conditions::RoutingRulesComponent.new(groupe_instructeur: @groupe_instructeur)
= hidden_field_tag('groupe_instructeur_id', @groupe_instructeur.id)
.fr-hint-text.mt-2.mb-2
%span Si vous ne trouvez pas l'option correspondant à votre groupe, veuillez l'ajouter dans le
%span
= link_to 'champ concerné', champs_admin_procedure_path(@procedure)
.flex
%p.fr-mb-1w.fr-mr-2w Routage
- if @groupe_instructeur.invalid_rule?
%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}
.flex.align-baseline.fr-mb-1w
.fr-mr-2w.no-wrap si le champ
.target.fr-mr-2w
= targeted_champ_tag
.operator.fr-mr-2w.no-wrap
= operator_tag
.value
= value_tag
.fr-hint-text
%span Si vous ne trouvez pas l'option correspondant à votre groupe, veuillez l'ajouter dans le champ dédié au
%span
= link_to 'routage', champs_admin_procedure_path(@procedure)
.flex.fr-btns-group--sm.fr-btns-group--inline.fr-btns-group--icon-right
.flex.fr-btns-group--sm.fr-btns-group--inline.fr-btns-group--icon-right.mb-2
- if @groupe_instructeur.can_delete?
%p= t('.delete')
= button_to admin_procedure_groupe_instructeur_path(@procedure, @groupe_instructeur),

View file

@ -116,7 +116,7 @@
.type-de-champ-add-button{ id: dom_id(coordinate, :type_de_champ_add_button), class: class_names(hidden: !coordinate.empty?) }
= 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))
= render(Conditions::ChampsConditionsComponent.new(tdc: type_de_champ, upper_tdcs: @upper_coordinates.map(&:type_de_champ), procedure_id: procedure.id))
.type-de-champ-add-button{ class: class_names(root: !coordinate.child?) }
= render TypesDeChampEditor::AddChampButtonComponent.new(revision: coordinate.revision, parent: coordinate&.parent, is_annotation: coordinate.private?, after_stable_id: type_de_champ.stable_id)

View file

@ -42,7 +42,7 @@ module Administrateurs
private
def build_condition_component
TypesDeChampEditor::ConditionsComponent.new(
Conditions::ChampsConditionsComponent.new(
tdc: @tdc,
upper_tdcs: @upper_tdcs,
procedure_id: @procedure.id
@ -50,7 +50,7 @@ module Administrateurs
end
def condition_form
ConditionForm.new(condition_params.merge({ upper_tdcs: @upper_tdcs }))
ConditionForm.new(condition_params.merge({ tdcs: @upper_tdcs }))
end
def retrieve_coordinate_and_uppers

View file

@ -1,56 +0,0 @@
module Administrateurs
class RoutingController < AdministrateurController
include Logic
before_action :retrieve_procedure
def update
left = targeted_champ
right = targeted_champ_changed? ? empty : value
new_routing_rule = case operator_name
when Eq.name
ds_eq(left, right)
when NotEq.name
ds_not_eq(left, right)
end
groupe_instructeur.update!(routing_rule: new_routing_rule)
end
def update_defaut_groupe_instructeur
new_defaut = @procedure.groupe_instructeurs.find(defaut_groupe_instructeur_id)
@procedure.update!(defaut_groupe_instructeur: new_defaut)
end
private
def targeted_champ_changed?
targeted_champ != groupe_instructeur.routing_rule&.left
end
def targeted_champ
Logic.from_json(params[:targeted_champ])
end
def operator_name
params[:operator_name]
end
def value
Logic.from_json(params[:value])
end
def groupe_instructeur
@groupe_instructeur ||= @procedure.groupe_instructeurs.find(groupe_instructeur_id)
end
def groupe_instructeur_id
params[:groupe_instructeur_id]
end
def defaut_groupe_instructeur_id
params[:defaut_groupe_instructeur_id]
end
end
end

View file

@ -0,0 +1,94 @@
module Administrateurs
class RoutingRulesController < AdministrateurController
include Logic
before_action :retrieve_procedure, :retrieve_groupe_instructeur, :retrieve_tdcs
def update
condition = condition_form.to_condition
@groupe_instructeur.update!(routing_rule: condition)
@routing_rule_component = build_routing_rule_component
end
def add_row
condition = Logic.add_empty_condition_to(@groupe_instructeur.routing_rule)
@groupe_instructeur.update!(routing_rule: condition)
@routing_rule_component = build_routing_rule_component
end
def delete_row
condition = condition_form.delete_row(row_index).to_condition
@groupe_instructeur.update!(routing_rule: condition)
@routing_rule_component = build_routing_rule_component
end
def change_targeted_champ
condition = condition_form.change_champ(row_index).to_condition
@groupe_instructeur.update!(routing_rule: condition)
@routing_rule_component = build_routing_rule_component
end
def update_defaut_groupe_instructeur
new_defaut = @procedure.groupe_instructeurs.find(defaut_groupe_instructeur_id)
@procedure.update!(defaut_groupe_instructeur: new_defaut)
end
private
def targeted_champ_changed?
targeted_champ != groupe_instructeur.routing_rule&.left
end
def targeted_champ
Logic.from_json(params[:targeted_champ])
end
def operator_name
params[:operator_name]
end
def value
Logic.from_json(params[:value])
end
def groupe_instructeur_id
params[:groupe_instructeur_id]
end
def defaut_groupe_instructeur_id
params[:defaut_groupe_instructeur_id]
end
def build_routing_rule_component
Conditions::RoutingRulesComponent.new(
groupe_instructeur: @groupe_instructeur
)
end
def condition_form
ConditionForm.new(routing_rule_params.merge({ tdcs: @tdcs }))
end
def retrieve_tdcs
@tdcs = @procedure.active_revision.types_de_champ
end
def retrieve_groupe_instructeur
@groupe_instructeur = @procedure.groupe_instructeurs.find(groupe_instructeur_id)
end
def routing_rule_params
params
.require(:groupe_instructeur)
.require(:condition_form)
.permit(:top_operator_name, rows: [:targeted_champ, :operator_name, :value])
end
def row_index
params[:row_index].to_i
end
end
end

View file

@ -2,7 +2,7 @@ class ConditionForm
include ActiveModel::Model
include Logic
attr_accessor :top_operator_name, :rows, :upper_tdcs
attr_accessor :top_operator_name, :rows, :tdcs
def to_condition
case sub_conditions.count
@ -22,7 +22,7 @@ class ConditionForm
end
def change_champ(i)
sub_conditions[i] = Logic.ensure_compatibility_from_left(sub_conditions[i], upper_tdcs)
sub_conditions[i] = Logic.ensure_compatibility_from_left(sub_conditions[i], tdcs)
self
end
@ -39,7 +39,7 @@ class ConditionForm
def row_to_condition(row)
left = Logic.from_json(row[:targeted_champ])
right = parse_value(left.type(upper_tdcs), row[:value])
right = parse_value(left.type(tdcs), row[:value])
Logic.class_from_name(row[:operator_name]).new(left, right)
end

View file

@ -84,7 +84,15 @@ class GroupeInstructeur < ApplicationRecord
def valid_rule?
return false if routing_rule.nil?
([routing_rule.left, routing_rule, routing_rule.right] in [ChampValue, Eq | NotEq, Constant]) && routing_rule_matches_tdc?
if [And, Or].include?(routing_rule.class)
routing_rule.operands.all? { |rule_line| valid_rule_line?(rule_line) }
else
valid_rule_line?(routing_rule)
end
end
def valid_rule_line?(routing_rule)
([routing_rule.left, routing_rule, routing_rule.right] in [ChampValue, (LessThan | LessThanEq | Eq | NotEq | GreaterThanEq | GreaterThan | IncludeOperator), Constant]) && routing_rule_matches_tdc?
end
def non_unique_rule?
@ -95,7 +103,7 @@ class GroupeInstructeur < ApplicationRecord
def groups_with_same_rule
return if routing_rule.nil?
other_groupe_instructeurs
.filter { |gi| !gi.routing_rule.nil? && gi.routing_rule.right != empty && gi.routing_rule == routing_rule }
.filter { |gi| !gi.routing_rule.nil? && gi.routing_rule&.right != empty && gi.routing_rule == routing_rule }
.map(&:label)
.join(', ')
end
@ -107,6 +115,14 @@ class GroupeInstructeur < ApplicationRecord
private
def routing_rule_matches_tdc?
if [And, Or].include?(routing_rule.class)
routing_rule.operands.all? { |rule_line| rule_line_matches_tdc?(rule_line) }
else
rule_line_matches_tdc?(routing_rule)
end
end
def rule_line_matches_tdc?(routing_rule)
routing_tdc = procedure.active_revision.types_de_champ.find_by(stable_id: routing_rule.left.stable_id)
options = case routing_tdc.type_champ
@ -114,8 +130,14 @@ class GroupeInstructeur < ApplicationRecord
APIGeoService.departements.map { _1[:code] }
when TypeDeChamp.type_champs.fetch(:regions)
APIGeoService.regions.map { _1[:code] }
when TypeDeChamp.type_champs.fetch(:drop_down_list)
when TypeDeChamp.type_champs.fetch(:drop_down_list), TypeDeChamp.type_champs.fetch(:multiple_drop_down_list)
routing_tdc.options_with_drop_down_other
when TypeDeChamp.type_champs.fetch(:checkbox), TypeDeChamp.type_champs.fetch(:yes_no)
[true, false]
when TypeDeChamp.type_champs.fetch(:integer_number)
return routing_rule.right.value.is_a? Integer
when TypeDeChamp.type_champs.fetch(:decimal_number)
return routing_rule.right.value.is_a? Float
end
routing_rule.right.value.in?(options)
end

View file

@ -0,0 +1,7 @@
- rendered = render @routing_rule_component
- if rendered.present?
= turbo_stream.replace dom_id(@groupe_instructeur, :routing_rule) do
- rendered
- else
= turbo_stream.remove dom_id(@groupe_instructeur, :routing_rule)

View file

@ -0,0 +1 @@
= render partial: 'update'

View file

@ -0,0 +1 @@
= render partial: 'update'

View file

@ -0,0 +1 @@
= render partial: 'update'

View file

@ -0,0 +1 @@
= render partial: 'update'

View file

@ -542,7 +542,12 @@ Rails.application.routes.draw do
delete :delete_row, on: :member
end
patch :update, controller: 'routing', as: :routing_rules
resources :routing_rules, only: [:update, :destroy], param: :groupe_instructeur_id do
patch :add_row, on: :member
patch :change_targeted_champ, on: :member
delete :delete_row, on: :member
end
patch :update_defaut_groupe_instructeur, controller: 'routing', as: :update_defaut_groupe_instructeur
put 'clone'

View file

@ -1,8 +1,82 @@
describe Administrateurs::RoutingController, type: :controller do
describe Administrateurs::RoutingRulesController, type: :controller do
include Logic
before { sign_in(procedure.administrateurs.first.user) }
context 'using condition component' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :drop_down_list, libelle: 'Votre ville', options: ['Paris', 'Lyon', 'Marseille'] }, { type: :text, libelle: 'Un champ texte' }]) }
let(:gi_2) { create(:groupe_instructeur, label: 'groupe 2', procedure: procedure) }
let(:drop_down_tdc) { procedure.draft_revision.types_de_champ.first }
let(:default_params) do
{
procedure_id: procedure.id,
groupe_instructeur_id: gi_2.id
}
end
describe '#update' do
before { post :update, params: params, format: :turbo_stream }
let(:params) { default_params.merge(groupe_instructeur: { condition_form: condition_form }) }
let(:condition_form) do
{
rows: [
{
targeted_champ: champ_value(drop_down_tdc.stable_id).to_json,
operator_name: Logic::Eq.name,
value: empty.to_json
}
]
}
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_eq(champ_value(drop_down_tdc.stable_id), empty))
end
end
describe '#add_row' do
before do
gi_2.update(routing_rule: ds_eq(champ_value(drop_down_tdc.stable_id), empty))
post :add_row, params: default_params, format: :turbo_stream
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_and([ds_eq(champ_value(drop_down_tdc.stable_id), empty), empty_operator(empty, empty)]))
end
end
describe '#delete_row' do
before do
gi_2.update(routing_rule: ds_and([ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')), empty_operator(empty, empty)]))
post :delete_row, params: params.merge(row_index: 1), format: :turbo_stream
end
let(:params) { default_params.merge(groupe_instructeur: { condition_form: condition_form }) }
let(:condition_form) do
{
rows: [
{
targeted_champ: champ_value(drop_down_tdc.stable_id).to_json,
operator_name: Logic::Eq.name,
value: constant('Lyon')
},
{
targeted_champ: empty,
operator_name: Logic::Eq.name,
value: empty
}
]
}
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
end
end
end
describe '#update targeted champ' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :drop_down_list, libelle: 'Votre ville', options: ['Paris', 'Lyon', 'Marseille'] }, { type: :text, libelle: 'Un champ texte' }]) }
let(:gi_2) { create(:groupe_instructeur, label: 'groupe 2', procedure: procedure) }