Sha256: 3dca7dfa19653789734747d4f983db300c95cb1b63630be2cee1e6ca06a7ea01

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 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
      include Rails.application.routes.url_helpers
      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)
          },
          title: @proposal.title,
          body: @proposal.body,
          votes: @proposal.proposal_votes_count,
          comments: @proposal.comments.count,
          created_at: @proposal.created_at,
          url: url,
          feature: { id: feature.id },
          meeting_urls: meetings
        }
      end

      private

      attr_reader :proposal

      def feature
        proposal.feature
      end

      def organization
        feature.organization
      end

      def meetings
        @proposal.linked_resources(:meetings, "proposals_from_meeting").map do |meeting|
          decidim_resource_url(
            meeting,
            feature_id: feature,
            participatory_process_id: participatory_process,
            host: organization.host
          )
        end
      end

      def participatory_process
        feature.participatory_process
      end

      def url
        Decidim::Proposals::Engine.routes.url_helpers.proposal_url(
          proposal,
          feature_id: feature,
          participatory_process_id: participatory_process,
          host: organization.host
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
decidim-proposals-0.1.0 app/services/decidim/proposals/proposal_serializer.rb
decidim-0.1.0 decidim-proposals/app/services/decidim/proposals/proposal_serializer.rb