123456789_123456789_123456789_123456789_123456789_

Class: ActiveStorage::Attached::Changes::CreateMany

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activestorage/lib/active_storage/attached/changes/create_many.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, record, attachables, pending_uploads: []) ⇒ CreateMany

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 7

def initialize(name, record, attachables, pending_uploads: [])
  @name, @record, @attachables = name, record, Array(attachables)
  blobs.each(&:identify_without_saving)
  @pending_uploads = Array(pending_uploads) + subchanges_without_blobs
  attachments
end

Instance Attribute Details

#attachables (readonly)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 5

attr_reader :name, :record, :attachables, :pending_uploads

#name (readonly)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 5

attr_reader :name, :record, :attachables, :pending_uploads

#pending_uploads (readonly)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 5

attr_reader :name, :record, :attachables, :pending_uploads

#record (readonly)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 5

attr_reader :name, :record, :attachables, :pending_uploads

Instance Method Details

#assign_associated_attachments (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 44

def assign_associated_attachments
  record.public_send("#{name}_attachments=", persisted_or_new_attachments)
end

#attachments

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 14

def attachments
  @attachments ||= subchanges.collect(&:attachment)
end

#blobs

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 18

def blobs
  @blobs ||= subchanges.collect(&:blob)
end

#build_subchange_from(attachable) (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 36

def build_subchange_from(attachable)
  ActiveStorage::Attached::Changes::CreateOneOfMany.new(name, record, attachable)
end

#persisted_or_new_attachments (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 52

def persisted_or_new_attachments
  attachments.select { |attachment| attachment.persisted? || attachment.new_record? }
end

#reset_associated_blobs (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 48

def reset_associated_blobs
  record.public_send("#{name}_blobs").reset
end

#save

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 26

def save
  assign_associated_attachments
  reset_associated_blobs
end

#subchanges (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 32

def subchanges
  @subchanges ||= attachables.collect { |attachable| build_subchange_from(attachable) }
end

#subchanges_without_blobs (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 40

def subchanges_without_blobs
  subchanges.reject { |subchange| subchange.attachable.is_a?(ActiveStorage::Blob) }
end

#upload

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_many.rb', line 22

def upload
  pending_uploads.each(&:upload)
end