Class: Decidim::Meetings::UserResponsesSerializer

Inherits:
Exporters::Serializer
  • Object
show all
Includes:
TranslationsHelper
Defined in:
lib/decidim/meetings/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/meetings/user_responses_serializer.rb', line 11

def initialize(responses)
  @responses = responses
end

Instance Method Details

#serializeObject

Public: Exports a hash with the serialized data for the user responses.



16
17
18
19
20
21
22
23
24
25
# File 'lib/decidim/meetings/user_responses_serializer.rb', line 16

def serialize
  @responses.each_with_index.inject({}) do |serialized, (response, idx)|
    serialized.update(
      response_translated_attribute_name(:id) => [response.id, response.user.id].join("_"),
      response_translated_attribute_name(:created_at) => response.created_at,
      response_translated_attribute_name(:user_status) => response_translated_attribute_name(response.decidim_user_id.present? ? "registered" : "unregistered"),
      "#{idx + 1}. #{translated_attribute(response.question.body)}" => normalize_body(response)
    )
  end
end