diff options
Diffstat (limited to 'activestorage/lib')
-rw-r--r-- | activestorage/lib/active_storage/attached/one.rb | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb index 0244232b2c..008f5a796b 100644 --- a/activestorage/lib/active_storage/attached/one.rb +++ b/activestorage/lib/active_storage/attached/one.rb @@ -64,16 +64,25 @@ module ActiveStorage private def replace(attachable) - blob.tap do + blob_was = blob + blob = create_blob_from(attachable) + + unless blob == blob_was transaction do detach - write_attachment build_attachment_from(attachable) + write_attachment build_attachment(blob: blob) end - end.purge_later + + blob_was.purge_later + end end def build_attachment_from(attachable) - ActiveStorage::Attachment.new(record: record, name: name, blob: create_blob_from(attachable)) + build_attachment blob: create_blob_from(attachable) + end + + def build_attachment(blob:) + ActiveStorage::Attachment.new(record: record, name: name, blob: blob) end def write_attachment(attachment) |