Sha256: 918b9ad75aef7e2a66d407e8f048d36f31ccbe6e97c43ada58f8ef01c1867fa9

Contents?: true

Size: 1.15 KB

Versions: 54

Compression:

Stored size: 1.15 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!(attributes)
      end

      def attributes
        {
          title: form.title,
          file: form.file,
          description: form.description
        }.reject do |_attribute, value|
          value.blank?
        end
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 2 rubygems

Version Path
decidim-admin-0.6.5 app/commands/decidim/admin/update_attachment.rb
decidim-0.6.5 decidim-admin/app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.6.4 app/commands/decidim/admin/update_attachment.rb
decidim-0.6.4 decidim-admin/app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.6.3 app/commands/decidim/admin/update_attachment.rb
decidim-0.6.3 decidim-admin/app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.6.2 app/commands/decidim/admin/update_attachment.rb
decidim-0.6.2 decidim-admin/app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.6.1 app/commands/decidim/admin/update_attachment.rb
decidim-0.6.1 decidim-admin/app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.6.0 app/commands/decidim/admin/update_attachment.rb
decidim-0.6.0 decidim-admin/app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.5.1 app/commands/decidim/admin/update_attachment.rb
decidim-0.5.1 decidim-admin/app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.5.0 app/commands/decidim/admin/update_attachment.rb
decidim-0.5.0 decidim-admin/app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.4.4 app/commands/decidim/admin/update_attachment.rb
decidim-0.4.4 decidim-admin/app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.4.3 app/commands/decidim/admin/update_attachment.rb
decidim-admin-0.4.2 app/commands/decidim/admin/update_attachment.rb