Sha256: 9072396298a6261b787a0fbf8dc319041fcf4925bbc53cc2a7c81d8da731aa0e

Contents?: true

Size: 1.8 KB

Versions: 13

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module Decidim
  module Accountability
    # This class serializes a Result so can be exported to CSV, JSON or other
    # formats.
    class ResultSerializer < Decidim::Exporters::Serializer
      include Decidim::ResourceHelper
      include Decidim::TranslationsHelper

      # Public: Initializes the serializer with a result.
      def initialize(result)
        @result = result
      end

      # Public: Exports a hash with the serialized data for this result.
      def serialize
        {
          id: result.id,
          category: {
            id: result.category.try(:id),
            name: result.category.try(:name) || empty_translatable
          },
          scope: {
            id: result.scope.try(:id),
            name: result.scope.try(:name) || empty_translatable
          },
          parent: {
            id: result.parent.try(:id)
          },
          title: result.title,
          description: result.description,
          start_date: result.start_date,
          end_date: result.end_date,
          status: {
            id: result.status.try(:id),
            key: result.status.try(:key),
            name: result.status.try(:name) || empty_translatable
          },
          progress: result.progress,
          created_at: result.created_at,
          url:,
          component: { id: component.id },
          proposal_urls: proposals
        }
      end

      private

      attr_reader :result
      alias resource result

      def component
        result.component
      end

      def proposals
        result.linked_resources(:proposals, "included_proposals").map do |proposal|
          Decidim::ResourceLocatorPresenter.new(proposal).url
        end
      end

      def url
        Decidim::ResourceLocatorPresenter.new(result).url
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-accountability-0.29.1 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.28.4 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.29.0 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.28.3 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.29.0.rc4 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.29.0.rc3 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.29.0.rc2 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.29.0.rc1 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.28.2 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.28.1 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.28.0 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.28.0.rc5 lib/decidim/accountability/result_serializer.rb
decidim-accountability-0.28.0.rc4 lib/decidim/accountability/result_serializer.rb