Skip to content
This repository
Browse code

Added Mongoid::Paranoia support

Mounted uploaders are kept around if the document is paranoid
  • Loading branch information...
commit 1e75d5e2e5cde25a0f62c2dc1c3a747342720e32 1 parent 1c49f91
Ryan McGeary authored February 11, 2013
4  lib/carrierwave/mongoid.rb
@@ -39,6 +39,10 @@ def #{column}=(new_file)
39 39
           super
40 40
         end
41 41
 
  42
+        def remove_#{column}!
  43
+          super unless paranoid? && flagged_for_destroy?
  44
+        end
  45
+
42 46
         # Overrides Mongoid's default dirty behavior to instead work more like
43 47
         # ActiveRecord's. Mongoid doesn't deem an attribute as changed unless
44 48
         # the new value is different than the original. Given that CarrierWave
27  spec/mongoid_spec.rb
@@ -766,6 +766,33 @@ def filename
766 766
     end
767 767
   end
768 768
 
  769
+  describe "with paranoia enabled" do
  770
+    before do
  771
+      @class = reset_mongo_class
  772
+      @class.collection.drop
  773
+      @class.class_eval do
  774
+        include Mongoid::Paranoia
  775
+      end
  776
+
  777
+      @doc = @class.new(image: stub_file("old.jpeg"))
  778
+      @doc.save.should be_true
  779
+    end
  780
+
  781
+    it "should not remove underlying image after #destroy" do
  782
+      @doc.destroy.should be_true
  783
+      @class.count.should eql(0)
  784
+      @class.deleted.count.should eql(1)
  785
+      File.exist?(public_path('uploads/old.jpeg')).should be_true
  786
+    end
  787
+
  788
+    it "should remove underlying image after #destroy!" do
  789
+      @doc.destroy!.should be_true
  790
+      @class.count.should eql(0)
  791
+      @class.deleted.count.should eql(0)
  792
+      File.exist?(public_path('uploads/old.jpeg')).should be_false
  793
+    end
  794
+  end
  795
+
769 796
   context "JSON serialization with multiple uploaders" do
770 797
     before do
771 798
       @class = reset_mongo_class

0 notes on commit 1e75d5e

Please sign in to comment.
Something went wrong with that request. Please try again.