Module: Decidim::Templates::Admin::QuestionnaireCopier
- Included in:
- ApplyQuestionnaireTemplate, CopyQuestionnaireTemplate
- Defined in:
- app/commands/decidim/templates/admin/questionnaire_copier.rb
Instance Method Summary collapse
- #copy_question_display_conditions(original_question, destination_question) ⇒ Object
- #copy_questionnaire_answer_options(original_question, new_question) ⇒ Object
- #copy_questionnaire_matrix_rows(original_question, new_question) ⇒ Object
- #copy_questionnaire_questions(original_questionnaire, new_questionnaire) ⇒ Object
- #find_answer_option_by_body(answer_options, body) ⇒ Object
- #find_question_by_position(questions, position) ⇒ Object
Instance Method Details
#copy_question_display_conditions(original_question, destination_question) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/commands/decidim/templates/admin/questionnaire_copier.rb', line 46 def copy_question_display_conditions(original_question, destination_question) original_question.display_conditions.each do |original_display_condition| new_display_condition = original_display_condition.dup new_display_condition.question = destination_question destination_question_to_be_checked = find_question_by_position(destination_question.questionnaire.questions, original_display_condition.condition_question.position) new_display_condition.condition_question = destination_question_to_be_checked if original_display_condition.answer_option new_display_condition.answer_option = find_answer_option_by_body(destination_question_to_be_checked., original_display_condition.answer_option.body) end new_display_condition.save! destination_question.display_conditions << new_display_condition end end |
#copy_questionnaire_answer_options(original_question, new_question) ⇒ Object
30 31 32 33 34 35 36 |
# File 'app/commands/decidim/templates/admin/questionnaire_copier.rb', line 30 def (original_question, new_question) original_question..each do |original_answer_option| new_answer_option = original_answer_option.dup new_answer_option.question = new_question new_answer_option.save! end end |
#copy_questionnaire_matrix_rows(original_question, new_question) ⇒ Object
38 39 40 41 42 43 44 |
# File 'app/commands/decidim/templates/admin/questionnaire_copier.rb', line 38 def copy_questionnaire_matrix_rows(original_question, new_question) original_question.matrix_rows.each do |original_matrix_row| new_matrix_row = original_matrix_row.dup new_matrix_row.question = new_question new_matrix_row.save! end end |
#copy_questionnaire_questions(original_questionnaire, new_questionnaire) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/commands/decidim/templates/admin/questionnaire_copier.rb', line 8 def copy_questionnaire_questions(original_questionnaire, new_questionnaire) # start by copying the questions so that they already exist when cross referencing them in the conditions original_questionnaire.reload.questions.includes(:answer_options, :matrix_rows, :display_conditions) original_questionnaire.questions.each do |original_question| new_question = original_question.dup new_question.questionnaire = new_questionnaire new_question.assign_attributes( answer_options_count: 0, matrix_rows_count: 0, display_conditions_count: 0, display_conditions_for_other_questions_count: 0 ) new_question.save! (original_question, new_question) copy_questionnaire_matrix_rows(original_question, new_question) end # once all questions are copied, copy display conditions original_questionnaire.questions.zip(new_questionnaire.questions.load).each do |original_question, new_question| copy_question_display_conditions(original_question, new_question) end end |
#find_answer_option_by_body(answer_options, body) ⇒ Object
66 67 68 |
# File 'app/commands/decidim/templates/admin/questionnaire_copier.rb', line 66 def find_answer_option_by_body(, body) .to_a.find { |ao| ao.body == body } end |
#find_question_by_position(questions, position) ⇒ Object
62 63 64 |
# File 'app/commands/decidim/templates/admin/questionnaire_copier.rb', line 62 def find_question_by_position(questions, position) questions.to_a.find { |q| q.position == position } end |