Sha256: d8c5428e0059654c2b6e8549ef4457ae6b5b63e9ddbff9e78ee4d3f46c579c49

Contents?: true

Size: 1.43 KB

Versions: 35

Compression:

Stored size: 1.43 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!
          notify_emendation_state_change!
        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

      def notify_emendation_state_change!
        emendation.process_amendment_state_change!
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
decidim-core-0.26.10 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.9 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.8 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.7 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.5 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.4 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.3 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.2 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.1 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.0 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.0.rc2 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.26.0.rc1 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.25.2 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.25.1 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.25.0 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.25.0.rc4 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.25.0.rc3 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.25.0.rc2 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.25.0.rc1 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.24.3 app/commands/decidim/amendable/withdraw.rb