Sha256: f45e61176000add798442612943060fe60368fddaab062d55286b88356ea937f

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    # This class serializes an Answer so it can be exported to CSV, JSON or other
    # formats.
    class AnswerSerializer < Decidim::Exporters::Serializer
      include Decidim::ApplicationHelper
      include Decidim::ResourceHelper
      include Decidim::TranslationsHelper

      # Public: Initializes the serializer with an answer.
      def initialize(answer)
        @answer = answer
      end

      # Public: Exports a hash with the serialized data for this answer.
      def serialize
        {
          participatory_space: {
            id: election.participatory_space.id,
            title: election.participatory_space.title
          },
          id: answer.id,
          election_id: election.id,
          election_title: election.title,
          question_id: question.id,
          question_title: question.title,
          answer_id: answer.id,
          answer_title: answer.title,
          answer_votes: answer.votes_count
        }
      end

      private

      attr_reader :answer

      def election
        answer.question.election
      end

      def question
        answer.question
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-elections-0.24.3 lib/decidim/elections/answer_serializer.rb
decidim-elections-0.24.2 lib/decidim/elections/answer_serializer.rb
decidim-elections-0.24.1 lib/decidim/elections/answer_serializer.rb
decidim-elections-0.24.0 lib/decidim/elections/answer_serializer.rb
decidim-elections-0.24.0.rc2 lib/decidim/elections/answer_serializer.rb
decidim-elections-0.24.0.rc1 lib/decidim/elections/answer_serializer.rb