Handle another case where a blob might be erroneously purged

This commit is contained in:
George Claghorn 2018-03-04 18:54:12 -05:00
parent 968c499aca
commit 7e658588fa
2 changed files with 22 additions and 5 deletions

@ -64,20 +64,25 @@ def purge_later
private
def replace(attachable)
unless attachable == blob
previous_blob = blob
blob_was = blob
blob = create_blob_from(attachable)
unless blob == blob_was
transaction do
detach
write_attachment build_attachment_from(attachable)
write_attachment build_attachment(blob: blob)
end
previous_blob.purge_later
blob_was.purge_later
end
end
def build_attachment_from(attachable)
ActiveStorage::Attachment.new(record: record, name: name, blob: create_blob_from(attachable))
build_attachment blob: create_blob_from(attachable)
end
def build_attachment(blob:)
ActiveStorage::Attachment.new(record: record, name: name, blob: blob)
end
def write_attachment(attachment)

@ -68,6 +68,18 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
end
end
test "replaced attached blob with itself by signed ID" do
@user.avatar.attach create_blob(filename: "funky.jpg")
assert_no_changes -> { @user.reload.avatar.blob } do
assert_no_changes -> { @user.reload.avatar.attachment } do
assert_no_enqueued_jobs do
@user.avatar.attach @user.avatar.blob.signed_id
end
end
end
end
test "attach blob to new record" do
user = User.new(name: "Jason")