demarches-normaliennes/app/components/procedure/routing_rules_component.rb

83 lines
2.1 KiB
Ruby
Raw Normal View History

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|
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
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',
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',
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) }
.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)
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
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