2022-07-04 11:27:39 +02:00
|
|
|
module Administrateurs
|
|
|
|
class ConditionsController < AdministrateurController
|
|
|
|
include Logic
|
|
|
|
|
|
|
|
before_action :retrieve_procedure, :retrieve_coordinate_and_uppers
|
|
|
|
|
|
|
|
def update
|
|
|
|
condition = condition_form.to_condition
|
2022-07-11 21:57:43 +02:00
|
|
|
@tdc.update!(condition: condition)
|
2022-07-04 11:27:39 +02:00
|
|
|
|
2022-07-11 21:57:43 +02:00
|
|
|
@condition_component = build_condition_component
|
2022-07-04 11:27:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_row
|
2022-07-11 21:57:43 +02:00
|
|
|
condition = Logic.add_empty_condition_to(@tdc.condition)
|
|
|
|
@tdc.update!(condition: condition)
|
2022-07-04 11:27:39 +02:00
|
|
|
|
2022-07-11 21:57:43 +02:00
|
|
|
@condition_component = build_condition_component
|
2022-07-04 11:27:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_row
|
|
|
|
condition = condition_form.delete_row(row_index).to_condition
|
2022-07-11 21:57:43 +02:00
|
|
|
@tdc.update!(condition: condition)
|
2022-07-04 11:27:39 +02:00
|
|
|
|
2022-07-11 21:57:43 +02:00
|
|
|
@condition_component = build_condition_component
|
2022-07-04 11:27:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2022-07-11 21:57:43 +02:00
|
|
|
@tdc.update!(condition: nil)
|
2022-07-04 11:27:39 +02:00
|
|
|
|
2022-07-11 21:57:43 +02:00
|
|
|
@condition_component = build_condition_component
|
2022-07-04 11:27:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def change_targeted_champ
|
|
|
|
condition = condition_form.change_champ(row_index).to_condition
|
2022-07-11 21:57:43 +02:00
|
|
|
@tdc.update!(condition: condition)
|
2022-07-04 11:27:39 +02:00
|
|
|
|
2022-07-11 21:57:43 +02:00
|
|
|
@condition_component = build_condition_component
|
2022-07-04 11:27:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-07-11 21:57:43 +02:00
|
|
|
def build_condition_component
|
|
|
|
TypesDeChampEditor::ConditionsComponent.new(
|
|
|
|
tdc: @tdc,
|
|
|
|
upper_tdcs: @upper_tdcs,
|
|
|
|
procedure_id: @procedure.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-07-04 11:27:39 +02:00
|
|
|
def condition_form
|
2022-09-26 21:23:10 +02:00
|
|
|
ConditionForm.new(condition_params.merge({ upper_tdcs: @upper_tdcs }))
|
2022-07-04 11:27:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_coordinate_and_uppers
|
2022-07-11 21:57:43 +02:00
|
|
|
@tdc = draft_revision.find_and_ensure_exclusive_use(params[:stable_id])
|
|
|
|
@coordinate = draft_revision.coordinate_for(@tdc)
|
2022-07-12 13:56:32 +02:00
|
|
|
@upper_tdcs = @coordinate.upper_siblings.map(&:type_de_champ)
|
2022-07-04 11:27:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def draft_revision
|
|
|
|
@procedure.draft_revision
|
|
|
|
end
|
|
|
|
|
|
|
|
def condition_params
|
|
|
|
params
|
|
|
|
.require(:type_de_champ)
|
|
|
|
.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
|