@@ -149,6 +149,42 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
149
149
assert_not ActiveStorage ::Blob . service . exist? ( @user . highlights . second . key )
150
150
end
151
151
152
+ test "replacing existing, dependent attachments on an existing record via assign and attach" do
153
+ [ create_blob ( filename : "funky.jpg" ) , create_blob ( filename : "town.jpg" ) ] . tap do |old_blobs |
154
+ @user . highlights . attach old_blobs
155
+
156
+ @user . highlights = [ ]
157
+ assert_not @user . highlights . attached?
158
+
159
+ perform_enqueued_jobs do
160
+ @user . highlights . attach create_blob ( filename : "whenever.jpg" ) , create_blob ( filename : "wherever.jpg" )
161
+ end
162
+
163
+ assert_equal "whenever.jpg" , @user . highlights . first . filename . to_s
164
+ assert_equal "wherever.jpg" , @user . highlights . second . filename . to_s
165
+ assert_not ActiveStorage ::Blob . exists? ( old_blobs . first . id )
166
+ assert_not ActiveStorage ::Blob . exists? ( old_blobs . second . id )
167
+ assert_not ActiveStorage ::Blob . service . exist? ( old_blobs . first . key )
168
+ assert_not ActiveStorage ::Blob . service . exist? ( old_blobs . second . key )
169
+ end
170
+ end
171
+
172
+ test "replacing existing, independent attachments on an existing record via assign and attach" do
173
+ [ create_blob ( filename : "funky.mp4" ) , create_blob ( filename : "town.mp4" ) ] . tap do |old_blobs |
174
+ @user . vlogs . attach old_blobs
175
+
176
+ @user . vlogs = [ ]
177
+ assert_not @user . vlogs . attached?
178
+
179
+ assert_no_enqueued_jobs only : ActiveStorage ::PurgeJob do
180
+ @user . vlogs . attach create_blob ( filename : "whenever.mp4" ) , create_blob ( filename : "wherever.mp4" )
181
+ end
182
+
183
+ assert_equal "whenever.mp4" , @user . vlogs . first . filename . to_s
184
+ assert_equal "wherever.mp4" , @user . vlogs . second . filename . to_s
185
+ end
186
+ end
187
+
152
188
test "successfully updating an existing record to replace existing, dependent attachments" do
153
189
[ create_blob ( filename : "funky.jpg" ) , create_blob ( filename : "town.jpg" ) ] . tap do |old_blobs |
154
190
@user . highlights . attach old_blobs
0 commit comments