Class: Decidim::Admin::CreateAttachment

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

Overview

A command with all the business logic to add an attachment to a participatory process.

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

#initialize(form, attached_to, user) ⇒ CreateAttachment

Public: Initializes the command.

form - A form object with the params. attached_to - The ActiveRecord::Base that will hold the attachment



12
13
14
15
16
# File 'decidim-admin/app/commands/decidim/admin/create_attachment.rb', line 12

def initialize(form, attached_to, user)
  @form = form
  @attached_to = attached_to
  @user = user
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 was not valid and we could not proceed.

Returns nothing.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'decidim-admin/app/commands/decidim/admin/create_attachment.rb', line 24

def call
  return broadcast(:invalid) if form.invalid?

  build_attachment

  if @attachment.valid?
    Decidim.traceability.perform_action!(:create, Decidim::Attachment, @user) do
      @attachment.save!
      notify_followers
      broadcast(:ok)
      @attachment
    end
  else
    @form.errors.add :file, @attachment.errors[:file] if @attachment.errors.has_key? :file
    broadcast(:invalid)
  end
end