Skip to content

Commit

Permalink
Delete underlying file/asset when parent edition is discarded
Browse files Browse the repository at this point in the history
This rectifies the inconsistency in behaviour explained in the previous
commits.

Calling `Attachment#destroy` for each attachment on the edition
triggers the `after_destroy` callback on `FileAttachment` which in turn
will delete its `AttachmentData` and its underlying file and Asset
Manager asset if no other attachments are still referring to the
`AttachmentData`.
  • Loading branch information
floehopper committed Mar 3, 2018
1 parent d5fd7a3 commit 2c15aec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/attachable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def next_ordering
end

def delete_all_attachments
attachments.each { |attachment| attachment.update(deleted: true) }
attachments.each(&:destroy)
end

def reorder_attachments(ordered_attachment_ids)
Expand Down
11 changes: 9 additions & 2 deletions test/integration/attachment_deletion_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ class AttachmentDeletionIntegrationTest < ActionDispatch::IntegrationTest
it 'responds with 404 Not Found for attachment URL' do
logout

get @attachment_url
assert_response :not_found
assert_raises(ActiveRecord::RecordNotFound) do
get @attachment_url
end
end

it 'deletes attachment in Asset Manager' do
Services.asset_manager.expects(:delete_asset)
.with(asset_id)
AssetManagerDeleteAssetWorker.drain
end
end
end
Expand Down

0 comments on commit 2c15aec

Please sign in to comment.