diff options
author | George Claghorn <george@basecamp.com> | 2018-07-16 15:57:43 -0400 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2018-07-16 15:57:43 -0400 |
commit | 36ec5428bfe78831e4c5c001bd8e7ce45c238b33 (patch) | |
tree | 4fcdf44f8dd90aac4d926da99be2f5a0b3e5ceba /activestorage | |
parent | 1d13de4e39aa8045d1829ebbec5f7a8a192ed4b2 (diff) | |
download | rails-36ec5428bfe78831e4c5c001bd8e7ce45c238b33.tar.gz rails-36ec5428bfe78831e4c5c001bd8e7ce45c238b33.tar.bz2 rails-36ec5428bfe78831e4c5c001bd8e7ce45c238b33.zip |
Fix that successive ActiveStorage::Attached::Many#attach calls would overwrite previous attachments
Diffstat (limited to 'activestorage')
-rw-r--r-- | activestorage/lib/active_storage/attached/changes/create_many.rb | 12 | ||||
-rw-r--r-- | activestorage/test/models/attached/many_test.rb | 37 |
2 files changed, 22 insertions, 27 deletions
diff --git a/activestorage/lib/active_storage/attached/changes/create_many.rb b/activestorage/lib/active_storage/attached/changes/create_many.rb index af19328a61..a7a8553e0f 100644 --- a/activestorage/lib/active_storage/attached/changes/create_many.rb +++ b/activestorage/lib/active_storage/attached/changes/create_many.rb @@ -21,7 +21,8 @@ module ActiveStorage end def save - record.public_send("#{name}_attachments=", attachments) + assign_associated_attachments + reset_associated_blobs end private @@ -32,5 +33,14 @@ module ActiveStorage def build_subchange_from(attachable) ActiveStorage::Attached::Changes::CreateOneOfMany.new(name, record, attachable) end + + + def assign_associated_attachments + record.public_send("#{name}_attachments=", attachments) + end + + def reset_associated_blobs + record.public_send("#{name}_blobs").reset + end end end diff --git a/activestorage/test/models/attached/many_test.rb b/activestorage/test/models/attached/many_test.rb index 2a45829da8..61a80b5dda 100644 --- a/activestorage/test/models/attached/many_test.rb +++ b/activestorage/test/models/attached/many_test.rb @@ -106,6 +106,17 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase assert_equal "video.mp4", @user.highlights.second.filename.to_s end + test "attaching existing records to an existing blob one at a time" do + @user.highlights.attach create_blob(filename: "funky.jpg") + @user.highlights.attach create_blob(filename: "town.jpg") + assert_equal "funky.jpg", @user.highlights.first.filename.to_s + assert_equal "town.jpg", @user.highlights.second.filename.to_s + + @user.reload + assert_equal "funky.jpg", @user.highlights.first.filename.to_s + assert_equal "town.jpg", @user.highlights.second.filename.to_s + end + test "updating an existing record to attach existing blobs" do @user.update! highlights: [ create_file_blob(filename: "racecar.jpg"), create_file_blob(filename: "video.mp4") ] assert_equal "racecar.jpg", @user.highlights.first.filename.to_s @@ -195,32 +206,6 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase end end - test "removing dependent attachments from an existing record" do - [ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs| - @user.highlights.attach blobs - - assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blobs.first ] do - assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blobs.second ] do - @user.highlights.attach [] - end - end - - assert_not @user.highlights.attached? - end - end - - test "removing independent attachments from an existing record" do - [ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs| - @user.vlogs.attach blobs - - assert_no_enqueued_jobs only: ActiveStorage::PurgeJob do - @user.vlogs.attach [] - end - - assert_not @user.vlogs.attached? - end - end - test "updating an existing record to remove dependent attachments" do [ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs| @user.highlights.attach blobs |