Sha256: d2203486e791e2dff10595bd20afdeb125999e636802f933ef31725ff6769ac9

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # This class serializes a Proposal so can be exported to CSV, JSON or other
    # formats.
    class ProposalSerializer < Decidim::Exporters::Serializer
      include Decidim::ResourceHelper

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

      # Public: Exports a hash with the serialized data for this proposal.
      def serialize
        {
          id: @proposal.id,
          category: {
            id: @proposal.category.try(:id),
            name: @proposal.category.try(:name)
          },
          scope: {
            id: @proposal.scope.try(:id),
            name: @proposal.scope.try(:name)
          },
          title: @proposal.title,
          body: @proposal.body,
          votes: @proposal.proposal_votes_count,
          comments: @proposal.comments.count,
          published_at: @proposal.published_at,
          url: url,
          component: { id: component.id },
          meeting_urls: meetings
        }
      end

      private

      attr_reader :proposal

      def component
        proposal.component
      end

      def meetings
        @proposal.linked_resources(:meetings, "proposals_from_meeting").map do |meeting|
          Decidim::ResourceLocatorPresenter.new(meeting).url
        end
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-proposals-0.13.1 lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.13.0 lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.13.0.pre1 lib/decidim/proposals/proposal_serializer.rb