Sha256: d3054a9a99d5db82aada64d6aafcbddd9ddc958fadfa846bd3ff2de072ccfa4e

Contents?: true

Size: 1.83 KB

Versions: 19

Compression:

Stored size: 1.83 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!
          notify_followers
          broadcast(:ok)
        else
          @form.errors.add :file, @attachment.errors[:file] if @attachment.errors.has_key? :file
          broadcast(:invalid)
        end
      end

      private

      attr_reader :form

      def build_attachment
        @attachment = Attachment.new(
          title: form.title,
          description: form.description,
          attached_to: @attached_to,
          weight: form.weight,
          attachment_collection: form.attachment_collection,
          file: form.file, # Define attached_to before this
          content_type: form.file.content_type
        )
      end

      def notify_followers
        return unless @attachment.attached_to.is_a?(Decidim::Followable)

        Decidim::EventsManager.publish(
          event: "decidim.events.attachments.attachment_created",
          event_class: Decidim::AttachmentCreatedEvent,
          resource: @attachment,
          followers: @attachment.attached_to.followers
        )
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-admin-0.26.10 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.9 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.8 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.7 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.5 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.4 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.3 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.2 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.1 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.0 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.0.rc2 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.26.0.rc1 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.25.2 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.25.1 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.25.0 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.25.0.rc4 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.25.0.rc3 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.25.0.rc2 app/commands/decidim/admin/create_attachment.rb
decidim-admin-0.25.0.rc1 app/commands/decidim/admin/create_attachment.rb