Sha256: fa06674cb6a4a24de141d41beca51152469ca3e9d84a84fc9250a68257f3c948

Contents?: true

Size: 1.29 KB

Versions: 25

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Decidim
  module Forms
    class DownloadYourDataUserAnswersSerializer < Decidim::Exporters::Serializer
      include Decidim::TranslationsHelper
      # Serializes an user answer for download your data
      def serialize
        {
          id: resource.id,
          user: {
            name: resource.user.name,
            email: resource.user.email
          },
          questionnaire: {
            id: resource.question.questionnaire.id,
            title: translated_attribute(resource.question.questionnaire.title),
            description: translated_attribute(resource.question.questionnaire.description),
            tos: translated_attribute(resource.question.questionnaire.tos)
          },
          question: {
            id: resource.question.id,
            body: translated_attribute(resource.question.body),
            description: translated_attribute(resource.question.description)
          },
          answer: normalize_body(resource)
        }
      end

      private

      def normalize_body(resource)
        attachments_for(resource) || resource.body || resource.choices.pluck(:body)
      end

      def attachments_for(resource)
        return if resource.attachments.blank?

        resource.attachments.map(&:url)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-forms-0.29.1 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.28.4 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.27.9 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.29.0 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.28.3 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.27.8 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.29.0.rc4 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.29.0.rc3 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.29.0.rc2 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.29.0.rc1 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.28.2 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.27.7 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.28.1 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.27.6 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.28.0 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.27.5 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.28.0.rc5 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.28.0.rc4 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.27.4 lib/decidim/forms/download_your_data_user_answers_serializer.rb
decidim-forms-0.27.3 lib/decidim/forms/download_your_data_user_answers_serializer.rb