Sha256: 58ce54dbd6cee2f2bc990dd0b1af7a8757d914ec7ae2b11e56f15221e474113d

Contents?: true

Size: 1.47 KB

Versions: 49

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Decidim
  module Surveys
    # This class serializes the specific data in each Survey.
    # This is `Questionnaire->questions->answer_options` but not `answers`
    # and `answer_choices`.
    class DataSerializer < Decidim::Exporters::Serializer
      # Returns: Array of Decidim::Forms::Questionnaire as a json hash,
      #     or nil if none exists.
      def serialize
        component = resource
        surveys = Decidim::Surveys::Survey.where(component: component)
        surveys.collect do |survey|
          next if survey.questionnaire.nil?

          json = serialize_survey(survey)
          json.with_indifferent_access.merge(survey_id: survey.id)
        end
      end

      def serialize_survey(survey)
        questionnaire = survey.questionnaire
        questionnaire_json = questionnaire.attributes.as_json
        questionnaire_json[:questions] = serialize_questions(questionnaire.questions.order(:position))
        json = survey.attributes.as_json
        json[:questionnaire] = questionnaire_json
        json
      end

      def serialize_questions(questions)
        questions.collect do |question|
          json = question.attributes.as_json
          json[:answer_options] = serialize_answer_options(question.answer_options)
          json
        end
      end

      def serialize_answer_options(answer_options)
        answer_options.collect do |option|
          option.attributes.as_json
        end
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
decidim-surveys-0.27.9 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.8 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.7 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.6 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.26.10 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.26.9 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.5 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.26.8 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.4 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.3 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.26.7 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.26.5 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.2 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.1 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.26.4 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.0 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.26.3 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.0.rc2 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.27.0.rc1 app/serializers/decidim/surveys/data_serializer.rb
decidim-surveys-0.26.2 app/serializers/decidim/surveys/data_serializer.rb