diff options
author | George Claghorn <george@basecamp.com> | 2018-07-17 09:33:28 -0400 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2018-07-17 09:33:48 -0400 |
commit | e13e16f5adf3221f4a95e7d105efb2d5e56c0b53 (patch) | |
tree | d9565b52a8b6afcaf31d8e51802c433a686cb66e /activestorage | |
parent | 379d98dcd40f28fd959f42958d662b5416171eea (diff) | |
download | rails-e13e16f5adf3221f4a95e7d105efb2d5e56c0b53.tar.gz rails-e13e16f5adf3221f4a95e7d105efb2d5e56c0b53.tar.bz2 rails-e13e16f5adf3221f4a95e7d105efb2d5e56c0b53.zip |
Fix replacing many attachments via assign and attach
Diffstat (limited to 'activestorage')
-rw-r--r-- | activestorage/lib/active_storage/attached/changes/delete_many.rb | 4 | ||||
-rw-r--r-- | activestorage/test/models/attached/many_test.rb | 36 |
2 files changed, 40 insertions, 0 deletions
diff --git a/activestorage/lib/active_storage/attached/changes/delete_many.rb b/activestorage/lib/active_storage/attached/changes/delete_many.rb index 5c7fe385de..6cbd1158dc 100644 --- a/activestorage/lib/active_storage/attached/changes/delete_many.rb +++ b/activestorage/lib/active_storage/attached/changes/delete_many.rb @@ -12,6 +12,10 @@ module ActiveStorage ActiveStorage::Attachment.none end + def blobs + ActiveStorage::Blob.none + end + def save record.public_send("#{name}_attachments=", []) end diff --git a/activestorage/test/models/attached/many_test.rb b/activestorage/test/models/attached/many_test.rb index ce77abc244..24974a78a6 100644 --- a/activestorage/test/models/attached/many_test.rb +++ b/activestorage/test/models/attached/many_test.rb @@ -149,6 +149,42 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase assert_not ActiveStorage::Blob.service.exist?(@user.highlights.second.key) end + test "replacing existing, dependent attachments on an existing record via assign and attach" do + [ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |old_blobs| + @user.highlights.attach old_blobs + + @user.highlights = [] + assert_not @user.highlights.attached? + + perform_enqueued_jobs do + @user.highlights.attach create_blob(filename: "whenever.jpg"), create_blob(filename: "wherever.jpg") + end + + assert_equal "whenever.jpg", @user.highlights.first.filename.to_s + assert_equal "wherever.jpg", @user.highlights.second.filename.to_s + assert_not ActiveStorage::Blob.exists?(old_blobs.first.id) + assert_not ActiveStorage::Blob.exists?(old_blobs.second.id) + assert_not ActiveStorage::Blob.service.exist?(old_blobs.first.key) + assert_not ActiveStorage::Blob.service.exist?(old_blobs.second.key) + end + end + + test "replacing existing, independent attachments on an existing record via assign and attach" do + [ create_blob(filename: "funky.mp4"), create_blob(filename: "town.mp4") ].tap do |old_blobs| + @user.vlogs.attach old_blobs + + @user.vlogs = [] + assert_not @user.vlogs.attached? + + assert_no_enqueued_jobs only: ActiveStorage::PurgeJob do + @user.vlogs.attach create_blob(filename: "whenever.mp4"), create_blob(filename: "wherever.mp4") + end + + assert_equal "whenever.mp4", @user.vlogs.first.filename.to_s + assert_equal "wherever.mp4", @user.vlogs.second.filename.to_s + end + end + test "successfully updating an existing record to replace existing, dependent attachments" do [ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |old_blobs| @user.highlights.attach old_blobs |