Sha256: 4c9f8204b5d6aa5378b0e986cbb92dff406f289490ea968108cf6ee4599d7447

Contents?: true

Size: 1.14 KB

Versions: 25

Compression:

Stored size: 1.14 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 DestroyDraft < Decidim::Command
      # Public: Initializes the command.
      #
      # amendment     - The amendment to destroy.
      # current_user  - The current user.
      def initialize(amendment, current_user)
        @amendment = amendment
        @amendable = amendment.amendable
        @emendation = amendment.emendation
        @amender = amendment.amender
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the amendable.
      # - :invalid if the amendment is not a draft.
      # - :invalid if the amender is not the current user.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) unless amendment.draft? && amender == current_user

        emendation.destroy!
        amendment.delete

        broadcast(:ok, amendable)
      end

      private

      attr_reader :amendment, :amendable, :emendation, :amender, :current_user
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.28.4 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.27.9 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.29.0 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.28.3 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.27.8 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.29.0.rc4 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.29.0.rc3 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.29.0.rc2 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.29.0.rc1 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.28.2 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.27.7 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.28.1 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.27.6 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.28.0 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.27.5 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.28.0.rc5 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.28.0.rc4 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.27.4 app/commands/decidim/amendable/destroy_draft.rb
decidim-core-0.27.3 app/commands/decidim/amendable/destroy_draft.rb