diff options
author | George Claghorn <george@basecamp.com> | 2018-03-05 17:01:31 -0500 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2018-03-05 17:01:31 -0500 |
commit | 9cc88043e70f927a3c8b151c862f6b3cb8b8a6f7 (patch) | |
tree | d3ccb0a4997b78dfc2151fe72d09f85980c41bd3 /activestorage/lib/active_storage | |
parent | 8228d12a43927a22d0ac9bd351d18f473d6a49ae (diff) | |
download | rails-9cc88043e70f927a3c8b151c862f6b3cb8b8a6f7.tar.gz rails-9cc88043e70f927a3c8b151c862f6b3cb8b8a6f7.tar.bz2 rails-9cc88043e70f927a3c8b151c862f6b3cb8b8a6f7.zip |
Fix purging dependent blobs when attachments aren't loaded
Diffstat (limited to 'activestorage/lib/active_storage')
-rw-r--r-- | activestorage/lib/active_storage/attached/macros.rb | 8 | ||||
-rw-r--r-- | activestorage/lib/active_storage/attached/one.rb | 32 |
2 files changed, 17 insertions, 23 deletions
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb index a87f02efe1..1bda86369e 100644 --- a/activestorage/lib/active_storage/attached/macros.rb +++ b/activestorage/lib/active_storage/attached/macros.rb @@ -38,13 +38,15 @@ module ActiveStorage end CODE - has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record, dependent: :delete + has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record, dependent: false has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob scope :"with_attached_#{name}", -> { includes("#{name}_attachment": :blob) } if dependent == :purge_later after_destroy_commit { public_send(name).purge_later } + else + before_destroy { public_send(name).detach } end end @@ -83,13 +85,15 @@ module ActiveStorage end CODE - has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: :delete_all + has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: false has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob scope :"with_attached_#{name}", -> { includes("#{name}_attachments": :blob) } if dependent == :purge_later after_destroy_commit { public_send(name).purge_later } + else + before_destroy { public_send(name).detach } end end end diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb index 008f5a796b..a582ed0137 100644 --- a/activestorage/lib/active_storage/attached/one.rb +++ b/activestorage/lib/active_storage/attached/one.rb @@ -20,10 +20,16 @@ module ActiveStorage # person.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/jpg") # person.avatar.attach(avatar_blob) # ActiveStorage::Blob object def attach(attachable) - if attached? && dependent == :purge_later - replace attachable - else - write_attachment build_attachment_from(attachable) + blob_was = blob if attached? + blob = create_blob_from(attachable) + + unless blob == blob_was + transaction do + detach + write_attachment build_attachment(blob: blob) + end + + blob_was.purge_later if blob_was && dependent == :purge_later end end @@ -63,23 +69,7 @@ module ActiveStorage end private - def replace(attachable) - blob_was = blob - blob = create_blob_from(attachable) - - unless blob == blob_was - transaction do - detach - write_attachment build_attachment(blob: blob) - end - - blob_was.purge_later - end - end - - def build_attachment_from(attachable) - build_attachment blob: create_blob_from(attachable) - end + delegate :transaction, to: :record def build_attachment(blob:) ActiveStorage::Attachment.new(record: record, name: name, blob: blob) |