| |   |
| 383 | 383 | # Reset the do_not_load_has_many_relations variable |
| 384 | 384 | # to it's original state |
| 385 | 385 | @do_not_load_has_many_relations = state_before_wants_to_load_relations |
| 386 | | |
| 386 | |
| 387 | 387 | perform_callback(:after_save) |
| 388 | 388 | |
| 389 | 389 | return the_return_value |
| … | … | |
| 403 | 403 | |
| 404 | 404 | db = CouchObject::Database.open(location) |
| 405 | 405 | |
| 406 | | unless (response = db.post("", self.to_json)).to_document["error"] |
| 406 | json_value = self.to_json |
| 407 | |
| 408 | unless (response = db.post("", json_value)).to_document["error"] |
| 407 | 409 | response_document = response.to_document |
| 408 | 410 | @id = response_document.id |
| 409 | 411 | @revision = response_document.revision |
| … | … | |
| 414 | 414 | @updated_at = Time.now if self. |
| 415 | 415 | class::couch_object_timestamp_on_update? |
| 416 | 416 | |
| 417 | # If the user has activated smart_save, we should set the state |
| 418 | # to the new contents of this instance! |
| 419 | @couch_object_original_state = json_value if use_smart_save |
| 420 | |
| 417 | 421 | perform_callback(:after_create) |
| 418 | 422 | |
| 419 | 423 | # Returns a hash with the ID and Revision |
| … | … | |
| 457 | 457 | |
| 458 | 458 | db = CouchObject::Database.open(location) |
| 459 | 459 | |
| 460 | | unless (response = db.put(id, self.to_json)).to_document["error"] |
| 460 | json_value = self.to_json |
| 461 | |
| 462 | unless (response = db.put(id, json_value)).to_document["error"] |
| 461 | 463 | |
| 462 | 464 | @revision = response.to_document.revision |
| 463 | 465 | |
| 466 | # If the user has activated smart_save, we should set the state |
| 467 | # to the new contents of this instance! |
| 468 | @couch_object_original_state = json_value if use_smart_save |
| 469 | |
| 464 | 470 | perform_callback(:after_update) |
| 465 | 471 | |
| 466 | 472 | # Returns a hash with the ID and Revision |
| toggle raw diff |
--- a/lib/couch_object/persistable.rb
+++ b/lib/couch_object/persistable.rb
@@ -383,7 +383,7 @@ module CouchObject
# Reset the do_not_load_has_many_relations variable
# to it's original state
@do_not_load_has_many_relations = state_before_wants_to_load_relations
-
+
perform_callback(:after_save)
return the_return_value
@@ -403,7 +403,9 @@ module CouchObject
db = CouchObject::Database.open(location)
- unless (response = db.post("", self.to_json)).to_document["error"]
+ json_value = self.to_json
+
+ unless (response = db.post("", json_value)).to_document["error"]
response_document = response.to_document
@id = response_document.id
@revision = response_document.revision
@@ -412,6 +414,10 @@ module CouchObject
@updated_at = Time.now if self.
class::couch_object_timestamp_on_update?
+ # If the user has activated smart_save, we should set the state
+ # to the new contents of this instance!
+ @couch_object_original_state = json_value if use_smart_save
+
perform_callback(:after_create)
# Returns a hash with the ID and Revision
@@ -451,10 +457,16 @@ module CouchObject
db = CouchObject::Database.open(location)
- unless (response = db.put(id, self.to_json)).to_document["error"]
+ json_value = self.to_json
+
+ unless (response = db.put(id, json_value)).to_document["error"]
@revision = response.to_document.revision
+ # If the user has activated smart_save, we should set the state
+ # to the new contents of this instance!
+ @couch_object_original_state = json_value if use_smart_save
+
perform_callback(:after_update)
# Returns a hash with the ID and Revision |
| |   |
| 102 | 102 | |
| 103 | 103 | end |
| 104 | 104 | |
| 105 | it "should not have unsaved_changes after having been saved" do |
| 106 | response = HTTPResponse.new(@content_motor_bike) |
| 107 | |
| 108 | @db.should_receive(:get).with("123BAC").and_return(response) |
| 109 | @db.should_receive(:put).and_return(@document_response) |
| 110 | |
| 111 | mbike = OtherMotorBike.get_by_id("123BAC", "foo") |
| 112 | |
| 113 | mbike.unsaved_changes?.should == false |
| 114 | mbike.wheels = 3 |
| 115 | mbike.unsaved_changes?.should == true |
| 116 | mbike.save |
| 117 | mbike.unsaved_changes?.should == false |
| 118 | |
| 119 | end |
| 120 | |
| 121 | |
| 105 | 122 | it "should only save relations that need to be saved" do |
| 106 | 123 | |
| 107 | 124 | response = HTTPResponse.new(@apartment_building_content) |
| toggle raw diff |
--- a/spec/persistable/unsaved_changes.rb
+++ b/spec/persistable/unsaved_changes.rb
@@ -102,6 +102,23 @@ describe CouchObject::Persistable, "for loading objects:" do
end
+ it "should not have unsaved_changes after having been saved" do
+ response = HTTPResponse.new(@content_motor_bike)
+
+ @db.should_receive(:get).with("123BAC").and_return(response)
+ @db.should_receive(:put).and_return(@document_response)
+
+ mbike = OtherMotorBike.get_by_id("123BAC", "foo")
+
+ mbike.unsaved_changes?.should == false
+ mbike.wheels = 3
+ mbike.unsaved_changes?.should == true
+ mbike.save
+ mbike.unsaved_changes?.should == false
+
+ end
+
+
it "should only save relations that need to be saved" do
response = HTTPResponse.new(@apartment_building_content) |