diff --git a/history.md b/history.md
index a211ca7..695379a 100644
--- a/history.md
+++ b/history.md
@@ -11,6 +11,8 @@
* DesignDoc cache refreshed if a database is deleted.
* Fixing dirty tracking on collection_of association.
* Uniqueness Validation views created on initialization, not on demand!
+ * #destroy freezes object instead of removing _id and _rev, better for callbacks (pointer by karmi)
+ * #destroyed? method now available
## 1.1.0.beta5 - 2011-04-30
diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb
index e77b663..977bbe2 100644
--- a/lib/couchrest/model/persistence.rb
+++ b/lib/couchrest/model/persistence.rb
@@ -54,19 +54,21 @@ module CouchRest
end
# Deletes the document from the database. Runs the :destroy callbacks.
- # Removes the _id and _rev fields, preparing the
- # document to be saved to a new _id if required.
def destroy
_run_destroy_callbacks do
result = database.delete_doc(self)
if result['ok']
- self.delete('_rev')
- self.delete('_id')
+ @_destroyed = true
+ self.freeze
end
result['ok']
end
end
+ def destroyed?
+ !!@_destroyed
+ end
+
# Update the document's attributes and save. For example:
#
# doc.update_attributes :name => "Fred"
diff --git a/spec/couchrest/persistence_spec.rb b/spec/couchrest/persistence_spec.rb
index 86ded62..4c0014c 100644
--- a/spec/couchrest/persistence_spec.rb
+++ b/spec/couchrest/persistence_spec.rb
@@ -5,6 +5,7 @@ require File.join(FIXTURE_PATH, 'more', 'cat')
require File.join(FIXTURE_PATH, 'more', 'article')
require File.join(FIXTURE_PATH, 'more', 'course')
require File.join(FIXTURE_PATH, 'more', 'card')
+require File.join(FIXTURE_PATH, 'more', 'event')
describe "Model Persistence" do
@@ -242,25 +243,34 @@ describe "Model Persistence" do
@templated.id.should == 'very-important'
end
end
-
+
describe "destroying an instance" do
before(:each) do
- @dobj = Basic.new
+ @dobj = Event.new
@dobj.save.should be_true
end
it "should return true" do
result = @dobj.destroy
result.should be_true
end
- it "should be resavable" do
- @dobj.destroy
- @dobj.rev.should be_nil
- @dobj.id.should be_nil
- @dobj.save.should be_true
- end
it "should make it go away" do
@dobj.destroy
- lambda{Basic.get!(@dobj.id)}.should raise_error
+ lambda{Basic.get!(@dobj.id)}.should raise_error(RestClient::ResourceNotFound)
+ end
+ it "should freeze the object" do
+ @dobj.destroy
+ # In Ruby 1.9.2 this raises RuntimeError, in 1.8.7 TypeError, D'OH!
+ lambda { @dobj.subject = "Test" }.should raise_error(StandardError)
+ end
+ it "trying to save after should fail" do
+ @dobj.destroy
+ lambda { @dobj.save }.should raise_error(StandardError)
+ lambda{Basic.get!(@dobj.id)}.should raise_error(RestClient::ResourceNotFound)
+ end
+ it "should make destroyed? true" do
+ @dobj.destroyed?.should be_false
+ @dobj.destroy
+ @dobj.destroyed?.should be_true
end
end