From: David Kamholz Date: Tue, 31 Jan 2006 23:58:52 +0000 (+0000) Subject: re-fix many_to_many and add tests X-Git-Tag: v0.05005~65 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0f6ac8bbd6575d44fcb30cefcb5e387bb09d8b79;p=dbsrgits%2FDBIx-Class.git re-fix many_to_many and add tests --- diff --git a/lib/DBIx/Class/Relationship/ManyToMany.pm b/lib/DBIx/Class/Relationship/ManyToMany.pm index f01cc10..0ecc915 100644 --- a/lib/DBIx/Class/Relationship/ManyToMany.pm +++ b/lib/DBIx/Class/Relationship/ManyToMany.pm @@ -11,8 +11,9 @@ sub many_to_many { no strict 'refs'; no warnings 'redefine'; *{"${class}::${meth}"} = sub { - my ($self,$cond,$attrs) = @_; - $self->search_related($rel)->search_related($f_rel, $cond, { %$rel_attrs, %{$attrs||{}} }); + my $self = shift; + my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}; + $self->search_related($rel)->search_related($f_rel, @_ > 0 ? @_ : undef, { %$rel_attrs, %$attrs }); }; } } diff --git a/t/lib/DBICTest/Schema/BasicRels.pm b/t/lib/DBICTest/Schema/BasicRels.pm index 551d8b9..7a6f520 100644 --- a/t/lib/DBICTest/Schema/BasicRels.pm +++ b/t/lib/DBICTest/Schema/BasicRels.pm @@ -100,5 +100,6 @@ DBICTest::Schema::CD_to_Producer->add_relationship( # now the Helpers DBICTest::Schema::CD->many_to_many( 'producers', 'cd_to_producer', 'producer'); +DBICTest::Schema::CD->many_to_many( 'producers_sorted', 'cd_to_producer', 'producer', { order_by => 'producer.name' }); 1; diff --git a/t/lib/DBICTest/Schema/HelperRels.pm b/t/lib/DBICTest/Schema/HelperRels.pm index 4c7ea1d..ff47640 100644 --- a/t/lib/DBICTest/Schema/HelperRels.pm +++ b/t/lib/DBICTest/Schema/HelperRels.pm @@ -54,5 +54,6 @@ DBICTest::Schema::ArtistUndirectedMap->has_many( # now the Helpers DBICTest::Schema::CD->many_to_many( 'producers', 'cd_to_producer', 'producer'); +DBICTest::Schema::CD->many_to_many( 'producers_sorted', 'cd_to_producer', 'producer', { order_by => 'producer.name' }); 1; diff --git a/t/lib/DBICTest/Setup.pm b/t/lib/DBICTest/Setup.pm index 9b64890..6c52140 100755 --- a/t/lib/DBICTest/Setup.pm +++ b/t/lib/DBICTest/Setup.pm @@ -103,11 +103,15 @@ $schema->populate('ArtistUndirectedMap', [ $schema->populate('Producer', [ [ qw/producerid name/ ], [ 1, 'Matt S Trout' ], + [ 2, 'Bob The Builder' ], + [ 3, 'Fred The Phenotype' ], ]); $schema->populate('CD_to_Producer', [ [ qw/cd producer/ ], [ 1, 1 ], + [ 1, 2 ], + [ 1, 3 ], ]); 1; diff --git a/t/run/06relationship.tl b/t/run/06relationship.tl index d29998e..65a2419 100644 --- a/t/run/06relationship.tl +++ b/t/run/06relationship.tl @@ -3,7 +3,7 @@ my $schema = shift; use strict; use warnings; -plan tests => 18; +plan tests => 20; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -111,6 +111,8 @@ like($@, qr/join condition/, 'failed when creating a rel without join condition, $cd = $schema->resultset("CD")->find(1); my @producers = $cd->producers(); is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' ); +is( $cd->producers_sorted->next->name, 'Bob The Builder', 'sorted many_to_many ok' ); +is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', 'sorted many_to_many with search condition ok' ); # test undirected many-to-many relationship (e.g. "related artists") my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;