Sha256: b1b733e3fbe7e714bd6639982d41a49e79d127689de6df2870233dc3fe040564

Contents?: true

Size: 1.27 KB

Versions: 46

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic to update an attachment from a
    # participatory process.
    class UpdateAttachment < Rectify::Command
      attr_reader :attachment

      # Public: Initializes the command.
      #
      # attachment - the Attachment to update
      # form - A form object with the params.
      def initialize(attachment, form)
        @attachment = attachment
        @form = form
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if form.invalid?

        update_attachment
        broadcast(:ok)
      end

      private

      attr_reader :form

      def update_attachment
        attachment.update!(attributes)
      end

      def attributes
        {
          title: form.title,
          file: form.file,
          description: form.description,
          weight: form.weight,
          attachment_collection: form.attachment_collection
        }.reject do |attribute, value|
          value.blank? && attribute != :attachment_collection
        end
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
decidim-admin-0.24.3 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.23.6 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.24.2 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.23.5 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.24.1 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.24.0 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.24.0.rc2 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.23.4 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.24.0.rc1 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.23.3 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.23.2 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.23.1 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.23.1.rc1 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.23.0 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.22.0 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.21.0 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.20.1 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.20.0 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.19.1 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.18.1 app/commands/decidim/admin/update_attachment.rb