Sha256: 72d0838b56a424fac3a17c188f6e8786f96ca2ddaef9b74adc44c10bc4fdceaa
Contents?: true
Size: 913 Bytes
Versions: 14
Compression:
Stored size: 913 Bytes
Contents
# frozen_string_literal: true module Decidim module Surveys # This class serializes the answers given by a User for survey so can be # exported to CSV, JSON or other formats. class SurveyUserAnswersSerializer < Decidim::Exporters::Serializer include Decidim::TranslationsHelper # Public: Initializes the serializer with a collection of SurveyAnswers. def initialize(survey_answers) @survey_answers = survey_answers end # Public: Exports a hash with the serialized data for the user answers. def serialize @survey_answers.each_with_index.inject({}) do |serialized, (answer, idx)| serialized.update("#{idx + 1}. #{translated_attribute(answer.question.body)}" => normalize_body(answer)) end end private def normalize_body(answer) answer.body || answer.choices.pluck(:body) end end end end
Version data entries
14 entries across 14 versions & 1 rubygems