Sha256: e8c41996efb79bb8fb11707952f9fead44fb9b76eea033edea11b04cd89e58d4

Contents?: true

Size: 1003 Bytes

Versions: 6

Compression:

Stored size: 1003 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module EnhancedTextwork
    # A command with all the business logic when a user destroys a draft paragraph.
    class DestroyParagraph < Rectify::Command
      # Public: Initializes the command.
      #
      # paragraph     - The paragraph to destroy.
      # current_user - The current user.
      def initialize(paragraph, current_user)
        @paragraph = paragraph
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid and the paragraph is deleted.
      # - :invalid if the paragraph is not a draft.
      # - :invalid if the paragraph's author is not the current user.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) unless @paragraph.draft?
        return broadcast(:invalid) unless @paragraph.authored_by?(@current_user)

        @paragraph.destroy!

        broadcast(:ok, @paragraph)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-enhanced_textwork-1.0.5 app/commands/decidim/enhanced_textwork/destroy_paragraph.rb
decidim-enhanced_textwork-1.0.4 app/commands/decidim/enhanced_textwork/destroy_paragraph.rb
decidim-enhanced_textwork-1.0.3 app/commands/decidim/enhanced_textwork/destroy_paragraph.rb
decidim-enhanced_textwork-1.0.2 app/commands/decidim/enhanced_textwork/destroy_paragraph.rb
decidim-enhanced_textwork-1.0.1 app/commands/decidim/enhanced_textwork/destroy_paragraph.rb
decidim-enhanced_textwork-1.0.0 app/commands/decidim/enhanced_textwork/destroy_paragraph.rb