Class: Decidim::Proposals::WithdrawCollaborativeDraft

Inherits:
Command
  • Object
show all
Defined in:
app/commands/decidim/proposals/withdraw_collaborative_draft.rb

Overview

A command with all the business logic when a user withdraws a collaborative_draft.

Instance Method Summary collapse

Constructor Details

#initialize(collaborative_draft, current_user) ⇒ WithdrawCollaborativeDraft

Public: Initializes the command.

collaborative_draft - The collaborative_draft to withdraw. current_user - The current user.



11
12
13
14
# File 'app/commands/decidim/proposals/withdraw_collaborative_draft.rb', line 11

def initialize(collaborative_draft, current_user)
  @collaborative_draft = collaborative_draft
  @current_user = current_user
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid and the collaborative_draft is published.

  • :invalid if the collaborative_draft’s author is not the current user.

Returns nothing.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/commands/decidim/proposals/withdraw_collaborative_draft.rb', line 22

def call
  return broadcast(:invalid) if @collaborative_draft.withdrawn?
  return broadcast(:invalid) if @collaborative_draft.published?
  return broadcast(:invalid) unless @collaborative_draft.authored_by? @current_user

  transaction do
    @collaborative_draft.requesters.each do |requester_user|
      RejectAccessToCollaborativeDraft.call(@collaborative_draft, current_user, requester_user)
    end

    withdraw_collaborative_draft
    send_notification_to_authors
  end

  broadcast(:ok, @collaborative_draft)
end