Class: Decidim::ParticipatoryDocuments::Admin::UpdateOrCreateAnnotation

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

Instance Method Summary collapse

Constructor Details

#initialize(form, document) ⇒ UpdateOrCreateAnnotation

Public: Initializes the command.

form - A form object with the params.



10
11
12
13
# File 'app/commands/decidim/participatory_documents/admin/update_or_create_annotation.rb', line 10

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

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/commands/decidim/participatory_documents/admin/update_or_create_annotation.rb', line 15

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

  begin
    @old_section = annotation.present? ? annotation.section : nil

    transaction do
      create_section! if section.blank?
      create_annotation! if annotation.blank?
      annotation.assign_attributes(**attributes)
      update_annotation! if annotation.changed?
    end

    destroy_old_section!
    document.update_positions!

    broadcast(:ok, annotation)
  rescue ActiveRecord::RecordInvalid
    broadcast(:invalid)
  end
end