attachinary icon indicating copy to clipboard operation
attachinary copied to clipboard

destroy_file callback not invoked

Open fabn opened this issue 9 years ago • 4 comments

I've just integrated attachinary in my project and everything is working. However when an Attachinary::File is destroyed remote file is not removed. My environment:

  * rails (4.2.6)
  * attachinary (1.3.1)
  * cloudinary (1.1.7)

Failing test:

it 'should remove remote file', :vcr do
  expect(Cloudinary.config.attachinary_keep_remote).to be_falsey
  expect(item.postcard).to_not be_nil
  # File should be removed
  expect(item.postcard).to receive(:destroy_file).and_call_original
  expect(Cloudinary::Uploader).to receive(:destroy).with(item.postcard.public_id).and_call_original
  item.update!(postcard: nil)
end

In any case, I think that destroy_file should be called in an after_commit callback instead of after_destroy otherwise if for any reason transaction is aborted record is no longer valid.

fabn avatar Sep 24 '16 10:09 fabn

I think I've found the culprit of this issue in this line. You're calling #clear which trigger a delete_all, thus callback are not invoked.

I'll see how to fix this and if I found a good solution I'll sketch a PR for this issue.

fabn avatar Sep 24 '16 10:09 fabn

Any news about this and related PR #139??

fabn avatar Dec 20 '16 11:12 fabn

This looks good, but I'm worried about it will break compatibility with older versions. (I don't how people with mongoid had been handling this).

orlando avatar Dec 26 '16 15:12 orlando

Compatibility with mongoid is addressed in the if statement and old versions have a bug because they don't destroy associated file on cloudinary.

fabn avatar Dec 26 '16 15:12 fabn