demarches-normaliennes/app/controllers/administrateurs/conditions_controller.rb
simon lehericey 564939e83a fix: allow conditional on first child of bloc based on the first champ
The upper_coordinates were wrong in administrateur/type_de_champ_controller. The upper coordinates outside the bloc where missing.

with the following tdc:
- number
- bloc
  - text # this text can be conditioned by the number
2023-10-03 15:17:50 +02:00

77 lines
1.9 KiB
Ruby

module Administrateurs
class ConditionsController < AdministrateurController
include Logic
before_action :retrieve_procedure, :retrieve_coordinate_and_uppers
after_action :reset_procedure
def update
condition = condition_form.to_condition
@tdc.update!(condition: condition)
@condition_component = build_condition_component
end
def add_row
condition = Logic.add_empty_condition_to(@tdc.condition)
@tdc.update!(condition: condition)
@condition_component = build_condition_component
end
def delete_row
condition = condition_form.delete_row(row_index).to_condition
@tdc.update!(condition: condition)
@condition_component = build_condition_component
end
def destroy
@tdc.update!(condition: nil)
@condition_component = build_condition_component
end
def change_targeted_champ
condition = condition_form.change_champ(row_index).to_condition
@tdc.update!(condition: condition)
@condition_component = build_condition_component
end
private
def build_condition_component
TypesDeChampEditor::ConditionsComponent.new(
tdc: @tdc,
upper_tdcs: @upper_tdcs,
procedure_id: @procedure.id
)
end
def condition_form
ConditionForm.new(condition_params.merge({ upper_tdcs: @upper_tdcs }))
end
def retrieve_coordinate_and_uppers
@tdc = draft_revision.find_and_ensure_exclusive_use(params[:stable_id])
@coordinate = draft_revision.coordinate_for(@tdc)
@upper_tdcs = @coordinate.upper_coordinates.map(&:type_de_champ)
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