Class: Decidim::Admin::CreateAttachment

Inherits:
Command
  • Object
show all
Defined in:
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

Constructor Details

#initialize(form, attached_to) ⇒ CreateAttachment

Public: Initializes the command.

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



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

def initialize(form, attached_to)
  @form = form
  @attached_to = attached_to
end

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 '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, current_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