2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-10-13 11:30:22 +02:00
|
|
|
class Conditions::ConditionsComponent < ApplicationComponent
|
2022-07-06 09:55:08 +02:00
|
|
|
include Logic
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def rows
|
|
|
|
condition_per_row.map { |c| Logic.split_condition(c) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def condition_per_row
|
|
|
|
if [And, Or].include?(@condition.class)
|
|
|
|
@condition.operands
|
|
|
|
else
|
|
|
|
[@condition].compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def far_left_tag(row_number)
|
|
|
|
if row_number == 0
|
2022-07-06 17:19:04 +02:00
|
|
|
t('.display_if')
|
2022-07-06 09:55:08 +02:00
|
|
|
elsif row_number == 1
|
|
|
|
select_tag(
|
|
|
|
"#{input_prefix}[top_operator_name]",
|
2023-09-19 11:09:29 +02:00
|
|
|
options_for_select(options_for_far_left_tag, @condition.class.name),
|
|
|
|
class: 'fr-select'
|
2022-07-06 09:55:08 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-06 17:19:04 +02:00
|
|
|
def options_for_far_left_tag
|
|
|
|
[And, Or]
|
|
|
|
.map(&:name)
|
|
|
|
.map { |name| [t(name, scope: 'logic.operators'), name] }
|
|
|
|
end
|
|
|
|
|
2022-07-06 09:55:08 +02:00
|
|
|
def left_operand_tag(targeted_champ, row_index)
|
2022-07-06 17:39:31 +02:00
|
|
|
# current_target can be invalid if
|
|
|
|
# - its type has changed : number -> carto
|
|
|
|
# - it has been removed
|
|
|
|
# - it has been put lower in the form
|
|
|
|
current_target_valid = targets_for_select.map(&:second).include?(targeted_champ.to_json)
|
2022-06-28 12:52:03 +02:00
|
|
|
|
|
|
|
selected_target = current_target_valid ? targeted_champ.to_json : empty.to_json
|
|
|
|
|
2022-07-06 09:55:08 +02:00
|
|
|
select_tag(
|
|
|
|
input_name_for('targeted_champ'),
|
2022-07-06 17:39:38 +02:00
|
|
|
options_for_select(targets_for_select, selected_target),
|
2022-07-06 09:55:08 +02:00
|
|
|
onchange: "this.form.action = this.form.action + '/change_targeted_champ?row_index=#{row_index}'",
|
2022-06-28 12:52:03 +02:00
|
|
|
id: input_id_for('targeted_champ', row_index),
|
2023-09-19 11:09:29 +02:00
|
|
|
class: { 'fr-select': true, alert: !current_target_valid }
|
2022-07-06 09:55:08 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-07-06 17:39:38 +02:00
|
|
|
def targets_for_select
|
2022-07-06 20:26:34 +02:00
|
|
|
empty_target_for_select + available_targets_for_select
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty_target_for_select
|
|
|
|
[[t('.select'), empty.to_json]]
|
2022-06-28 12:52:03 +02:00
|
|
|
end
|
|
|
|
|
2022-07-06 17:39:38 +02:00
|
|
|
def available_targets_for_select
|
2023-10-13 11:30:22 +02:00
|
|
|
@source_tdcs
|
2024-06-05 17:08:00 +02:00
|
|
|
.filter(&:conditionable?)
|
2022-06-28 12:52:03 +02:00
|
|
|
.map { |tdc| [tdc.libelle, champ_value(tdc.stable_id).to_json] }
|
2022-07-06 09:55:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def operator_tag(operator_name, targeted_champ, row_index)
|
2022-07-06 17:39:38 +02:00
|
|
|
operators_for_select = compatibles_operators_for_select(targeted_champ)
|
2022-06-27 15:09:27 +02:00
|
|
|
|
2022-07-07 10:38:14 +02:00
|
|
|
current_operator_invalid = !operators_for_select.map(&:second).include?(operator_name)
|
2022-06-27 15:09:27 +02:00
|
|
|
|
2022-07-07 10:38:14 +02:00
|
|
|
if current_operator_invalid
|
|
|
|
operators_for_select = [[t('.select'), EmptyOperator.name]] + operators_for_select
|
2022-06-27 15:09:27 +02:00
|
|
|
end
|
|
|
|
|
2022-07-06 09:55:08 +02:00
|
|
|
select_tag(
|
|
|
|
input_name_for('operator_name'),
|
2022-07-06 17:39:38 +02:00
|
|
|
options_for_select(operators_for_select, selected: operator_name),
|
2022-06-27 15:09:27 +02:00
|
|
|
id: input_id_for('operator_name', row_index),
|
2023-09-19 11:09:29 +02:00
|
|
|
class: { 'fr-select': true, alert: current_operator_invalid }
|
2022-07-06 09:55:08 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-07-06 17:39:38 +02:00
|
|
|
def compatibles_operators_for_select(left)
|
2023-10-13 11:30:22 +02:00
|
|
|
case left.type(@source_tdcs)
|
2022-07-06 09:55:08 +02:00
|
|
|
when ChampValue::CHAMP_VALUE_TYPE.fetch(:boolean)
|
|
|
|
[
|
2022-07-06 17:19:04 +02:00
|
|
|
[t('is', scope: 'logic'), Eq.name]
|
2022-07-06 09:55:08 +02:00
|
|
|
]
|
|
|
|
when ChampValue::CHAMP_VALUE_TYPE.fetch(:empty)
|
|
|
|
[
|
2022-07-06 17:19:04 +02:00
|
|
|
[t('is', scope: 'logic'), EmptyOperator.name]
|
2022-07-06 09:55:08 +02:00
|
|
|
]
|
|
|
|
when ChampValue::CHAMP_VALUE_TYPE.fetch(:enum)
|
|
|
|
[
|
2022-08-09 15:41:42 +02:00
|
|
|
[t('is', scope: 'logic'), Eq.name],
|
|
|
|
[t('is_not', scope: 'logic'), NotEq.name]
|
2022-07-06 09:55:08 +02:00
|
|
|
]
|
2024-01-29 15:15:18 +01:00
|
|
|
when ChampValue::CHAMP_VALUE_TYPE.fetch(:commune_enum), ChampValue::CHAMP_VALUE_TYPE.fetch(:epci_enum), ChampValue::CHAMP_VALUE_TYPE.fetch(:address)
|
2023-12-06 17:11:32 +01:00
|
|
|
[
|
|
|
|
[t(InDepartementOperator.name, scope: 'logic.operators'), InDepartementOperator.name],
|
2023-12-08 14:03:06 +01:00
|
|
|
[t(NotInDepartementOperator.name, scope: 'logic.operators'), NotInDepartementOperator.name],
|
|
|
|
[t(InRegionOperator.name, scope: 'logic.operators'), InRegionOperator.name],
|
|
|
|
[t(NotInRegionOperator.name, scope: 'logic.operators'), NotInRegionOperator.name]
|
2023-12-06 17:11:32 +01:00
|
|
|
]
|
|
|
|
when ChampValue::CHAMP_VALUE_TYPE.fetch(:departement_enum)
|
|
|
|
[
|
|
|
|
[t('is', scope: 'logic'), Eq.name],
|
|
|
|
[t('is_not', scope: 'logic'), NotEq.name],
|
2023-12-08 14:03:06 +01:00
|
|
|
[t(InRegionOperator.name, scope: 'logic.operators'), InRegionOperator.name],
|
|
|
|
[t(NotInRegionOperator.name, scope: 'logic.operators'), NotInRegionOperator.name]
|
2023-12-06 17:11:32 +01:00
|
|
|
]
|
2022-09-09 15:36:50 +02:00
|
|
|
when ChampValue::CHAMP_VALUE_TYPE.fetch(:enums)
|
|
|
|
[
|
2023-11-15 17:44:46 +01:00
|
|
|
[t(IncludeOperator.name, scope: 'logic.operators'), IncludeOperator.name],
|
|
|
|
[t(ExcludeOperator.name, scope: 'logic.operators'), ExcludeOperator.name]
|
2022-09-09 15:36:50 +02:00
|
|
|
]
|
2022-07-06 09:55:08 +02:00
|
|
|
when ChampValue::CHAMP_VALUE_TYPE.fetch(:number)
|
|
|
|
[Eq, LessThan, GreaterThan, LessThanEq, GreaterThanEq]
|
|
|
|
.map(&:name)
|
|
|
|
.map { |name| [t(name, scope: 'logic.operators'), name] }
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-06 17:11:32 +01:00
|
|
|
def right_operand_tag(left, right, row_index, operator_name)
|
2022-07-07 10:38:14 +02:00
|
|
|
right_invalid = !current_right_valid?(left, right)
|
2022-06-27 15:51:54 +02:00
|
|
|
|
2023-10-13 11:30:22 +02:00
|
|
|
case left.type(@source_tdcs)
|
2022-07-06 09:55:08 +02:00
|
|
|
when :boolean
|
2022-07-07 10:46:38 +02:00
|
|
|
booleans_for_select = [[t('utils.yes'), constant(true).to_json], [t('utils.no'), constant(false).to_json]]
|
2022-06-27 15:51:54 +02:00
|
|
|
|
2022-07-07 10:38:14 +02:00
|
|
|
if right_invalid
|
2022-07-07 10:46:38 +02:00
|
|
|
booleans_for_select = empty_target_for_select + booleans_for_select
|
2022-06-27 15:51:54 +02:00
|
|
|
end
|
|
|
|
|
2022-07-06 09:55:08 +02:00
|
|
|
select_tag(
|
|
|
|
input_name_for('value'),
|
2022-07-07 10:46:38 +02:00
|
|
|
options_for_select(booleans_for_select, right.to_json),
|
2022-06-27 15:51:54 +02:00
|
|
|
id: input_id_for('value', row_index),
|
2023-09-19 11:09:29 +02:00
|
|
|
class: { 'fr-select': true, alert: right_invalid }
|
2022-07-06 09:55:08 +02:00
|
|
|
)
|
|
|
|
when :empty
|
|
|
|
select_tag(
|
|
|
|
input_name_for('value'),
|
2022-07-07 10:46:38 +02:00
|
|
|
options_for_select(empty_target_for_select),
|
2023-09-19 11:09:29 +02:00
|
|
|
id: input_id_for('value', row_index),
|
|
|
|
class: 'fr-select'
|
2022-07-06 09:55:08 +02:00
|
|
|
)
|
2024-01-29 15:15:18 +01:00
|
|
|
when :enum, :enums, :commune_enum, :epci_enum, :departement_enum, :address
|
2023-12-06 17:11:32 +01:00
|
|
|
enums_for_select = left.options(@source_tdcs, operator_name)
|
2022-06-27 15:51:54 +02:00
|
|
|
|
2022-07-07 10:38:14 +02:00
|
|
|
if right_invalid
|
2022-07-07 10:46:38 +02:00
|
|
|
enums_for_select = empty_target_for_select + enums_for_select
|
2022-06-27 15:51:54 +02:00
|
|
|
end
|
|
|
|
|
2022-07-06 09:55:08 +02:00
|
|
|
select_tag(
|
|
|
|
input_name_for('value'),
|
2022-07-07 10:46:38 +02:00
|
|
|
options_for_select(enums_for_select, right.value),
|
2022-06-27 15:51:54 +02:00
|
|
|
id: input_id_for('value', row_index),
|
2023-09-19 11:09:29 +02:00
|
|
|
class: { 'fr-select': true, alert: right_invalid }
|
2022-07-06 09:55:08 +02:00
|
|
|
)
|
|
|
|
when :number
|
2022-09-14 16:28:00 +02:00
|
|
|
text_field_tag(
|
2022-06-27 15:51:54 +02:00
|
|
|
input_name_for('value'),
|
|
|
|
right.value,
|
|
|
|
required: true,
|
|
|
|
id: input_id_for('value', row_index),
|
2023-09-19 11:09:29 +02:00
|
|
|
class: { 'fr-select': true, alert: right_invalid }
|
2022-06-27 15:51:54 +02:00
|
|
|
)
|
2022-07-06 09:55:08 +02:00
|
|
|
else
|
2023-09-19 11:09:29 +02:00
|
|
|
text_field_tag(input_name_for('value'), '', id: input_id_for('value', row_index), class: 'fr-input')
|
2022-07-06 09:55:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-27 15:51:54 +02:00
|
|
|
def current_right_valid?(left, right)
|
2023-10-13 11:30:22 +02:00
|
|
|
Logic.compatible_type?(left, right, @source_tdcs)
|
2022-06-27 15:51:54 +02:00
|
|
|
end
|
|
|
|
|
2022-07-06 09:55:08 +02:00
|
|
|
def add_condition_tag
|
2022-06-28 12:39:05 +02:00
|
|
|
tag.button(
|
2022-10-12 17:44:24 +02:00
|
|
|
t('.add_condition'),
|
2023-10-13 11:30:22 +02:00
|
|
|
formaction: add_condition_path,
|
2022-06-28 12:39:05 +02:00
|
|
|
formnovalidate: true,
|
2022-10-12 17:44:24 +02:00
|
|
|
class: 'fr-btn fr-btn--secondary fr-btn--sm fr-icon-add-circle-line fr-btn--icon-left'
|
2022-06-28 12:39:05 +02:00
|
|
|
)
|
2022-07-06 09:55:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_condition_tag(row_index)
|
2022-06-28 13:03:11 +02:00
|
|
|
tag.button(
|
2023-11-27 17:06:29 +01:00
|
|
|
class: "fr-btn fr-btn--sm fr-btn--tertiary fr-icon-delete-line",
|
|
|
|
title: t('.remove_a_row'),
|
2023-10-13 11:30:22 +02:00
|
|
|
formaction: delete_condition_path(row_index),
|
2022-06-28 13:03:11 +02:00
|
|
|
formmethod: 'delete',
|
|
|
|
formnovalidate: true
|
|
|
|
)
|
2022-07-06 09:55:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def render?
|
2022-07-12 13:56:53 +02:00
|
|
|
@condition.present? || available_targets_for_select.any?
|
2022-07-06 09:55:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def input_name_for(name)
|
|
|
|
"#{input_prefix}[rows][][#{name}]"
|
|
|
|
end
|
|
|
|
end
|