Sha256: 191913c4084c4217a10105016238c247c758341d4f124f7e7afae6bb345181b7

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Decidim
  module Commands
    class DestroyResource < ::Decidim::Command
      # Initializes the command.
      #
      # @param resource [ActiveRecord::Base] the resource to destroy.
      # @param current_user [Decidim::User] the current user.
      def initialize(resource, current_user)
        @resource = resource
        @current_user = current_user
      end

      # Destroys the result.
      #
      # Broadcasts :ok if successful, :invalid otherwise.
      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

      protected

      attr_reader :resource, :current_user

      def invalid? = false

      def destroy_resource
        Decidim.traceability.perform_action!(
          :delete,
          resource,
          current_user,
          **extra_params
        ) do
          resource.destroy!
        end
      end

      # Any extra params that you want to pass to the traceability service.
      #
      # @usage
      #  def extra_params = { "visibility" => "all"}
      #  def extra_params = { "visibility" => "public-only" }
      #  def extra_params = { "visibility" => "admin-only" }
      #  def extra_params
      #    {
      #      resource: {
      #        title: resource.title
      #      },
      #      participatory_space: {
      #        title: resource.participatory_space.title
      #      }
      #    }
      #  end
      #
      # @return [Hash] a hash with the extra params.
      def extra_params = {}

      # Useful for running any code that you may want to execute before deleting the resource.
      def run_before_hooks; end

      # Useful for running any code that you may want to execute after deleting the resource.
      def run_after_hooks; end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.29.1 lib/decidim/commands/destroy_resource.rb
decidim-core-0.29.0 lib/decidim/commands/destroy_resource.rb
decidim-core-0.29.0.rc4 lib/decidim/commands/destroy_resource.rb
decidim-core-0.29.0.rc3 lib/decidim/commands/destroy_resource.rb
decidim-core-0.29.0.rc2 lib/decidim/commands/destroy_resource.rb
decidim-core-0.29.0.rc1 lib/decidim/commands/destroy_resource.rb