Sha256: 800e1fd5aa9902f9de531b04c036cbd0eeead60c5c5a9b58c07f402fe68a34c3

Contents?: true

Size: 1.64 KB

Versions: 9

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Decidim
  module Commands
    class CreateResource < ::Decidim::Command
      include Decidim::Commands::ResourceHandler

      # Initializes the command.
      # @param form [Decidim::Form] the form object to create the resource.
      def initialize(form)
        @form = form
      end

      # Creates the result if valid.
      #
      # Broadcasts :ok if successful, :invalid otherwise.
      def call
        return broadcast(:invalid) if invalid?

        perform!
        broadcast(:ok, resource)
      rescue ActiveRecord::RecordInvalid
        add_file_attribute_errors!
        broadcast(:invalid)
      rescue Decidim::Commands::HookError
        broadcast(:invalid)
      end

      protected

      # @usage
      #  create_resource - Will create the resource, raising any validation errors.
      def create_resource
        @resource = Decidim.traceability.send(create_method, resource_class, current_user, attributes, **extra_params)
        @resource.persisted? ? resource : raise(ActiveRecord::RecordInvalid, resource)
      end

      attr_reader :form, :resource

      delegate :current_user, to: :form

      def create_method
        has_file_attributes? ? :create : :create!
      end

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

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

      private

      def perform!
        transaction do
          run_before_hooks
          create_resource
          run_after_hooks
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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