Sha256: 010cd7a50e59ac1ce4128f7fb78c3b3104c1f02353e4cc12feb903764011064a

Contents?: true

Size: 1.3 KB

Versions: 50

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic to add an attachment to a
    # participatory process.
    class CreateAttachment < Rectify::Command
      # Public: Initializes the command.
      #
      # form - A form object with the params.
      # attached_to - The ActiveRecord::Base that will hold the attachment
      def initialize(form, attached_to)
        @form = form
        @attached_to = attached_to
      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?

        build_attachment

        if @attachment.valid?
          @attachment.save!
          broadcast(:ok)
        else
          if @attachment.errors.has_key? :file
            @form.errors.add :file, @attachment.errors[:file]
          end
          broadcast(:invalid)
        end
      end

      private

      attr_reader :form

      def build_attachment
        @attachment = Attachment.new(
          title: form.title,
          description: form.description,
          file: form.file,
          attached_to: @attached_to
        )
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 2 rubygems

Version Path
decidim-admin-0.4.0 app/commands/decidim/admin/create_attachment.rb
decidim-0.4.0 decidim-admin/app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.3.2 app/commands/decidim/admin/create_attachment.rb
decidim-0.3.2 decidim-admin/app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.3.1 app/commands/decidim/admin/create_attachment.rb
decidim-0.3.1 decidim-admin/app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.3.0 app/commands/decidim/admin/create_attachment.rb
decidim-0.3.0 decidim-admin/app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.2.0 app/commands/decidim/admin/create_attachment.rb
decidim-0.2.0 decidim-admin/app/commands/decidim/admin/create_attachment.rb