Class: Decidim::Admin::ContentBlocks::UpdateContentBlock

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

Overview

This command gets called when a content block is updated from the admin panel.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

#initialize(form, content_block, scope) ⇒ UpdateContentBlock

Public: Initializes the command.

form - The form from which the data in this component comes from. component - The component to update. scope - the scope where the content block belongs to.



16
17
18
19
20
# File 'decidim-admin/app/commands/decidim/admin/content_blocks/update_content_block.rb', line 16

def initialize(form, content_block, scope)
  @form = form
  @content_block = content_block
  @scope = scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Attribute Details

#content_blockObject (readonly)

Returns the value of attribute content_block.



9
10
11
# File 'decidim-admin/app/commands/decidim/admin/content_blocks/update_content_block.rb', line 9

def content_block
  @content_block
end

#formObject (readonly)

Returns the value of attribute form.



9
10
11
# File 'decidim-admin/app/commands/decidim/admin/content_blocks/update_content_block.rb', line 9

def form
  @form
end

#scopeObject (readonly)

Returns the value of attribute scope.



9
10
11
# File 'decidim-admin/app/commands/decidim/admin/content_blocks/update_content_block.rb', line 9

def scope
  @scope
end

Instance Method Details

#callObject

Public: Updates the content block settings and its attachments.

Broadcasts :ok if created, :invalid otherwise.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'decidim-admin/app/commands/decidim/admin/content_blocks/update_content_block.rb', line 25

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

  images_valid = true

  transaction do
    update_content_block_settings
    content_block.save!

    # Saving the images will cause the image file validations to run
    # according to their uploader settings and the organization settings.
    # The content block validation will fail in case there are processing
    # errors on the image files.
    #
    # NOTE:
    # The images can be only stored correctly if the content block is
    # already persisted. This is not the case e.g. when creating a new
    # newsletter which uses the content blocks through newsletter
    # templates. This is why this needs to happen after the initial
    # `content_block.save!` call.
    update_content_block_images
    unless content_block.valid?
      images_valid = false
      raise ActiveRecord::Rollback
    end

    # The save method needs to be called another time in order to store
    # the image information.
    content_block.save!
  end

  return broadcast(:invalid) unless images_valid

  broadcast(:ok, content_block)
end