Class: Decidim::Initiatives::Admin::CreateInitiativeTypeScope

Inherits:
Command
  • Object
show all
Defined in:
decidim-initiatives/app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb

Overview

A command with all the business logic that creates a new initiative type scope

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

#initialize(form) ⇒ CreateInitiativeTypeScope

Public: Initializes the command.

form - A form object with the params.


11
12
13
# File 'decidim-initiatives/app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb', line 11

def initialize(form)
  @form = form
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the form wasn't valid and we couldn't proceed.

Returns nothing.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'decidim-initiatives/app/commands/decidim/initiatives/admin/create_initiative_type_scope.rb', line 21

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