diff options
author | George Claghorn <george.claghorn@gmail.com> | 2018-07-07 23:25:33 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-07 23:25:33 -0400 |
commit | e8682c5bf051517b0b265e446aa1a7eccfd47bf7 (patch) | |
tree | cc04c8a28113bc0fa3748fdc6035d487e3e16af7 /activestorage/app/models | |
parent | 0b534cd1c814a4db2d0aa283981f1d55e5e62d25 (diff) | |
download | rails-e8682c5bf051517b0b265e446aa1a7eccfd47bf7.tar.gz rails-e8682c5bf051517b0b265e446aa1a7eccfd47bf7.tar.bz2 rails-e8682c5bf051517b0b265e446aa1a7eccfd47bf7.zip |
Store newly-uploaded files on save rather than assignment
Diffstat (limited to 'activestorage/app/models')
-rw-r--r-- | activestorage/app/models/active_storage/attachment.rb | 18 | ||||
-rw-r--r-- | activestorage/app/models/active_storage/blob.rb | 21 |
2 files changed, 29 insertions, 10 deletions
diff --git a/activestorage/app/models/active_storage/attachment.rb b/activestorage/app/models/active_storage/attachment.rb index c59877a9a5..bb80799044 100644 --- a/activestorage/app/models/active_storage/attachment.rb +++ b/activestorage/app/models/active_storage/attachment.rb @@ -15,17 +15,18 @@ class ActiveStorage::Attachment < ActiveRecord::Base delegate_missing_to :blob after_create_commit :analyze_blob_later, :identify_blob + after_destroy_commit :purge_dependent_blob_later - # Synchronously purges the blob (deletes it from the configured service) and destroys the attachment. + # Synchronously purges the blob (deletes it from the configured service) and deletes the attachment. def purge blob.purge - destroy + delete end - # Destroys the attachment and asynchronously purges the blob (deletes it from the configured service). + # Deletes the attachment and queues a background job to purge the blob (delete it from the configured service). def purge_later blob.purge_later - destroy + delete end private @@ -36,4 +37,13 @@ class ActiveStorage::Attachment < ActiveRecord::Base def analyze_blob_later blob.analyze_later unless blob.analyzed? end + + def purge_dependent_blob_later + blob.purge_later if dependent == :purge_later + end + + + def dependent + record.attachment_reflections[name]&.options[:dependent] + end end diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb index 73029f21a1..86f3dba524 100644 --- a/activestorage/app/models/active_storage/blob.rb +++ b/activestorage/app/models/active_storage/blob.rb @@ -48,15 +48,17 @@ class ActiveStorage::Blob < ActiveRecord::Base # Returns a new, unsaved blob instance after the +io+ has been uploaded to the service. # When providing a content type, pass <tt>identify: false</tt> to bypass automatic content type inference. def build_after_upload(io:, filename:, content_type: nil, metadata: nil, identify: true) - new.tap do |blob| - blob.filename = filename - blob.content_type = content_type - blob.metadata = metadata - + new(filename: filename, content_type: content_type, metadata: metadata).tap do |blob| blob.upload(io, identify: identify) end end + def build_after_unfurling(io:, filename:, content_type: nil, metadata: nil, identify: true) #:nodoc: + new(filename: filename, content_type: content_type, metadata: metadata).tap do |blob| + blob.unfurl(io, identify: identify) + end + end + # Returns a saved blob instance after the +io+ has been uploaded to the service. Note, the blob is first built, # then the +io+ is uploaded, then the blob is saved. This is done this way to avoid uploading (which may take # time), while having an open database transaction. @@ -152,12 +154,19 @@ class ActiveStorage::Blob < ActiveRecord::Base # Normally, you do not have to call this method directly at all. Use the factory class methods of +build_after_upload+ # and +create_after_upload!+. def upload(io, identify: true) + unfurl io, identify: identify + upload_without_unfurling io + end + + def unfurl(io, identify: true) #:nodoc: self.checksum = compute_checksum_in_chunks(io) self.content_type = extract_content_type(io) if content_type.nil? || identify self.byte_size = io.size self.identified = true + end - service.upload(key, io, checksum: checksum) + def upload_without_unfurling(io) #:nodoc: + service.upload key, io, checksum: checksum end # Downloads the file associated with this blob. If no block is given, the entire file is read into memory and returned. |