|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "active_support/concern" |
| 4 | +module DecidimZuerich |
| 5 | + module Forms |
| 6 | + module Admin |
| 7 | + # This command is executed when the user changes a Questionnaire from the admin |
| 8 | + # panel. |
| 9 | + module UpdateQuestionnaire |
| 10 | + extend ActiveSupport::Concern |
| 11 | + |
| 12 | + included do |
| 13 | + def update_display_conditions(form_question, question) |
| 14 | + # Remove all existing conditions for this question to avoid association issues |
| 15 | + question.display_conditions.destroy_all |
| 16 | + |
| 17 | + # Recreate conditions from form data |
| 18 | + form_question.display_conditions.each do |form_display_condition| |
| 19 | + # Skip conditions marked for deletion |
| 20 | + next if form_display_condition.deleted? |
| 21 | + |
| 22 | + type = form_display_condition.condition_type |
| 23 | + |
| 24 | + display_condition_attributes = { |
| 25 | + condition_question: form_display_condition.condition_question, |
| 26 | + condition_type: form_display_condition.condition_type, |
| 27 | + condition_value: type == "match" ? form_display_condition.condition_value : nil, |
| 28 | + answer_option: %w(equal not_equal).include?(type) ? form_display_condition.answer_option : nil, |
| 29 | + mandatory: form_display_condition.mandatory |
| 30 | + } |
| 31 | + |
| 32 | + # Create new condition and associate it explicitly to the correct question to avoid that |
| 33 | + # it goes to the question the condition was about |
| 34 | + new_condition = question.display_conditions.build(display_condition_attributes) |
| 35 | + new_condition.save! |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + def update_questionnaire_question(form_question) |
| 40 | + question_attributes = { |
| 41 | + body: form_question.body, |
| 42 | + description: form_question.description, |
| 43 | + position: form_question.position, |
| 44 | + mandatory: form_question.mandatory, |
| 45 | + question_type: form_question.question_type, |
| 46 | + max_choices: form_question.max_choices, |
| 47 | + max_characters: form_question.max_characters |
| 48 | + } |
| 49 | + |
| 50 | + update_nested_model(form_question, question_attributes, @questionnaire.questions) do |question| |
| 51 | + form_question.answer_options.each do |form_answer_option| |
| 52 | + answer_option_attributes = { |
| 53 | + body: form_answer_option.body, |
| 54 | + free_text: form_answer_option.free_text |
| 55 | + } |
| 56 | + |
| 57 | + update_nested_model(form_answer_option, answer_option_attributes, question.answer_options) |
| 58 | + end |
| 59 | + |
| 60 | + # FIX: Collect IDs of display_conditions to preserve |
| 61 | + form_display_condition_ids = form_question.display_conditions |
| 62 | + .reject { |dc| dc.deleted? && dc.id.blank? } |
| 63 | + .map(&:id) |
| 64 | + .compact |
| 65 | + |
| 66 | + # FIX: Remove display_conditions no longer present in form |
| 67 | + question.display_conditions.where.not(id: form_display_condition_ids).destroy_all if form_display_condition_ids.any? |
| 68 | + |
| 69 | + # FIX: Updated display_conditions handling |
| 70 | + update_display_conditions(form_question, question) |
| 71 | + |
| 72 | + form_question.matrix_rows_by_position.each_with_index do |form_matrix_row, idx| |
| 73 | + matrix_row_attributes = { |
| 74 | + body: form_matrix_row.body, |
| 75 | + position: form_matrix_row.position || idx |
| 76 | + } |
| 77 | + |
| 78 | + update_nested_model(form_matrix_row, matrix_row_attributes, question.matrix_rows) |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments