Class: Decidim::Admin::CreateAttachment

Inherits:
Rectify::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



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

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 wasn’t valid and we couldn’t proceed.

Returns nothing.



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

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

  build_attachment

  if @attachment.valid?
    @attachment.save!
    notify_followers
    broadcast(:ok)
  else
    @form.errors.add :file, @attachment.errors[:file] if @attachment.errors.has_key? :file
    broadcast(:invalid)
  end
end