Sha256: 0d1ce208ef0594ad8f062c5160506ad7036611c536c9dad6dcae9cfffb6129b4

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    module Admin
      # A command with all the business logic that creates a new initiative type scope
      class CreateInitiativeTypeScope < Decidim::Command
        # Public: Initializes the command.
        #
        # form - A form object with the params.
        def initialize(form)
          @form = form
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if the form was not valid and we could not proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if form.invalid?

          initiative_type_scope = create_initiative_type_scope

          if initiative_type_scope.persisted?
            broadcast(:ok, initiative_type_scope)
          else
            initiative_type_scope.errors.each do |error|
              form.errors.add(error.attribute, error.message)
            end

            broadcast(:invalid)
          end
        end

        private

        attr_reader :form

        def create_initiative_type_scope
          initiative_type = InitiativesTypeScope.new(
            supports_required: form.supports_required,
            decidim_scopes_id: form.decidim_scopes_id,
            decidim_initiatives_types_id: form.context.type_id
          )

          return initiative_type unless initiative_type.valid?

          initiative_type.save
          initiative_type
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-initiatives-0.28.4 app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb
decidim-initiatives-0.28.3 app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb
decidim-initiatives-0.28.2 app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb
decidim-initiatives-0.28.1 app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb
decidim-initiatives-0.28.0 app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb
decidim-initiatives-0.28.0.rc5 app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb
decidim-initiatives-0.28.0.rc4 app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb