Class: Decidim::Notify::CreateNote

Inherits:
Rectify::Command
  • Object
show all
Defined in:
app/commands/decidim/notify/create_note.rb

Instance Method Summary collapse

Constructor Details

#initialize(form) ⇒ CreateNote

Public: Initializes the command.

form - A config form



9
10
11
# File 'app/commands/decidim/notify/create_note.rb', line 9

def initialize(form)
  @form = form
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if we couldn’t proceed.

Returns nothing.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/commands/decidim/notify/create_note.rb', line 19

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

  begin
    note = Note.create!(
      author: Author.find_by(code: form.code, component: current_component)&.user,
      body: form.body,
      creator: current_user,
      chapter: create_chapter,
      component: current_component
    )

    broadcast(:ok, note, @new_chapter)
  rescue ActiveRecord::ActiveRecordError => e
    broadcast(:invalid, e.message)
  end
end