Class: Decidim::Commands::DestroyResource

Inherits:
Decidim::Command show all
Defined in:
lib/decidim/commands/destroy_resource.rb

Instance Method Summary collapse

Methods inherited from Decidim::Command

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

Constructor Details

#initialize(resource, current_user) ⇒ DestroyResource

Initializes the command.

Parameters:

  • resource (ActiveRecord::Base)

    the resource to destroy.

  • current_user (Decidim::User)

    the current user.



10
11
12
13
# File 'lib/decidim/commands/destroy_resource.rb', line 10

def initialize(resource, current_user)
  @resource = resource
  @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

Destroys the result.

Broadcasts :ok if successful, :invalid otherwise.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/decidim/commands/destroy_resource.rb', line 18

def call
  return broadcast(:invalid) if invalid?

  run_before_hooks
  destroy_resource
  run_after_hooks

  broadcast(:ok, resource)
rescue Decidim::Commands::HookError, StandardError
  broadcast(:invalid)
end