Sha256: 84882f87826ccb0a323e84e4ff95fc7e1e2fe9856b56634abd0c2b6cc889d2ab

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Decidim
  module Amendable
    # A command with all the business logic when a user starts amending a resource.
    class Withdraw < Rectify::Command
      # Public: Initializes the command.
      #
      # emendation     - The resource to withdraw.
      # current_user - The current user.
      def initialize(emendation, current_user)
        @emendation = emendation
        @current_user = current_user
      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) if @emendation.votes.any?

        transaction do
          change_amendment_state_to_withdrawn
          change_emendation_state_to_withdrawn
        end

        broadcast(:ok, @emendation)
      end

      private

      def change_amendment_state_to_withdrawn
        @emendation.amendment.update state: "withdrawn"
      end

      def change_emendation_state_to_withdrawn
        # rubocop:disable Rails/SkipsModelValidations
        @emendation.update_attribute :state, "withdrawn"
        # rubocop:enable Rails/SkipsModelValidations
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-core-0.18.1 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.17.2 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.18.0 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.17.1 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.16.1 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.17.0 app/commands/decidim/amendable/withdraw.rb
decidim-core-0.16.0 app/commands/decidim/amendable/withdraw.rb