Sha256: c7028ff641ba4bd9f2aed6d86ed43002dda05e45f4f17a73645f6c6b67614d5c

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Decidim
  module Votings
    # This class serializes a Voting so it can be exported to CSV, JSON or other
    # formats.
    class VotingSerializer < Decidim::Exporters::Serializer
      include Decidim::ApplicationHelper
      include Decidim::ResourceHelper
      include Decidim::TranslationsHelper

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

      # Public: Exports a hash with the serialized data for this voting.
      def serialize
        {
          participatory_space_id: voting.id,
          url:,
          title: voting.title,
          description: voting.description,
          start_time: voting.start_time,
          end_time: voting.end_time,
          voting_type: translated_voting_type,
          scope: {
            id: voting.scope.try(:id),
            name: voting.scope.try(:name)
          },
          banner_image_url: Decidim::Votings::VotingPresenter.new(voting).banner_image_url,
          introductory_image_url: Decidim::Votings::VotingPresenter.new(voting).introductory_image_url
        }
      end

      private

      attr_reader :voting
      alias resource voting

      def translated_voting_type
        translation_hash = {}
        voting.organization.available_locales.each do |locale|
          translation_hash[locale] = I18n.t(voting.voting_type, scope: "decidim.votings.admin.votings.form.voting_type")
        end

        translation_hash
      end

      def url
        Decidim::Votings::Engine.routes.url_helpers.voting_url(host: voting.organization.host, slug: voting.slug)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-elections-0.28.4 lib/decidim/votings/voting_serializer.rb
decidim-elections-0.28.3 lib/decidim/votings/voting_serializer.rb
decidim-elections-0.28.2 lib/decidim/votings/voting_serializer.rb
decidim-elections-0.28.1 lib/decidim/votings/voting_serializer.rb
decidim-elections-0.28.0 lib/decidim/votings/voting_serializer.rb
decidim-elections-0.28.0.rc5 lib/decidim/votings/voting_serializer.rb
decidim-elections-0.28.0.rc4 lib/decidim/votings/voting_serializer.rb