Commit ddc72356377743b1e3e75b43e2177c5ed698ed9f

Added convenience method +get_with_smart_save+ for loading an instance with smart save activated although the class itself does not have it defined

Commit diff

README.txt

 
678678
679679 instance_WITHOUT_smart_save_activated_2 = NotSoSmartClass.get("dong")
680680
681The same as in the example above can be achieved with the convenience
682method +get_with_smart_save+ which takes the same parameters as +get_by_id+.
683The 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

lib/couch_object/persistable.rb

 
119119 #
120120 alias get get_by_id
121121
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
122141
123142 #
124143 # Loads all document from a given view from the database
280280 instance_variable_set("@belongs_to", belongs_to) \
281281 if belongs_to
282282
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
299284
300285 # Returns the new object
301286 new_object_from_couch
308308 id.nil? || revision.nil?
309309 end
310310
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
311337 #
312338 # Saves the object to the db_uri supplied, or if not set, to the
313339 # location the object has previously been saved to.
toggle raw diff

spec/persistable/unsaved_changes.rb

 
7373
7474 end
7575
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
7692 it "should save an object that does not unsaved changes if it hasn't activated smart_save" do
7793 response = HTTPResponse.new(@normal_content_motor_bike)
7894
toggle raw diff