Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Mongoid::Paranoia support

Mounted uploaders are kept around if the document is paranoid
  • Loading branch information...
commit 1e75d5e2e5cde25a0f62c2dc1c3a747342720e32 1 parent 1c49f91
Ryan McGeary rmm5t authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.