Class: Dscf::Core::Attachable::MultipleAttachmentProxy
- Inherits:
-
Object
- Object
- Dscf::Core::Attachable::MultipleAttachmentProxy
- Includes:
- Enumerable
- Defined in:
- app/models/concerns/dscf/core/attachable.rb
Overview
MultipleAttachmentProxy - handles has_many_files attachments
Instance Attribute Summary collapse
-
#errors ⇒ Array<String>
readonly
Get upload errors.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Array<Hash>
Serialize for JSON.
-
#attach(files, replace: false) ⇒ Boolean
Attach one or more files.
-
#attached? ⇒ Boolean
Check if any files are attached.
-
#attachments ⇒ ActiveRecord::Relation
Get all FileAttachment records for this attachment name.
-
#count ⇒ Integer
(also: #size, #length)
Get count of attached files.
-
#detach(file_key) ⇒ Boolean
Remove a specific file by its file_key.
-
#empty? ⇒ Boolean
Check if empty.
-
#find_by_key(file_key) ⇒ FileAttachment?
Find attachment by file_key.
-
#first ⇒ FileAttachment?
Get first attachment.
-
#human_total_size ⇒ String
Get human-readable total size.
-
#images ⇒ ActiveRecord::Relation
Filter to only images.
-
#initialize(record, name, options) ⇒ MultipleAttachmentProxy
constructor
A new instance of MultipleAttachmentProxy.
-
#last ⇒ FileAttachment?
Get last attachment.
-
#pdfs ⇒ ActiveRecord::Relation
Filter to only PDFs.
-
#purge_all ⇒ Boolean
Remove all attached files.
-
#total_size ⇒ Integer
Get total size of all attachments.
-
#uploaded_by(user) ⇒ self
Set the user who is uploading the files.
-
#videos ⇒ ActiveRecord::Relation
Filter to only videos.
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, ) @record = record @name = name.to_s @options = @errors = [] @uploader_context = nil end |
Instance Attribute Details
#errors ⇒ Array<String> (readonly)
Get upload errors
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
388 389 390 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 388 def as_json( = {}) .map { |att| att.as_json() } end |
#attach(files, replace: false) ⇒ Boolean
Attach one or more files
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() current_position = .maximum(:position).to_i files.each do |file| result = uploader.upload(file) if result current_position += 1 @record..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.}") @errors << "Failed to attach files: #{e.}" false end |
#attached? ⇒ Boolean
Check if any files are attached
308 309 310 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 308 def attached? .exists? end |
#attachments ⇒ ActiveRecord::Relation
Get all FileAttachment records for this attachment name
314 315 316 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 314 def @attachments ||= @record..with_name(@name).ordered end |
#count ⇒ Integer Also known as: size, length
Get count of attached files
320 321 322 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 320 def count .count end |
#detach(file_key) ⇒ Boolean
Remove a specific file by its file_key
289 290 291 292 293 294 295 296 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 289 def detach(file_key) att = .find_by(file_key: file_key) return false unless att att.purge @attachments = nil true end |
#empty? ⇒ Boolean
Check if empty
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
348 349 350 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 348 def find_by_key(file_key) .find_by(file_key: file_key) end |
#first ⇒ FileAttachment?
Get first attachment
335 336 337 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 335 def first .first end |
#human_total_size ⇒ String
Get human-readable total size
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 |
#images ⇒ ActiveRecord::Relation
Filter to only images
354 355 356 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 354 def images .images end |
#last ⇒ FileAttachment?
Get last attachment
341 342 343 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 341 def last .last end |
#pdfs ⇒ ActiveRecord::Relation
Filter to only PDFs
360 361 362 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 360 def pdfs .pdfs end |
#purge_all ⇒ Boolean
Remove all attached files
300 301 302 303 304 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 300 def purge_all .each(&:purge) @attachments = nil true end |
#total_size ⇒ Integer
Get total size of all attachments
372 373 374 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 372 def total_size .sum(:size) end |
#uploaded_by(user) ⇒ self
Set the user who is uploading the files
237 238 239 240 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 237 def uploaded_by(user) @uploader_context = user self end |
#videos ⇒ ActiveRecord::Relation
Filter to only videos
366 367 368 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 366 def videos .videos end |