diff options
author | George Claghorn <george@basecamp.com> | 2018-03-04 18:54:12 -0500 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2018-03-04 18:54:31 -0500 |
commit | 7e658588fa15e799614268f1f733946bf2ef26cd (patch) | |
tree | 9e49401430d6392b0fbba99d77c20cb867b4983b /activestorage | |
parent | 968c499aca5ca71dc909ba7a632c5dbc4f58b101 (diff) | |
download | rails-7e658588fa15e799614268f1f733946bf2ef26cd.tar.gz rails-7e658588fa15e799614268f1f733946bf2ef26cd.tar.bz2 rails-7e658588fa15e799614268f1f733946bf2ef26cd.zip |
Handle another case where a blob might be erroneously purged
Diffstat (limited to 'activestorage')
-rw-r--r-- | activestorage/lib/active_storage/attached/one.rb | 15 | ||||
-rw-r--r-- | activestorage/test/models/attachments_test.rb | 12 |
2 files changed, 22 insertions, 5 deletions
diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb index e3600dc241..008f5a796b 100644 --- a/activestorage/lib/active_storage/attached/one.rb +++ b/activestorage/lib/active_storage/attached/one.rb @@ -64,20 +64,25 @@ module ActiveStorage private def replace(attachable) - unless attachable == blob - previous_blob = blob + 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 - previous_blob.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) diff --git a/activestorage/test/models/attachments_test.rb b/activestorage/test/models/attachments_test.rb index 4c6b3e6000..25e0352eca 100644 --- a/activestorage/test/models/attachments_test.rb +++ b/activestorage/test/models/attachments_test.rb @@ -68,6 +68,18 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase end end + test "replaced attached blob with itself by signed ID" do + @user.avatar.attach create_blob(filename: "funky.jpg") + + assert_no_changes -> { @user.reload.avatar.blob } do + assert_no_changes -> { @user.reload.avatar.attachment } do + assert_no_enqueued_jobs do + @user.avatar.attach @user.avatar.blob.signed_id + end + end + end + end + test "attach blob to new record" do user = User.new(name: "Jason") |