Class: Decidim::Forms::UserResponsesSerializer

Inherits:
Exporters::Serializer
  • Object
show all
Includes:
TranslationsHelper
Defined in:
lib/decidim/forms/user_responses_serializer.rb

Overview

This class serializes the responses given by a User for questionnaire so can be exported to CSV, JSON or other formats.

Instance Method Summary collapse

Constructor Details

#initialize(responses) ⇒ UserResponsesSerializer

Public: Initializes the serializer with a collection of Responses.



11
12
13
# File 'lib/decidim/forms/user_responses_serializer.rb', line 11

def initialize(responses)
  @responses = responses
end

Instance Method Details

#serializeObject

i18n-tasks-use t(‘decidim.forms.user_responses_serializer.body’) i18n-tasks-use t(‘decidim.forms.user_responses_serializer.id’) i18n-tasks-use t(‘decidim.forms.user_responses_serializer.question’) i18n-tasks-use t(‘decidim.forms.user_responses_serializer.registered’) i18n-tasks-use t(‘decidim.forms.user_responses_serializer.unregistered’) Public: Exports a hash with the serialized data for the user responses.



21
22
23
24
25
26
27
28
29
30
# File 'lib/decidim/forms/user_responses_serializer.rb', line 21

def serialize
  responses_hash = hash_for(@responses.first)
  responses_hash.merge!(questions_hash)

  @responses.each do |response|
    responses_hash[translated_question_key(response.question.position, response.question.body)] = normalize_body(response)
  end

  responses_hash
end