public
Rubygem
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
Sensible default :remote_name for ManyToMany.

Applied patch by lobo_tuerto to set you change the :class_name of a
many to many relationship without needing to set the :remote_name.

This closes out #560.
Thu Sep 11 10:13:55 -0700 2008
commit  8ec812f88bf54c456913f9156c71a09d134abc81
tree    9acbfa69553785ab0028737703f3d0abd1dc6608
parent  3938f0f9755c417ef3560839ff15227f701ccfa6
...
42
43
44
45
 
46
47
48
...
42
43
44
 
45
46
47
48
0
@@ -42,7 +42,7 @@ module DataMapper
0
         opts[:child_model]              ||= opts.delete(:class_name)  || Extlib::Inflection.classify(name)
0
         opts[:parent_model]             =   model
0
         opts[:repository_name]          =   repository_name
0
-        opts[:remote_relationship_name] ||= opts.delete(:remote_name) || name
0
+        opts[:remote_relationship_name] ||= opts.delete(:remote_name) || Extlib::Inflection.tableize(opts[:child_model])
0
         opts[:parent_key]               =   opts[:parent_key]
0
         opts[:child_key]                =   opts[:child_key]
0
         opts[:mutable]                  =   true
...
294
295
296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
440
441
442
443
444
445
446
447
448
449
0
@@ -294,4 +294,156 @@ describe DataMapper::Associations::ManyToMany::Proxy do
0
     end
0
   end
0
 
0
+  describe "with renamed associations" do
0
+    before :all do
0
+      class Singer
0
+        include DataMapper::Resource
0
+
0
+        def self.default_repository_name; ADAPTER end
0
+
0
+        property :id, Serial
0
+        property :name, String
0
+
0
+        has n, :tunes, :through => Resource, :class_name => 'Song'
0
+      end
0
+
0
+      class Song
0
+        include DataMapper::Resource
0
+
0
+        def self.default_repository_name; ADAPTER end
0
+
0
+        property :id, Serial
0
+        property :title, String
0
+
0
+        has n, :performers, :through => Resource, :class_name => 'Singer'
0
+      end
0
+    end
0
+
0
+    before do
0
+      [ Singer, Song, SingerSong ].each { |k| k.auto_migrate! }
0
+
0
+      song_1 = Song.create(:title => "Dubliners")
0
+      song_2 = Song.create(:title => "Portrait of the Artist as a Young Man")
0
+      song_3 = Song.create(:title => "Ulysses")
0
+
0
+      singer_1 = Singer.create(:name => "Jon Doe")
0
+      singer_2 = Singer.create(:name => "Jane Doe")
0
+
0
+      SingerSong.create(:song => song_1, :singer => singer_1)
0
+      SingerSong.create(:song => song_2, :singer => singer_1)
0
+      SingerSong.create(:song => song_1, :singer => singer_2)
0
+
0
+      @parent      = song_3
0
+      @association = @parent.performers
0
+      @other       = [ singer_1 ]
0
+    end
0
+
0
+    it "should provide #replace" do
0
+      @association.should respond_to(:replace)
0
+    end
0
+
0
+    it "should correctly link records" do
0
+      Song.get(1).should have(2).performers
0
+      Song.get(2).should have(1).performers
0
+      Song.get(3).should have(0).performers
0
+      Singer.get(1).should have(2).tunes
0
+      Singer.get(2).should have(1).tunes
0
+    end
0
+
0
+    it "should be able to have associated objects manually added" do
0
+      song = Song.get(3)
0
+      song.should have(0).performers
0
+
0
+      be = SingerSong.new(:song_id => song.id, :singer_id => 2)
0
+      song.singer_songs << be
0
+      song.save
0
+
0
+      song.reload.should have(1).performers
0
+    end
0
+
0
+    it "should automatically added necessary through class" do
0
+      song = Song.get(3)
0
+      song.should have(0).performers
0
+
0
+      song.performers << Singer.get(1)
0
+      song.performers << Singer.new(:name => "Jimmy John")
0
+      song.save
0
+
0
+      song.reload.should have(2).performers
0
+    end
0
+
0
+    it "should react correctly to a new record" do
0
+      song = Song.new(:title => "Finnegan's Wake")
0
+      singer = Singer.get(2)
0
+      song.should have(0).performers
0
+      singer.should have(1).tunes
0
+
0
+      song.performers << singer
0
+      song.save
0
+
0
+      song.reload.should have(1).performers
0
+      singer.reload.should have(2).tunes
0
+    end
0
+
0
+    it "should be able to delete intermediate model" do
0
+      song = Song.get(1)
0
+      song.should have(2).singer_songs
0
+      song.should have(2).performers
0
+
0
+      be = SingerSong.get(1,1)
0
+      song.singer_songs.delete(be)
0
+      song.save
0
+
0
+      song.reload
0
+      song.should have(1).singer_songs
0
+      song.should have(1).performers
0
+    end
0
+
0
+    it "should be clearable" do
0
+      repository(ADAPTER) do
0
+        song = Song.get(2)
0
+        song.should have(1).singer_songs
0
+        song.should have(1).performers
0
+
0
+        song.performers.clear
0
+        song.save
0
+
0
+        song.reload
0
+        song.should have(0).singer_songs
0
+        song.should have(0).performers
0
+      end
0
+      repository(ADAPTER) do
0
+        Song.get(2).should have(0).performers
0
+      end
0
+    end
0
+
0
+    it "should be able to delete one object" do
0
+      song = Song.get(1)
0
+      song.should have(2).singer_songs
0
+      song.should have(2).performers
0
+
0
+      editor = song.performers.first
0
+      song.performers.delete(editor)
0
+      song.save
0
+
0
+      song.reload
0
+      song.should have(1).singer_songs
0
+      song.should have(1).performers
0
+      editor.reload.tunes.should_not include(song)
0
+    end
0
+
0
+    it "should be destroyable" do
0
+      pending "cannot destroy a collection yet" do
0
+        song = Song.get(2)
0
+        song.should have(1).performers
0
+
0
+        song.performers.destroy
0
+        song.save
0
+
0
+        song.reload
0
+        song.should have(0).performers
0
+      end
0
+    end
0
+  end
0
+
0
 end

Comments