Sha256: a43a1e94f5f1f49a5339b5e2cdf62ff17b6dcbb94beea8d1d6268455429a5edf

Contents?: true

Size: 1.53 KB

Versions: 24

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Decidim
  module Meetings
    # This class serializes the answers given by a User for questionnaire so can be
    # exported to CSV, JSON or other formats.
    class UserAnswersSerializer < Decidim::Exporters::Serializer
      include Decidim::TranslationsHelper

      # Public: Initializes the serializer with a collection of Answers.
      def initialize(answers)
        @answers = answers
      end

      # Public: Exports a hash with the serialized data for the user answers.
      def serialize
        @answers.each_with_index.inject({}) do |serialized, (answer, idx)|
          serialized.update(
            answer_translated_attribute_name(:id) => [answer.id, answer.user.id].join("_"),
            answer_translated_attribute_name(:created_at) => answer.created_at.to_s(:db),
            answer_translated_attribute_name(:user_status) => answer_translated_attribute_name(answer.decidim_user_id.present? ? "registered" : "unregistered"),
            "#{idx + 1}. #{translated_attribute(answer.question.body)}" => normalize_body(answer)
          )
        end
      end

      private

      attr_reader :answers
      alias resource answers

      def normalize_body(answer)
        normalize_choices(answer.choices)
      end

      def normalize_choices(choices)
        choices.map do |choice|
          choice.try(:body)
        end
      end

      def answer_translated_attribute_name(attribute)
        I18n.t(attribute.to_sym, scope: "decidim.forms.user_answers_serializer")
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
decidim-meetings-0.26.8 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.27.4 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.27.3 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.26.7 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.26.5 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.27.2 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.27.1 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.26.4 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.27.0 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.26.3 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.27.0.rc2 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.27.0.rc1 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.26.2 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.26.1 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.26.0 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.26.0.rc2 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.26.0.rc1 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.25.2 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.25.1 lib/decidim/meetings/user_answers_serializer.rb
decidim-meetings-0.25.0 lib/decidim/meetings/user_answers_serializer.rb