Sha256: f93c8726eb44d987718eca4d8bb8efc16852933c4218bbfbec38a35e42c245eb

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # This command gets called when a content block is updated from the admin
    # panel.
    class UpdateContentBlock < Rectify::Command
      attr_reader :form, :content_block, :scope

      # 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.
      def initialize(form, content_block, scope)
        @form = form
        @content_block = content_block
        @scope = scope
      end

      # Public: Updates the content block settings and its attachments.
      #
      # Broadcasts :ok if created, :invalid otherwise.
      def call
        return broadcast(:invalid) if form.invalid?

        transaction do
          update_content_block_settings
          update_content_block_images
          content_block.save!
        end

        broadcast(:ok)
      end

      private

      def update_content_block_settings
        content_block.settings = form.settings
      end

      def update_content_block_images
        content_block.manifest.images.each do |image_config|
          image_name = image_config[:name]

          if form.images["remove_#{image_name}".to_sym]
            content_block.images_container.send("remove_#{image_name}=", true)
          elsif form.images[image_name]
            content_block.images_container.send("#{image_name}=", form.images[image_name])
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-admin-0.15.0 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.14.4 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.14.3 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.14.2 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.14.1 app/commands/decidim/admin/update_content_block.rb