Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Mounted file from Mongoid::Paranoia document is hard deleted #41

Merged
merged 1 commit into from

4 participants

Anders Kindberg (Bengtsson) Ryan McGeary Luccas Maso Jean Bredeche
Anders Kindberg (Bengtsson)

Hi,

I have mounted an uploader in a document with Mongoid::Paranoia included, to prevent hard deletes. But while the document is softly deleted (i.e. has the deleted_at field set), its mounted file is hard deleted. This makes Mongoid::Paranoia useless when combined with Carrierwave.

  class MyDocument
    include Mongoid::Document
    include Mongoid::Paranoia
    mount_uploader :media, MyUploader

    # ...
  end
Ryan McGeary
Collaborator

This is similar to carrierwaveuploader/carrierwave#624. Given the nature of the dependency, I don't think this is something that carrierwave-mongoid (or carrierwave) should be responsible for.

In your document, try something like this:

skip_callback :destroy, :after, :remove_media!
Ryan McGeary rmm5t closed this
Ryan McGeary rmm5t reopened this
Ryan McGeary
Collaborator

I'm going to take back my original statement for now and instead think about it some more. Upon further review, this is slightly different than carrierwaveuploader/carrierwave#624 because acts_as_paranoid is a third party dependency while Mongoid::Paranoid is built into Mongoid.

Meanwhile, the skip_callback workaround should suffice.

Anders Kindberg (Bengtsson)

Thanks. I'm overriding remove!() with an empty method in the uploader right now, which seems to work, but it would be nice to not have to work around it.

Luccas Maso

@rmm5t The skip_callback :destroy, :after, :remove_media! seems to not work...

@ghostganz Hi, could you show how did you work around? Thanks

Anders Kindberg (Bengtsson)

I'm doing it like this:

class MediaUploader < CarrierWave::Uploader::Base
  include CarrierWave::MimeTypes
  storage :grid_fs

  def store_dir
    "#{model.id}"
  end

  def remove!(*args)
    # no-op override
  end
end
Luccas Maso

Thanks, @ghostganz, i'll give a try. Actually I have found this gist too: https://gist.github.com/1596243

Jean Bredeche

Similarly, if I already have a Mongoid document that is deleted (using Mongoid::Paranoia), and then try to upload an attachment with carrierwave and carrierwave-mongoid, the upload is done but the identifier doesn't get written to the document.

It looks like the document's changes array is empty right before save, so the new value doesn't get saved. Still investigating, but have you guys seen this?

Ryan McGeary rmm5t Added Mongoid::Paranoia support
Mounted uploaders are kept around if the document is paranoid
1e75d5e
Ryan McGeary rmm5t merged commit 8c42e27 into from
Ryan McGeary
Collaborator

Could everyone please confirm that the new paranoia support that was just added resolves the underlying issue?

gem "carrierwave-mongoid", git: "git://github.com/jnicklas/carrierwave-mongoid.git"

I'll release a new official carrierwave-mongoid release soon after.

Ryan McGeary
Collaborator

Just checking back in with everyone on this issue. If I don't hear back from anyone, I'll assume that the code changes attached to this issue resolve everyone's Mongoid::Paranoia problems but might wait for another feature or fix before issuing a new official release. On the other hand, if people would benefit from a new release, I'm glad to do that, but only after getting confirmation that this fully resolves the underlying issue.

Anders Kindberg (Bengtsson)

I'm stuck on an old version, because of the massive changes needed to go to Mongoid 3, so I won't be able to verify the fix.
I'd be happy if you have a release out by the time we've done the leap to Mongoid 3. :)

Ryan McGeary
Collaborator

@ghostganz I just back ported the paranoia support to the v0.2 line. See carrierwave-mongoid v0.2.2. If any issues, please submit a pull request against the 0.2-stable branch.

Everyone else, there's now a new v0.5.0 release that support Mongoid::Paranoia under Mongoid ~>3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 11, 2013
  1. Ryan McGeary

    Added Mongoid::Paranoia support

    rmm5t authored
    Mounted uploaders are kept around if the document is paranoid
This page is out of date. Refresh to see the latest.
Showing with 31 additions and 0 deletions.
  1. +4 −0 lib/carrierwave/mongoid.rb
  2. +27 −0 spec/mongoid_spec.rb
4 lib/carrierwave/mongoid.rb
View
@@ -39,6 +39,10 @@ def #{column}=(new_file)
super
end
+ def remove_#{column}!
+ super unless paranoid? && flagged_for_destroy?
+ end
+
# Overrides Mongoid's default dirty behavior to instead work more like
# ActiveRecord's. Mongoid doesn't deem an attribute as changed unless
# the new value is different than the original. Given that CarrierWave
27 spec/mongoid_spec.rb
View
@@ -766,6 +766,33 @@ def filename
end
end
+ describe "with paranoia enabled" do
+ before do
+ @class = reset_mongo_class
+ @class.collection.drop
+ @class.class_eval do
+ include Mongoid::Paranoia
+ end
+
+ @doc = @class.new(image: stub_file("old.jpeg"))
+ @doc.save.should be_true
+ end
+
+ it "should not remove underlying image after #destroy" do
+ @doc.destroy.should be_true
+ @class.count.should eql(0)
+ @class.deleted.count.should eql(1)
+ File.exist?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ it "should remove underlying image after #destroy!" do
+ @doc.destroy!.should be_true
+ @class.count.should eql(0)
+ @class.deleted.count.should eql(0)
+ File.exist?(public_path('uploads/old.jpeg')).should be_false
+ end
+ end
+
context "JSON serialization with multiple uploaders" do
before do
@class = reset_mongo_class
Something went wrong with that request. Please try again.