2023-03-22 17:33:14 +01:00
|
|
|
class Procedure::RoutingRulesComponent < ApplicationComponent
|
|
|
|
include Logic
|
|
|
|
|
|
|
|
def initialize(revision:, groupe_instructeurs:)
|
|
|
|
@revision = revision
|
|
|
|
@groupe_instructeurs = groupe_instructeurs
|
2023-04-05 17:21:55 +02:00
|
|
|
@procedure_id = revision.procedure_id
|
2023-03-22 17:33:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def rows
|
|
|
|
@groupe_instructeurs.active.map do |gi|
|
2023-04-07 09:51:19 +02:00
|
|
|
if gi.routing_rule.present?
|
|
|
|
[gi.routing_rule.left, gi.routing_rule.right, gi]
|
|
|
|
else
|
|
|
|
[empty, empty, gi]
|
|
|
|
end
|
2023-03-22 17:33:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-27 16:01:39 +02:00
|
|
|
def can_route?
|
|
|
|
available_targets_for_select.present?
|
|
|
|
end
|
|
|
|
|
2023-03-22 17:33:14 +01:00
|
|
|
def targeted_champ_tag(targeted_champ, row_index)
|
|
|
|
select_tag(
|
|
|
|
'targeted_champ',
|
2023-04-07 09:51:19 +02:00
|
|
|
options_for_select(targeted_champs_for_select, selected: targeted_champ.to_json),
|
2023-03-22 17:33:14 +01:00
|
|
|
id: input_id_for('targeted_champ', row_index)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def value_tag(targeted_champ, value, row_index)
|
|
|
|
select_tag(
|
|
|
|
'value',
|
2023-04-07 09:53:46 +02:00
|
|
|
options_for_select(
|
|
|
|
values_for_select(targeted_champ, row_index),
|
|
|
|
selected: value.to_json
|
|
|
|
),
|
2023-03-22 17:33:14 +01:00
|
|
|
id: input_id_for('value', row_index)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def hidden_groupe_instructeur_tag(groupe_instructeur_id)
|
|
|
|
hidden_field_tag(
|
|
|
|
'groupe_instructeur_id',
|
|
|
|
groupe_instructeur_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def targeted_champs_for_select
|
|
|
|
empty_target_for_select + available_targets_for_select
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty_target_for_select
|
|
|
|
[[t('.select'), empty.to_json]]
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_targets_for_select
|
|
|
|
@revision.types_de_champ_public
|
|
|
|
.filter { |tdc| [:drop_down_list].include?(tdc.type_champ.to_sym) }
|
2023-04-07 09:51:19 +02:00
|
|
|
.map { |tdc| [tdc.libelle, champ_value(tdc.stable_id).to_json] }
|
2023-03-22 17:33:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def available_values_for_select(targeted_champ)
|
2023-04-07 09:51:19 +02:00
|
|
|
return [] if targeted_champ.is_a?(Logic::Empty)
|
|
|
|
targeted_champ
|
|
|
|
.options(@revision.types_de_champ_public)
|
|
|
|
.map { |tdc| [tdc.first, constant(tdc.first).to_json] }
|
2023-03-22 17:33:14 +01:00
|
|
|
end
|
|
|
|
|
2023-04-07 09:53:46 +02:00
|
|
|
def values_for_select(targeted_champ, row_index)
|
|
|
|
(empty_target_for_select + available_values_for_select(targeted_champ))
|
|
|
|
# add id to help morph render selected option
|
|
|
|
.map { |(libelle, json)| [libelle, json, { id: "#{row_index}-option-#{libelle}" }] }
|
2023-03-22 17:33:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def input_id_for(name, row_index)
|
|
|
|
"#{name}-#{row_index}"
|
|
|
|
end
|
|
|
|
end
|