diff --git a/lib/extends/lib/decidim/forms/user_answers_serializer_extend.rb b/lib/extends/lib/decidim/forms/user_answers_serializer_extend.rb index 57345d30c2..31a0d7efc5 100644 --- a/lib/extends/lib/decidim/forms/user_answers_serializer_extend.rb +++ b/lib/extends/lib/decidim/forms/user_answers_serializer_extend.rb @@ -6,6 +6,20 @@ module UserAnswersSerializerExtends included do private + def questions_hash + questionnaire_id = @answers.first&.decidim_questionnaire_id + return {} unless questionnaire_id + + questions = Decidim::Forms::Question.where(decidim_questionnaire_id: questionnaire_id).order(:position) + return {} if questions.none? + + questions.each.inject({}) do |serialized, question| + serialized.update( + translated_question_key(question.position, question.body) => "" + ) + end + end + def hash_for(answer) { answer_translated_attribute_name(:id) => answer&.session_token, diff --git a/spec/serializers/decidim/forms/user_answers_serializer_spec.rb b/spec/serializers/decidim/forms/user_answers_serializer_spec.rb index 581372dbc9..6badfad42e 100644 --- a/spec/serializers/decidim/forms/user_answers_serializer_spec.rb +++ b/spec/serializers/decidim/forms/user_answers_serializer_spec.rb @@ -209,6 +209,13 @@ def memory_usage end end end + + describe "questions_hash" do + it "generates a hash of questions ordered by position" do + questions.shuffle! + expect(subject.instance_eval { questions_hash }.keys.map { |key| key[0].to_i }.uniq).to eq(questions.sort_by(&:position).map { |question| question.position + 1 }) + end + end end end end