Sha256: fa1e5bce5cc157dd1ca398a95a17686b281841ab30a5c4bf2838f92df157cfef

Contents?: true

Size: 969 Bytes

Versions: 8

Compression:

Stored size: 969 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic to destroy a category in the
    # system.
    class DestroyCategory < Decidim::Command
      # Public: Initializes the command.
      #
      # category - A Category that will be destroyed
      def initialize(category, user)
        @category = category
        @user = user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid.
      # - :invalid if the data wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if category.nil? || category.subcategories.any?

        destroy_category
        broadcast(:ok)
      end

      private

      attr_reader :category

      def destroy_category
        Decidim.traceability.perform_action!(:delete, category, @user) do
          category.destroy!
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-admin-0.27.5 app/commands/decidim/admin/destroy_category.rb
decidim-admin-0.27.4 app/commands/decidim/admin/destroy_category.rb
decidim-admin-0.27.3 app/commands/decidim/admin/destroy_category.rb
decidim-admin-0.27.2 app/commands/decidim/admin/destroy_category.rb
decidim-admin-0.27.1 app/commands/decidim/admin/destroy_category.rb
decidim-admin-0.27.0 app/commands/decidim/admin/destroy_category.rb
decidim-admin-0.27.0.rc2 app/commands/decidim/admin/destroy_category.rb
decidim-admin-0.27.0.rc1 app/commands/decidim/admin/destroy_category.rb