chore(conditions): champs_conditions_component and routing_rules_component inherit from conditions_component
This commit is contained in:
parent
baaf4e3517
commit
979b5101ae
10 changed files with 120 additions and 70 deletions
44
app/components/conditions/champs_conditions_component.rb
Normal file
44
app/components/conditions/champs_conditions_component.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
class Conditions::ChampsConditionsComponent < Conditions::ConditionsComponent
|
||||
def initialize(tdc:, upper_tdcs:, procedure_id:)
|
||||
@tdc, @condition, @source_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
|
|
@ -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: @source_tdcs)
|
||||
|
||||
- if @condition.present?
|
||||
%table.condition-table.mt-2.width-100
|
|
@ -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
|
||||
@source_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(@source_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(@source_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(@source_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, @source_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
|
28
app/components/conditions/routing_rules_component.rb
Normal file
28
app/components/conditions/routing_rules_component.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
class Conditions::RoutingRulesComponent < Conditions::ConditionsComponent
|
||||
include Logic
|
||||
|
||||
def initialize(groupe_instructeur:)
|
||||
@groupe_instructeur = groupe_instructeur
|
||||
@condition = groupe_instructeur.routing_rule || empty_operator(empty, empty)
|
||||
@procedure_id = groupe_instructeur.procedure_id
|
||||
@source_tdcs = groupe_instructeur.procedure.active_revision.types_de_champ_public
|
||||
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
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
fr:
|
||||
display_if: Router si
|
||||
select: Sélectionner
|
||||
add_condition: Ajouter une règle de routage
|
||||
remove_a_row: Supprimer la ligne
|
|
@ -0,0 +1,25 @@
|
|||
.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
|
||||
- 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
|
|
@ -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 d’instructeurs
|
||||
|
||||
= 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),
|
||||
|
|
|
@ -134,7 +134,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)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
describe TypesDeChampEditor::ConditionsComponent, type: :component do
|
||||
describe Conditions::ChampsConditionsComponent, type: :component do
|
||||
include Logic
|
||||
|
||||
describe 'render' do
|
Loading…
Add table
Reference in a new issue