Sha256: 3b68235d770a71fd4f75afcfd6bd61ad27ac60c6f7ad52f9f63ba0e236a1f8ef

Contents?: true

Size: 1.55 KB

Versions: 14

Compression:

Stored size: 1.55 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[image_name]
            content_block.images_container.send("#{image_name}=", form.images[image_name])
          elsif form.images["remove_#{image_name}".to_sym] == "1"
            content_block.images_container.send("remove_#{image_name}=", true)
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
decidim-admin-0.21.0 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.20.1 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.20.0 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.19.1 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.18.1 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.19.0 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.17.2 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.18.0 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.17.1 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.16.1 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.17.0 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.16.0 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.15.2 app/commands/decidim/admin/update_content_block.rb
decidim-admin-0.15.1 app/commands/decidim/admin/update_content_block.rb