Class: Decidim::Admin::UpdateContentBlock

Inherits:
Command
  • Object
show all
Defined in:
decidim-admin/app/commands/decidim/admin/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

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.


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

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

Instance Attribute Details

#content_blockObject (readonly)

Returns the value of attribute content_block.


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

def content_block
  @content_block
end

#formObject (readonly)

Returns the value of attribute form.


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

def form
  @form
end

#scopeObject (readonly)

Returns the value of attribute scope.


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

def scope
  @scope
end

Instance Method Details

#callObject

Public: Updates the content block settings and its attachments.

Broadcasts :ok if created, :invalid otherwise.


24
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
# File 'decidim-admin/app/commands/decidim/admin/update_content_block.rb', line 24

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