Sha256: 55ceef646f06b5e9c8fa934252158ebe5e301bce405ff56dfe330a8c31039f74

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 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 < Decidim::Command
      include ::Decidim::AttachmentAttributesMethods

      attr_reader :attachment

      delegate :current_user, to: :form
      # 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 was not valid and we could not proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if form.invalid?

        update_attachment
        broadcast(:ok)
      end

      private

      attr_reader :form

      def update_attachment
        Decidim.traceability.update!(@attachment, current_user, attributes)
      end

      def attributes
        {
          title: form.title,
          file: form.file,
          link: form.link,
          description: form.description,
          weight: form.weight
        }.merge(
          attachment_attributes(:file)
        ).compact_blank.merge(
          attachment_collection: form.attachment_collection
        ).tap do |attrs|
          attrs[:file] = nil if form.link.present? && form.file.blank?
          attrs[:link] = nil if form.file.present? && form.link.blank?
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-admin-0.29.1 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.29.0 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.29.0.rc4 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.29.0.rc3 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.29.0.rc2 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.29.0.rc1 app/commands/decidim/admin/update_attachment.rb