Orphaned attachment relationship safe navigation

closes #42514

- Adds Test for Orphaned Attachment Relationship
- Adds Changelog entry (with issue number)
This commit is contained in:
Don Sisco 2021-06-16 15:22:38 -04:00 committed by Donald Sisco
parent acee501edd
commit f329ae8886
3 changed files with 15 additions and 1 deletions

View File

@ -1,3 +1,9 @@
* Attachments can be deleted after their association is no longer defined.
Fixes #42514
*Don Sisco*
* Allow to detach an attachment when record is not persisted
*Jacopo Beschi*

View File

@ -74,7 +74,7 @@ class ActiveStorage::Attachment < ActiveStorage::Record
end
def dependent
record.attachment_reflections[name]&.options[:dependent]
record.attachment_reflections[name]&.options&.fetch(:dependent, nil)
end
def variants

View File

@ -134,6 +134,14 @@ class ActiveStorage::AttachmentTest < ActiveSupport::TestCase
assert_equal blob, ActiveStorage::Blob.find_signed(signed_id)
end
test "can destroy attachment without existing relation" do
blob = create_blob
@user.highlights.attach(blob)
attachment = @user.highlights.find_by(blob_id: blob.id)
attachment.update_attribute(:name, "old_highlights")
assert_nothing_raised { attachment.destroy }
end
private
def assert_blob_identified_before_owner_validated(owner, blob, content_type)
validated_content_type = nil