Destroy method now freezes instead of removing ids

This commit is contained in:
Sam Lown 2011-06-05 11:21:01 +02:00
parent d56179aa6b
commit 36157a01d3
3 changed files with 27 additions and 13 deletions

View file

@ -11,6 +11,8 @@
* DesignDoc cache refreshed if a database is deleted. * DesignDoc cache refreshed if a database is deleted.
* Fixing dirty tracking on collection_of association. * Fixing dirty tracking on collection_of association.
* Uniqueness Validation views created on initialization, not on demand! * 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 ## 1.1.0.beta5 - 2011-04-30

View file

@ -54,19 +54,21 @@ module CouchRest
end end
# Deletes the document from the database. Runs the :destroy callbacks. # Deletes the document from the database. Runs the :destroy callbacks.
# Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
# document to be saved to a new <tt>_id</tt> if required.
def destroy def destroy
_run_destroy_callbacks do _run_destroy_callbacks do
result = database.delete_doc(self) result = database.delete_doc(self)
if result['ok'] if result['ok']
self.delete('_rev') @_destroyed = true
self.delete('_id') self.freeze
end end
result['ok'] result['ok']
end end
end end
def destroyed?
!!@_destroyed
end
# Update the document's attributes and save. For example: # Update the document's attributes and save. For example:
# #
# doc.update_attributes :name => "Fred" # doc.update_attributes :name => "Fred"

View file

@ -5,6 +5,7 @@ require File.join(FIXTURE_PATH, 'more', 'cat')
require File.join(FIXTURE_PATH, 'more', 'article') require File.join(FIXTURE_PATH, 'more', 'article')
require File.join(FIXTURE_PATH, 'more', 'course') require File.join(FIXTURE_PATH, 'more', 'course')
require File.join(FIXTURE_PATH, 'more', 'card') require File.join(FIXTURE_PATH, 'more', 'card')
require File.join(FIXTURE_PATH, 'more', 'event')
describe "Model Persistence" do describe "Model Persistence" do
@ -242,25 +243,34 @@ describe "Model Persistence" do
@templated.id.should == 'very-important' @templated.id.should == 'very-important'
end end
end end
describe "destroying an instance" do describe "destroying an instance" do
before(:each) do before(:each) do
@dobj = Basic.new @dobj = Event.new
@dobj.save.should be_true @dobj.save.should be_true
end end
it "should return true" do it "should return true" do
result = @dobj.destroy result = @dobj.destroy
result.should be_true result.should be_true
end 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 it "should make it go away" do
@dobj.destroy @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
end end