From 8ccbe6f728f11c2b295a0896252c585c56ca9fed Mon Sep 17 00:00:00 2001 From: stephanie rousset Date: Mon, 28 Oct 2024 13:39:41 +0100 Subject: [PATCH 1/5] chore: update after pull --- db/schema.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/schema.rb b/db/schema.rb index 7b40047a8a..af643bb11c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,6 +11,7 @@ # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2024_10_10_092645) do + # These are extensions that must be enabled in order to support this database enable_extension "ltree" enable_extension "pg_trgm" From d0c4f1686ebd37b699d9f34ee30f166478efaf21 Mon Sep 17 00:00:00 2001 From: stephanie rousset Date: Mon, 28 Oct 2024 18:13:38 +0100 Subject: [PATCH 2/5] fix: order questions by position in serializer --- .../forms/user_answers_serializer_extend.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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, From 85ad39c01a9b2277ebeaab393fd882e3d55b12ec Mon Sep 17 00:00:00 2001 From: stephanie rousset Date: Mon, 28 Oct 2024 18:14:57 +0100 Subject: [PATCH 3/5] test: add test for question order --- .../decidim/forms/user_answers_serializer_spec.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/serializers/decidim/forms/user_answers_serializer_spec.rb b/spec/serializers/decidim/forms/user_answers_serializer_spec.rb index 581372dbc9..e11c716229 100644 --- a/spec/serializers/decidim/forms/user_answers_serializer_spec.rb +++ b/spec/serializers/decidim/forms/user_answers_serializer_spec.rb @@ -67,6 +67,7 @@ module Forms end describe "#serialize" do + let(:serialized) { subject.serialize } it "includes the answer for each question" do @@ -86,7 +87,7 @@ module Forms end serialized_files_answer = files_answer.attachments.map(&:url) - + expect(serialized).to include( "#{multichoice_question.position + 1}. #{translated(multichoice_question.body, locale: I18n.locale)}" => [multichoice_answer_choices.first.body, multichoice_answer_choices.last.body] ) @@ -102,6 +103,7 @@ module Forms expect(serialized).to include( "#{files_question.position + 1}. #{translated(files_question.body, locale: I18n.locale)}" => serialized_files_answer ) + end context "and includes the attributes" do @@ -209,6 +211,14 @@ 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{|q| q.position}.map{|question| question.position + 1}) + end + end end end end From 10213d88a09a554057d2434b5cecbb40b11e88c3 Mon Sep 17 00:00:00 2001 From: stephanie rousset Date: Tue, 29 Oct 2024 09:11:00 +0100 Subject: [PATCH 4/5] refactor: update test --- .../decidim/forms/user_answers_serializer_spec.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/spec/serializers/decidim/forms/user_answers_serializer_spec.rb b/spec/serializers/decidim/forms/user_answers_serializer_spec.rb index e11c716229..6badfad42e 100644 --- a/spec/serializers/decidim/forms/user_answers_serializer_spec.rb +++ b/spec/serializers/decidim/forms/user_answers_serializer_spec.rb @@ -67,7 +67,6 @@ module Forms end describe "#serialize" do - let(:serialized) { subject.serialize } it "includes the answer for each question" do @@ -87,7 +86,7 @@ module Forms end serialized_files_answer = files_answer.attachments.map(&:url) - + expect(serialized).to include( "#{multichoice_question.position + 1}. #{translated(multichoice_question.body, locale: I18n.locale)}" => [multichoice_answer_choices.first.body, multichoice_answer_choices.last.body] ) @@ -103,7 +102,6 @@ module Forms expect(serialized).to include( "#{files_question.position + 1}. #{translated(files_question.body, locale: I18n.locale)}" => serialized_files_answer ) - end context "and includes the attributes" do @@ -213,10 +211,9 @@ def memory_usage 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{|q| q.position}.map{|question| question.position + 1}) + 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 From 4076cc69932cf70dca3ff267c565731da4c7a7f3 Mon Sep 17 00:00:00 2001 From: stephanie rousset Date: Wed, 30 Oct 2024 13:56:28 +0100 Subject: [PATCH 5/5] style: remove empty line --- db/schema.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index af643bb11c..7b40047a8a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,6 @@ # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2024_10_10_092645) do - # These are extensions that must be enabled in order to support this database enable_extension "ltree" enable_extension "pg_trgm"