forked from decidim/decidim
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create multiple surveys within same Survey component (decidim#13420)
Co-authored-by: Ivan Vergés <[email protected]>
- Loading branch information
1 parent
2c7da23
commit e506575
Showing
103 changed files
with
2,888 additions
and
1,197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
decidim-forms/app/commands/decidim/forms/admin/update_questions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module Forms | ||
module Admin | ||
# This command is executed when the user changes a Questionnaire questions from the admin | ||
# panel. | ||
class UpdateQuestions < Decidim::Command | ||
# Initializes a UpdateQuestions Command. | ||
# | ||
# form - The form from which to get the data. | ||
# questionnaire - The current instance of the questionnaire questions to be updated. | ||
def initialize(form, questionnaire) | ||
@form = form | ||
@questionnaire = questionnaire | ||
end | ||
|
||
# Updates the questionnaire if valid. | ||
# | ||
# Broadcasts :ok if successful, :invalid otherwise. | ||
def call | ||
return broadcast(:invalid) if @form.invalid? | ||
|
||
Decidim.traceability.perform_action!("update", | ||
@questionnaire, | ||
@form.current_user) do | ||
Decidim::Forms::Questionnaire.transaction do | ||
update_questionnaire_questions if @questionnaire.questions_editable? | ||
end | ||
end | ||
|
||
broadcast(:ok) | ||
end | ||
|
||
private | ||
|
||
def update_questionnaire_questions | ||
@form.questions.each do |form_question| | ||
update_questionnaire_question(form_question) | ||
end | ||
end | ||
|
||
def update_questionnaire_question(form_question) | ||
question_attributes = { | ||
body: form_question.body, | ||
description: form_question.description, | ||
position: form_question.position, | ||
mandatory: form_question.mandatory, | ||
question_type: form_question.question_type, | ||
max_choices: form_question.max_choices, | ||
max_characters: form_question.max_characters | ||
} | ||
|
||
update_nested_model(form_question, question_attributes, @questionnaire.questions) do |question| | ||
form_question.answer_options.each do |form_answer_option| | ||
answer_option_attributes = { | ||
body: form_answer_option.body, | ||
free_text: form_answer_option.free_text | ||
} | ||
|
||
update_nested_model(form_answer_option, answer_option_attributes, question.answer_options) | ||
end | ||
|
||
form_question.display_conditions.each do |form_display_condition| | ||
type = form_display_condition.condition_type | ||
|
||
display_condition_attributes = { | ||
condition_question: form_display_condition.condition_question, | ||
condition_type: form_display_condition.condition_type, | ||
condition_value: type == "match" ? form_display_condition.condition_value : nil, | ||
answer_option: %w(equal not_equal).include?(type) ? form_display_condition.answer_option : nil, | ||
mandatory: form_display_condition.mandatory | ||
} | ||
|
||
next if form_display_condition.deleted? && form_display_condition.id.blank? | ||
|
||
update_nested_model(form_display_condition, display_condition_attributes, question.display_conditions) | ||
end | ||
|
||
form_question.matrix_rows_by_position.each_with_index do |form_matrix_row, idx| | ||
matrix_row_attributes = { | ||
body: form_matrix_row.body, | ||
position: form_matrix_row.position || idx | ||
} | ||
|
||
update_nested_model(form_matrix_row, matrix_row_attributes, question.matrix_rows) | ||
end | ||
end | ||
end | ||
|
||
def update_nested_model(form, attributes, parent_association) | ||
record = parent_association.find_by(id: form.id) || parent_association.build(attributes) | ||
|
||
yield record if block_given? | ||
|
||
if record.persisted? | ||
if form.deleted? | ||
record.destroy! | ||
else | ||
record.update!(attributes) | ||
end | ||
else | ||
record.save! | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
decidim-forms/app/forms/decidim/forms/admin/questions_form.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module Forms | ||
module Admin | ||
# This class holds a Form to update questionnaires questions from Decidim's admin panel. | ||
class QuestionsForm < Decidim::Form | ||
attribute :questions, Array[QuestionForm] | ||
|
||
def map_model(model) | ||
self.questions = model.questions.map do |question| | ||
QuestionForm.from_model(question) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.