From: Peter Rabbitson Date: Sun, 5 Jul 2015 14:27:01 +0000 (+0200) Subject: Rewrite incredibly overcomplicated add_X m2m helper X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=86ed11c90f4995d43cd636c5e5a021a5e0d5d816;p=dbsrgits%2FDBIx-Class.git Rewrite incredibly overcomplicated add_X m2m helper Goal was no functional changes, but one never knows - original code was... rather peculiar --- diff --git a/lib/DBIx/Class/Relationship/ManyToMany.pm b/lib/DBIx/Class/Relationship/ManyToMany.pm index 36ec18d..37c8461 100644 --- a/lib/DBIx/Class/Relationship/ManyToMany.pm +++ b/lib/DBIx/Class/Relationship/ManyToMany.pm @@ -80,32 +80,29 @@ EOW my $add_meth_name = join '::', $class, $add_meth; *$add_meth_name = subname $add_meth_name, sub { my $self = shift; - @_ > 0 or $self->throw_exception( + @_ or $self->throw_exception( "${add_meth} needs an object or hashref" ); - my $source = $self->result_source; - my $schema = $source->schema; - my $rel_source_name = $source->relationship_info($rel)->{source}; - my $rel_source = $schema->resultset($rel_source_name)->result_source; - my $f_rel_source_name = $rel_source->relationship_info($f_rel)->{source}; - my $f_rel_rs = $schema->resultset($f_rel_source_name)->search({}, $rel_attrs||{}); - - my $obj; - if (ref $_[0]) { - if (ref $_[0] eq 'HASH') { - $obj = $f_rel_rs->find_or_create($_[0]); - } else { - $obj = $_[0]; - } - } else { - $obj = $f_rel_rs->find_or_create({@_}); - } - my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}; - my $link = $self->search_related($rel)->new_result($link_vals); - $link->set_from_related($f_rel, $obj); + my $link = $self->search_related_rs($rel)->new_result( + ( @_ > 1 && ref $_[-1] eq 'HASH' ) + ? pop + : {} + ); + + my $far_obj = defined blessed $_[0] + ? $_[0] + : $self->result_source + ->related_source( $rel ) + ->related_source( $f_rel ) + ->resultset->search_rs( {}, $rel_attrs||{} ) + ->find_or_create( ref $_[0] eq 'HASH' ? $_[0] : {@_} ) + ; + + $link->set_from_related($f_rel, $far_obj); $link->insert(); - return $obj; + + return $far_obj; }; my $set_meth_name = join '::', $class, $set_meth;