Sha256: 8d709154774824e5fc0d6ebf0bf7ad367d0383fa585e1f0abca046138417d6f8

Contents?: true

Size: 1.51 KB

Versions: 19

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Decidim
  module MultipleAttachmentsMethods
    private

    def build_attachments
      @documents = []
      @form.add_documents.each do |file|
        @documents << Attachment.new(
          title: { I18n.locale => file.original_filename },
          attached_to: @attached_to || documents_attached_to,
          file: file,
          content_type: file.content_type
        )
      end
    end

    def attachments_invalid?
      @documents.each do |document|
        next if document.valid? || !document.errors.has_key?(:file)

        document.errors[:file].each do |error|
          @form.errors.add(:add_documents, error)
        end

        return true
      end

      false
    end

    def create_attachments
      @documents.map! do |document|
        document.attached_to = documents_attached_to
        document.save!
        @form.documents << document
      end
    end

    def document_cleanup!
      documents_attached_to.documents.each do |document|
        document.destroy! if @form.documents.map(&:id).exclude? document.id
      end

      documents_attached_to.reload
      documents_attached_to.instance_variable_set(:@documents, nil)
    end

    def process_attachments?
      @form.add_documents.any?
    end

    def documents_attached_to
      return @attached_to if @attached_to.present?
      return form.current_organization if form.respond_to?(:current_organization)

      form.current_component.organization if form.respond_to?(:current_component)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

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