Sha256: d8c3a206d35daf013d9db0b4f1f098c90e88198bec43a3848ec1464fe12e8029

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic when a user updates a proposal.
      class UpdateProposal < Rectify::Command
        include AttachmentMethods

        # Public: Initializes the command.
        #
        # form         - A form object with the params.
        # proposal - the proposal to update.
        def initialize(form, proposal)
          @form = form
          @proposal = proposal
          @attached_to = proposal
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid, together with the proposal.
        # - :invalid if the form wasn't valid and we couldn't proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if form.invalid?

          if process_attachments?
            @proposal.attachments.destroy_all

            build_attachment
            return broadcast(:invalid) if attachment_invalid?
          end

          transaction do
            update_proposal
            create_attachment if process_attachments?
          end

          broadcast(:ok, proposal)
        end

        private

        attr_reader :form, :proposal, :attachment

        def update_proposal
          parsed_title = Decidim::ContentProcessor.parse_with_processor(:hashtag, form.title, current_organization: form.current_organization).rewrite
          parsed_body = Decidim::ContentProcessor.parse_with_processor(:hashtag, form.body, current_organization: form.current_organization).rewrite

          Decidim.traceability.update!(
            proposal,
            form.current_user,
            title: parsed_title,
            body: parsed_body,
            category: form.category,
            scope: form.scope,
            address: form.address,
            latitude: form.latitude,
            longitude: form.longitude
          )
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-proposals-0.15.2 app/commands/decidim/proposals/admin/update_proposal.rb
decidim-proposals-0.15.1 app/commands/decidim/proposals/admin/update_proposal.rb
decidim-proposals-0.15.0 app/commands/decidim/proposals/admin/update_proposal.rb