Sha256: 50778e65817f631c33158fb18d85c2cd4dc64bd4b6a224da7605a557947ef56f

Contents?: true

Size: 1.41 KB

Versions: 44

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # A command with all the business logic when a user withdraws a new proposal.
    class WithdrawProposal < Rectify::Command
      # Public: Initializes the command.
      #
      # proposal     - The proposal to withdraw.
      # current_user - The current user.
      def initialize(proposal, current_user)
        @proposal = proposal
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the proposal.
      # - :has_supports if the proposal already has supports or does not belong to current user.
      #
      # Returns nothing.
      def call
        return broadcast(:has_supports) if @proposal.votes.any?

        transaction do
          change_proposal_state_to_withdrawn
          reject_emendations_if_any
        end

        broadcast(:ok, @proposal)
      end

      private

      def change_proposal_state_to_withdrawn
        @proposal.update state: "withdrawn"
      end

      def reject_emendations_if_any
        return if @proposal.emendations.empty?

        @proposal.emendations.each do |emendation|
          @form = form(Decidim::Amendable::RejectForm).from_params(id: emendation.amendment.id)
          result = Decidim::Amendable::Reject.call(@form)
          return result[:ok] if result[:ok]
        end
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

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