Sha256: 291fd15e64fcc21f0a8ebc971d3646103f47eb31790d2d5be96ef9868838e6b3

Contents?: true

Size: 1.89 KB

Versions: 4

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Decidim
  module Amendable
    # A command with all the business logic to withdraw an amendment.
    class Withdraw < Rectify::Command
      # Public: Initializes the command.
      #
      # amendment     - The amendment to withdraw.
      # current_user  - The current user.
      def initialize(amendment, current_user)
        @amendment = amendment
        @amender = amendment.amender
        @current_user = current_user
        @emendation = amendment.emendation
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the resource.
      # - :invalid if the resource already has supports or does not belong to current user.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) unless emendation.votes.empty? && amender == current_user

        transaction do
          withdraw_amendment!
          withdraw_emendation!
        end

        broadcast(:ok, emendation)
      end

      private

      attr_reader :amendment, :amender, :current_user, :emendation

      def withdraw_amendment!
        @amendment = Decidim.traceability.update!(
          amendment,
          current_user,
          { state: "withdrawn" },
          visibility: "public-only"
        )
      end

      # Unlike other Amendable commands, we need to update the state of the
      # emendation for the scope Decidim::Proposals::Proposal::expect_withdrawn
      # to be able to retrieve rejected emendations.
      #
      # Because we are modifying the emendation itself, we need to prevent
      # PaperTrail from creating an additional version to ensure that this
      # change does not appear in the diff renderer of the emendation page.
      def withdraw_emendation!
        PaperTrail.request(enabled: false) do
          emendation.update!(state: "withdrawn")
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-core-0.20.1 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.20.0 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.19.1 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.19.0 app/commands/decidim/amendable/withdraw.rb