| |   |
| 678 | 678 | |
| 679 | 679 | instance_WITHOUT_smart_save_activated_2 = NotSoSmartClass.get("dong") |
| 680 | 680 | |
| 681 | The same as in the example above can be achieved with the convenience |
| 682 | method +get_with_smart_save+ which takes the same parameters as +get_by_id+. |
| 683 | The same example could therefor also be written as: |
| 684 | |
| 685 | class NotSoSmartClass |
| 686 | include CouchObject::Persistable |
| 687 | end |
| 688 | |
| 689 | instance_WITHOUT_smart_save_activated = NotSoSmartClass.get("foo") |
| 690 | |
| 691 | instance_WITH_smart_save_activated = NotSoSmartClass.get_with_smart_save("bar") |
| 692 | |
| 693 | instance_WITHOUT_smart_save_activated_2 = NotSoSmartClass.get("dong") |
| 694 | |
| toggle raw diff |
--- a/README.txt
+++ b/README.txt
@@ -678,3 +678,17 @@ or it can be toggled manually:
instance_WITHOUT_smart_save_activated_2 = NotSoSmartClass.get("dong")
+The same as in the example above can be achieved with the convenience
+method +get_with_smart_save+ which takes the same parameters as +get_by_id+.
+The same example could therefor also be written as:
+
+ class NotSoSmartClass
+ include CouchObject::Persistable
+ end
+
+ instance_WITHOUT_smart_save_activated = NotSoSmartClass.get("foo")
+
+ instance_WITH_smart_save_activated = NotSoSmartClass.get_with_smart_save("bar")
+
+ instance_WITHOUT_smart_save_activated_2 = NotSoSmartClass.get("dong")
+ |
| |   |
| 119 | 119 | # |
| 120 | 120 | alias get get_by_id |
| 121 | 121 | |
| 122 | # |
| 123 | # Takes, returns and raises the same things as +get_by_id+ |
| 124 | # |
| 125 | # Creates a new object that is forced into smart save mode |
| 126 | # although the class it is stemming from might not have smart |
| 127 | # saving enabled. |
| 128 | # |
| 129 | def get_with_smart_save(id, db_uri = self.location) |
| 130 | |
| 131 | new_object = self.get_by_id(id, db_uri) |
| 132 | |
| 133 | # Force it into smart save mode |
| 134 | new_object.couch_force_smart_save |
| 135 | |
| 136 | # Initialize the original state. |
| 137 | new_object.couch_set_initial_state |
| 138 | |
| 139 | new_object |
| 140 | end |
| 122 | 141 | |
| 123 | 142 | # |
| 124 | 143 | # Loads all document from a given view from the database |
| … | … | |
| 280 | 280 | instance_variable_set("@belongs_to", belongs_to) \ |
| 281 | 281 | if belongs_to |
| 282 | 282 | |
| 283 | | # For the unsaved_changes? instance method to work, we have to |
| 284 | | # supply a snapshot of what the fresh object looked like. |
| 285 | | # BUT ONLY if the user has activated the smart_save option |
| 286 | | if new_object_from_couch.use_smart_save |
| 287 | | |
| 288 | | new_object_from_couch. |
| 289 | | instance_variable_set("@couch_initial_load", true) |
| 290 | | |
| 291 | | new_object_from_couch. |
| 292 | | instance_variable_set("@couch_object_original_state", \ |
| 293 | | new_object_from_couch.to_json) |
| 294 | | |
| 295 | | new_object_from_couch. |
| 296 | | instance_variable_set("@couch_initial_load", false) |
| 297 | | |
| 298 | | end |
| 283 | new_object_from_couch.couch_set_initial_state |
| 299 | 284 | |
| 300 | 285 | # Returns the new object |
| 301 | 286 | new_object_from_couch |
| … | … | |
| 308 | 308 | id.nil? || revision.nil? |
| 309 | 309 | end |
| 310 | 310 | |
| 311 | # |
| 312 | # Stores the initial value of the instance to a variable |
| 313 | # for later reference by the +unsaved_changes?+ method |
| 314 | # |
| 315 | def couch_set_initial_state |
| 316 | # For the unsaved_changes? instance method to work, we have to |
| 317 | # supply a snapshot of what the fresh object looked like. |
| 318 | # BUT ONLY if the user has activated the smart_save option |
| 319 | if use_smart_save |
| 320 | |
| 321 | @couch_initial_load = true |
| 322 | @couch_object_original_state = to_json |
| 323 | @couch_initial_load = false |
| 324 | |
| 325 | end |
| 326 | end |
| 327 | |
| 328 | # |
| 329 | # Forces the instance object into smart save mode |
| 330 | # |
| 331 | def couch_force_smart_save |
| 332 | def self.use_smart_save |
| 333 | true |
| 334 | end |
| 335 | end |
| 336 | |
| 311 | 337 | # |
| 312 | 338 | # Saves the object to the db_uri supplied, or if not set, to the |
| 313 | 339 | # location the object has previously been saved to. |
| toggle raw diff |
--- a/lib/couch_object/persistable.rb
+++ b/lib/couch_object/persistable.rb
@@ -119,6 +119,25 @@ module CouchObject
#
alias get get_by_id
+ #
+ # Takes, returns and raises the same things as +get_by_id+
+ #
+ # Creates a new object that is forced into smart save mode
+ # although the class it is stemming from might not have smart
+ # saving enabled.
+ #
+ def get_with_smart_save(id, db_uri = self.location)
+
+ new_object = self.get_by_id(id, db_uri)
+
+ # Force it into smart save mode
+ new_object.couch_force_smart_save
+
+ # Initialize the original state.
+ new_object.couch_set_initial_state
+
+ new_object
+ end
#
# Loads all document from a given view from the database
@@ -261,22 +280,7 @@ module CouchObject
instance_variable_set("@belongs_to", belongs_to) \
if belongs_to
- # For the unsaved_changes? instance method to work, we have to
- # supply a snapshot of what the fresh object looked like.
- # BUT ONLY if the user has activated the smart_save option
- if new_object_from_couch.use_smart_save
-
- new_object_from_couch.
- instance_variable_set("@couch_initial_load", true)
-
- new_object_from_couch.
- instance_variable_set("@couch_object_original_state", \
- new_object_from_couch.to_json)
-
- new_object_from_couch.
- instance_variable_set("@couch_initial_load", false)
-
- end
+ new_object_from_couch.couch_set_initial_state
# Returns the new object
new_object_from_couch
@@ -304,6 +308,32 @@ module CouchObject
id.nil? || revision.nil?
end
+ #
+ # Stores the initial value of the instance to a variable
+ # for later reference by the +unsaved_changes?+ method
+ #
+ def couch_set_initial_state
+ # For the unsaved_changes? instance method to work, we have to
+ # supply a snapshot of what the fresh object looked like.
+ # BUT ONLY if the user has activated the smart_save option
+ if use_smart_save
+
+ @couch_initial_load = true
+ @couch_object_original_state = to_json
+ @couch_initial_load = false
+
+ end
+ end
+
+ #
+ # Forces the instance object into smart save mode
+ #
+ def couch_force_smart_save
+ def self.use_smart_save
+ true
+ end
+ end
+
#
# Saves the object to the db_uri supplied, or if not set, to the
# location the object has previously been saved to. |
| |   |
| 73 | 73 | |
| 74 | 74 | end |
| 75 | 75 | |
| 76 | it "should know if an object has unsaved for an object where the instance is forced into smart save mode" do |
| 77 | response = HTTPResponse.new(@normal_content_motor_bike) |
| 78 | |
| 79 | @db.should_receive(:get).with("123BAC").and_return(response) |
| 80 | |
| 81 | mbike = MotorBike.get_with_smart_save("123BAC", "foo") |
| 82 | mbike.id.should == "123BAC" |
| 83 | mbike.use_smart_save.should == true |
| 84 | |
| 85 | mbike.unsaved_changes?.should == false |
| 86 | mbike.wheels = 3 |
| 87 | mbike.unsaved_changes?.should == true |
| 88 | |
| 89 | end |
| 90 | |
| 91 | |
| 76 | 92 | it "should save an object that does not unsaved changes if it hasn't activated smart_save" do |
| 77 | 93 | response = HTTPResponse.new(@normal_content_motor_bike) |
| 78 | 94 | |
| toggle raw diff |
--- a/spec/persistable/unsaved_changes.rb
+++ b/spec/persistable/unsaved_changes.rb
@@ -73,6 +73,22 @@ describe CouchObject::Persistable, "for loading objects:" do
end
+ it "should know if an object has unsaved for an object where the instance is forced into smart save mode" do
+ response = HTTPResponse.new(@normal_content_motor_bike)
+
+ @db.should_receive(:get).with("123BAC").and_return(response)
+
+ mbike = MotorBike.get_with_smart_save("123BAC", "foo")
+ mbike.id.should == "123BAC"
+ mbike.use_smart_save.should == true
+
+ mbike.unsaved_changes?.should == false
+ mbike.wheels = 3
+ mbike.unsaved_changes?.should == true
+
+ end
+
+
it "should save an object that does not unsaved changes if it hasn't activated smart_save" do
response = HTTPResponse.new(@normal_content_motor_bike)
|