Sha256: aedd4740a338d57894a2ccb82f89c9741716fb373245de5c30ee89d499608606

Contents?: true

Size: 1.79 KB

Versions: 13

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module Decidim
  # A module with all the attachment common methods
  module AttachmentMethods
    private

    def build_attachment(attached_to = nil)
      attached_to = @attached_to if attached_to.blank?
      attached_to = form.current_organization if attached_to.blank? && form.respond_to?(:current_organization)

      @attachment = Attachment.new(
        title: { I18n.locale => @form.attachment.title },
        attached_to:,
        file: signed_id_for(@form.attachment.file),
        content_type: content_type_for(@form.attachment.file)
      )
    end

    def attachment_invalid?
      if attachment.invalid? && attachment.errors.has_key?(:file)
        @form.attachment.errors.add :file, attachment.errors[:file]
        true
      end
    end

    def attachment_present?
      @form.attachment.file.present?
    end

    def attachment_file_uploaded?
      !@form.attachment.file.is_a?(Decidim::ApplicationUploader)
    end

    def create_attachment(weight: 0)
      attachment.weight = weight
      attachment.attached_to = @attached_to
      attachment.save!
    end

    def attachments_allowed?
      @form.current_component.settings.attachments_allowed?
    end

    def process_attachments?
      attachments_allowed? && attachment_present? && attachment_file_uploaded?
    end

    def delete_attachment?
      @form.attachment&.delete_file.present?
    end

    protected

    def signed_id_for(attachment)
      return attachment[:file] if attachment.is_a?(Hash)

      attachment
    end

    def content_type_for(attachment)
      return attachment.content_type if attachment.instance_of?(ActionDispatch::Http::UploadedFile)

      blob(signed_id_for(attachment)).content_type
    end

    def blob(signed_id)
      ActiveStorage::Blob.find_signed(signed_id)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/commands/decidim/attachment_methods.rb
decidim-core-0.28.4 app/commands/decidim/attachment_methods.rb
decidim-core-0.29.0 app/commands/decidim/attachment_methods.rb
decidim-core-0.28.3 app/commands/decidim/attachment_methods.rb
decidim-core-0.29.0.rc4 app/commands/decidim/attachment_methods.rb
decidim-core-0.29.0.rc3 app/commands/decidim/attachment_methods.rb
decidim-core-0.29.0.rc2 app/commands/decidim/attachment_methods.rb
decidim-core-0.29.0.rc1 app/commands/decidim/attachment_methods.rb
decidim-core-0.28.2 app/commands/decidim/attachment_methods.rb
decidim-core-0.28.1 app/commands/decidim/attachment_methods.rb
decidim-core-0.28.0 app/commands/decidim/attachment_methods.rb
decidim-core-0.28.0.rc5 app/commands/decidim/attachment_methods.rb
decidim-core-0.28.0.rc4 app/commands/decidim/attachment_methods.rb