Class: Dscf::Core::Attachable::MultipleAttachmentProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/models/concerns/dscf/core/attachable.rb

Overview

MultipleAttachmentProxy - handles has_many_files attachments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, name, options) ⇒ MultipleAttachmentProxy

Returns a new instance of MultipleAttachmentProxy.



226
227
228
229
230
231
232
# File 'app/models/concerns/dscf/core/attachable.rb', line 226

def initialize(record, name, options)
  @record = record
  @name = name.to_s
  @options = options
  @errors = []
  @uploader_context = nil
end

Instance Attribute Details

#errorsArray<String> (readonly)

Get upload errors

Returns:

  • (Array<String>)


384
385
386
# File 'app/models/concerns/dscf/core/attachable.rb', line 384

def errors
  @errors
end

Instance Method Details

#as_json(options = {}) ⇒ Array<Hash>

Serialize for JSON

Returns:

  • (Array<Hash>)


388
389
390
# File 'app/models/concerns/dscf/core/attachable.rb', line 388

def as_json(options = {})
  attachments.map { |att| att.as_json(options) }
end

#attach(files, replace: false) ⇒ Boolean

Attach one or more files

Parameters:

  • files (ActionDispatch::Http::UploadedFile, Array<ActionDispatch::Http::UploadedFile>)
  • replace (Boolean) (defaults to: false)

    whether to replace existing files (default: false)

Returns:

  • (Boolean)

    success status



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/models/concerns/dscf/core/attachable.rb', line 246

def attach(files, replace: false)
  @errors = []
  files = Array(files).compact
  return true if files.empty?

  # Purge existing if replacing
  purge_all if replace

  uploader = FileStorage::Uploader.new(uploader_options)
  current_position = attachments.maximum(:position).to_i

  files.each do |file|
    result = uploader.upload(file)

    if result
      current_position += 1
      @record.file_attachments.create!(
        name: @name,
        file_key: result[:file_key],
        filename: result[:filename],
        content_type: result[:content_type],
        size: result[:size],
        metadata: result[:metadata] || {},
        position: current_position,
        uploaded_by: @uploader_context
      )
    else
      @errors.concat(uploader.errors)
    end
  end

  @attachments = nil # Reset cache
  @uploader_context = nil # Reset uploader context
  @errors.empty?
rescue StandardError => e
  Rails.logger.error("Failed to attach files: #{e.message}")
  @errors << "Failed to attach files: #{e.message}"
  false
end

#attached?Boolean

Check if any files are attached

Returns:

  • (Boolean)


308
309
310
# File 'app/models/concerns/dscf/core/attachable.rb', line 308

def attached?
  attachments.exists?
end

#attachmentsActiveRecord::Relation

Get all FileAttachment records for this attachment name

Returns:

  • (ActiveRecord::Relation)


314
315
316
# File 'app/models/concerns/dscf/core/attachable.rb', line 314

def attachments
  @attachments ||= @record.file_attachments.with_name(@name).ordered
end

#countInteger Also known as: size, length

Get count of attached files

Returns:

  • (Integer)


320
321
322
# File 'app/models/concerns/dscf/core/attachable.rb', line 320

def count
  attachments.count
end

#detach(file_key) ⇒ Boolean

Remove a specific file by its file_key

Parameters:

  • file_key (String)

Returns:

  • (Boolean)


289
290
291
292
293
294
295
296
# File 'app/models/concerns/dscf/core/attachable.rb', line 289

def detach(file_key)
  att = attachments.find_by(file_key: file_key)
  return false unless att

  att.purge
  @attachments = nil
  true
end

#empty?Boolean

Check if empty

Returns:

  • (Boolean)


329
330
331
# File 'app/models/concerns/dscf/core/attachable.rb', line 329

def empty?
  !attached?
end

#find_by_key(file_key) ⇒ FileAttachment?

Find attachment by file_key

Parameters:

  • file_key (String)

Returns:



348
349
350
# File 'app/models/concerns/dscf/core/attachable.rb', line 348

def find_by_key(file_key)
  attachments.find_by(file_key: file_key)
end

#firstFileAttachment?

Get first attachment

Returns:



335
336
337
# File 'app/models/concerns/dscf/core/attachable.rb', line 335

def first
  attachments.first
end

#human_total_sizeString

Get human-readable total size

Returns:

  • (String)


378
379
380
# File 'app/models/concerns/dscf/core/attachable.rb', line 378

def human_total_size
  ActiveSupport::NumberHelper.number_to_human_size(total_size)
end

#imagesActiveRecord::Relation

Filter to only images

Returns:

  • (ActiveRecord::Relation)


354
355
356
# File 'app/models/concerns/dscf/core/attachable.rb', line 354

def images
  attachments.images
end

#lastFileAttachment?

Get last attachment

Returns:



341
342
343
# File 'app/models/concerns/dscf/core/attachable.rb', line 341

def last
  attachments.last
end

#pdfsActiveRecord::Relation

Filter to only PDFs

Returns:

  • (ActiveRecord::Relation)


360
361
362
# File 'app/models/concerns/dscf/core/attachable.rb', line 360

def pdfs
  attachments.pdfs
end

#purge_allBoolean

Remove all attached files

Returns:

  • (Boolean)


300
301
302
303
304
# File 'app/models/concerns/dscf/core/attachable.rb', line 300

def purge_all
  attachments.each(&:purge)
  @attachments = nil
  true
end

#total_sizeInteger

Get total size of all attachments

Returns:

  • (Integer)


372
373
374
# File 'app/models/concerns/dscf/core/attachable.rb', line 372

def total_size
  attachments.sum(:size)
end

#uploaded_by(user) ⇒ self

Set the user who is uploading the files

Parameters:

  • user (ActiveRecord::Base)

    the user performing the upload

Returns:

  • (self)


237
238
239
240
# File 'app/models/concerns/dscf/core/attachable.rb', line 237

def uploaded_by(user)
  @uploader_context = user
  self
end

#videosActiveRecord::Relation

Filter to only videos

Returns:

  • (ActiveRecord::Relation)


366
367
368
# File 'app/models/concerns/dscf/core/attachable.rb', line 366

def videos
  attachments.videos
end