Sha256: 351450e0e50733b21bbc80f6947cfb44ecc7558615ac7317475f0c0bdf956e2b

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 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: attached_to,
        file: @form.attachment.file # Define attached_to before this
      )
    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
      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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-core-0.23.2 app/commands/decidim/attachment_methods.rb
decidim-core-0.23.1 app/commands/decidim/attachment_methods.rb
decidim-core-0.23.1.rc1 app/commands/decidim/attachment_methods.rb
decidim-core-0.23.0 app/commands/decidim/attachment_methods.rb