Class: Decidim::Amendable::DestroyDraft

Inherits:
Command
  • Object
show all
Defined in:
decidim-core/app/commands/decidim/amendable/destroy_draft.rb

Overview

A command with all the business logic when a user starts amending a resource.

Instance Method Summary collapse

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events

Constructor Details

#initialize(amendment, current_user) ⇒ DestroyDraft

Public: Initializes the command.

amendment - The amendment to destroy. current_user - The current user.



11
12
13
14
15
16
17
# File 'decidim-core/app/commands/decidim/amendable/destroy_draft.rb', line 11

def initialize(amendment, current_user)
  @amendment = amendment
  @amendable = amendment.amendable
  @emendation = amendment.emendation
  @amender = amendment.amender
  @current_user = current_user
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Method Details

#callObject

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.



26
27
28
29
30
31
32
33
# File 'decidim-core/app/commands/decidim/amendable/destroy_draft.rb', line 26

def call
  return broadcast(:invalid) unless amendment.draft? && amender == current_user

  emendation.destroy!
  amendment.delete

  broadcast(:ok, amendable)
end