Sha256: c695853b6ecffc123ef509c5ed361ef6ad709d4509781c74ea1e701e45d228e6

Contents?: true

Size: 1.46 KB

Versions: 7

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,
          created_at: @proposal.created_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

7 entries across 7 versions & 1 rubygems

Version Path
decidim-proposals-0.12.2 lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.12.1 lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.12.0 lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.11.2 lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.12.0.pre lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.11.1 lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.11.0.pre1 lib/decidim/proposals/proposal_serializer.rb