Sha256: 19112669331293e32acbc4a41dae774ff1fe7b3f780eab84ce24effc2a8b7b6a

Contents?: true

Size: 1.68 KB

Versions: 8

Compression:

Stored size: 1.68 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 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::ResourceLocatorPresenter.new(meeting).url
        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

8 entries across 8 versions & 2 rubygems

Version Path
decidim-proposals-0.4.0 lib/decidim/proposals/proposal_serializer.rb
decidim-0.4.0 decidim-proposals/lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.3.2 lib/decidim/proposals/proposal_serializer.rb
decidim-0.3.2 decidim-proposals/lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.3.1 lib/decidim/proposals/proposal_serializer.rb
decidim-0.3.1 decidim-proposals/lib/decidim/proposals/proposal_serializer.rb
decidim-proposals-0.3.0 lib/decidim/proposals/proposal_serializer.rb
decidim-0.3.0 decidim-proposals/lib/decidim/proposals/proposal_serializer.rb