Sha256: 3f47e35bdd4b86fcc74df6ad69c53eaeb8654b6e882c67c8ed01d47da27b5cd4

Contents?: true

Size: 1.39 KB

Versions: 8

Compression:

Stored size: 1.39 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
        )
      end
    end

    def attachments_invalid?
      @documents.each do |file|
        if file.invalid? && file.errors.has_key?(:file)
          @form.errors.add(:add_documents, file.errors[:file])
          return true
        end
      end
      false
    end

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

    def document_cleanup!
      documents_attached_to.documents.each do |file|
        file.destroy! if @form.documents.exclude? file.id.to_s
      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

8 entries across 8 versions & 1 rubygems

Version Path
decidim-core-0.23.6 app/commands/decidim/multiple_attachments_methods.rb
decidim-core-0.23.5 app/commands/decidim/multiple_attachments_methods.rb
decidim-core-0.23.4 app/commands/decidim/multiple_attachments_methods.rb
decidim-core-0.23.3 app/commands/decidim/multiple_attachments_methods.rb
decidim-core-0.23.2 app/commands/decidim/multiple_attachments_methods.rb
decidim-core-0.23.1 app/commands/decidim/multiple_attachments_methods.rb
decidim-core-0.23.1.rc1 app/commands/decidim/multiple_attachments_methods.rb
decidim-core-0.23.0 app/commands/decidim/multiple_attachments_methods.rb