Sha256: 54e627becfefbec46f0e3b74db98b2b57b8cff14031a4188ead33ebaf55d6619

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Decidim
  module EnhancedTextwork
    # A command with all the business logic when a user withdraws a new paragraph.
    class WithdrawParagraph < Rectify::Command
      # Public: Initializes the command.
      #
      # paragraph     - The paragraph to withdraw.
      # 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, together with the paragraph.
      # - :has_supports if the paragraph already has supports or does not belong to current user.
      #
      # Returns nothing.
      def call
        return broadcast(:has_supports) if @paragraph.votes.any?

        transaction do
          change_paragraph_state_to_withdrawn
          reject_emendations_if_any
        end

        broadcast(:ok, @paragraph)
      end

      private

      def change_paragraph_state_to_withdrawn
        @paragraph.update state: "withdrawn"
      end

      def reject_emendations_if_any
        return if @paragraph.emendations.empty?

        @paragraph.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

6 entries across 6 versions & 1 rubygems

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