public
Rubygem
Fork of sam/dm-core
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/anthonyw/dm-core.git
Fix so that Resource#reload and #reload_attribute only run when the 
resource is not new.
anthonyw (author)
Wed Jun 18 12:30:41 -0700 2008
commit  ef75fb7c06e9bc1c75259d30a105bbfb57679622
tree    b5b07f3a426a2380fe671d06acd1b41deecbbab2
parent  08e9f8b62cc0912ea976e71efe77ffafe040c30c
...
408
409
410
411
412
 
 
 
 
 
413
414
415
...
424
425
426
427
 
 
 
 
428
429
430
...
408
409
410
 
 
411
412
413
414
415
416
417
418
...
427
428
429
 
430
431
432
433
434
435
436
0
@@ -408,8 +408,11 @@ module DataMapper
0
     # --
0
     # @public
0
     def reload
0
- reload_attributes(*loaded_attributes)
0
- (parent_associations + child_associations).each { |association| association.reload }
0
+ unless new_record?
0
+ reload_attributes(*loaded_attributes)
0
+ (parent_associations + child_associations).each { |association| association.reload }
0
+ end
0
+
0
       self
0
     end
0
 
0
@@ -424,7 +427,10 @@ module DataMapper
0
     # --
0
     # @public
0
     def reload_attributes(*attributes)
0
- collection.reload(:fields => attributes)
0
+ unless attributes.empty? || new_record?
0
+ collection.reload(:fields => attributes)
0
+ end
0
+
0
       self
0
     end
0
 
...
370
371
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
374
375
...
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
0
@@ -370,6 +370,70 @@ describe "DataMapper::Resource" do
0
     earth.orbit_period.should == 365.26
0
   end
0
 
0
+ describe "#reload_attributes" do
0
+ it 'should call collection.reload if not a new record' do
0
+ planet = Planet.new(:name => 'Omicron Persei VIII')
0
+ planet.stub!(:new_record?).and_return(false)
0
+
0
+ collection = mock('collection')
0
+ collection.should_receive(:reload).with(:fields => [:name]).once
0
+
0
+ planet.stub!(:collection).and_return(collection)
0
+ planet.reload_attributes(:name)
0
+ end
0
+
0
+ it 'should not call collection.reload if no attributes are provided to reload' do
0
+ planet = Planet.new(:name => 'Omicron Persei VIII')
0
+ planet.stub!(:new_record?).and_return(false)
0
+
0
+ collection = mock('collection')
0
+ collection.should_not_receive(:reload)
0
+
0
+ planet.stub!(:collection).and_return(collection)
0
+ planet.reload_attributes
0
+ end
0
+
0
+ it 'should not call collection.reload if the record is new' do
0
+ planet = Planet.new(:name => 'Omicron Persei VIII')
0
+ planet.should_not_receive(:collection)
0
+ planet.reload_attributes(:name)
0
+ end
0
+ end
0
+
0
+ describe '#reload' do
0
+ it 'should call #reload_attributes with the currently loaded attributes' do
0
+ planet = Planet.new(:name => 'Omicron Persei VIII', :age => 1)
0
+ planet.stub!(:new_record?).and_return(false)
0
+
0
+ planet.should_receive(:reload_attributes).with(:name, :age).once
0
+
0
+ planet.reload
0
+ end
0
+
0
+ it 'should call #reload on the parent and child associations' do
0
+ planet = Planet.new(:name => 'Omicron Persei VIII', :age => 1)
0
+ planet.stub!(:new_record?).and_return(false)
0
+
0
+ child_association = mock('child assoc')
0
+ child_association.should_receive(:reload).once.and_return(true)
0
+
0
+ parent_association = mock('parent assoc')
0
+ parent_association.should_receive(:reload).once.and_return(true)
0
+
0
+ planet.stub!(:child_associations).and_return([child_association])
0
+ planet.stub!(:parent_associations).and_return([parent_association])
0
+ planet.stub!(:reload_attributes).and_return(planet)
0
+
0
+ planet.reload
0
+ end
0
+
0
+ it 'should not do anything if the record is new' do
0
+ planet = Planet.new(:name => 'Omicron Persei VIII', :age => 1)
0
+ planet.should_not_receive(:reload_attributes)
0
+ planet.reload
0
+ end
0
+ end
0
+
0
   describe "anonymity" do
0
     it "should require a default storage name and accept a block" do
0
       pluto = DataMapper::Resource.new("planets") do

Comments

    No one has commented yet.