]> wagnertech.de Git - kivitendo-erp.git/commitdiff
LinkedRecords: bidi Verhalten den docs angepasst und direction Parameter optional...
authorSven Schöling <s.schoeling@linet-services.de>
Wed, 28 May 2014 10:39:14 +0000 (12:39 +0200)
committerSven Schöling <s.schoeling@linet-services.de>
Wed, 28 May 2014 10:51:59 +0000 (12:51 +0200)
SL/DB/Helper/LinkedRecords.pm
t/db_helper/record_links.t

index d072a55c7546cdf6c9038e7e7ff77bab19c18d5c..33248bfaee9ec9d41f11da6a5b7d468cc59db6c9 100644 (file)
@@ -30,7 +30,19 @@ sub _linked_records_implementation {
   my $self     = shift;
   my %params   = @_;
 
   my $self     = shift;
   my %params   = @_;
 
-  my $wanted   = $params{direction} || croak("Missing parameter `direction'");
+  my $wanted   = $params{direction};
+
+  if (!$wanted) {
+    if ($params{to} && $params{from}) {
+      $wanted = 'both';
+    } elsif ($params{to}) {
+      $wanted = 'to';
+    } elsif ($params{from}) {
+      $wanted = 'from';
+    } else {
+      $wanted = 'both';
+    }
+  }
 
   if ($wanted eq 'both') {
     my $both       = delete($params{both});
 
   if ($wanted eq 'both') {
     my $both       = delete($params{both});
@@ -132,7 +144,7 @@ sub link_to_record {
                );
 
     my $link = SL::DB::Manager::RecordLink->find_by(and => [ %data ]);
                );
 
     my $link = SL::DB::Manager::RecordLink->find_by(and => [ %data ]);
-    push @links, $link ? $link : SL::DB::RecordLink->new(%data)->save unless $link;
+    push @links, $link ? $link : SL::DB::RecordLink->new(%data)->save;
   }
 
   return wantarray ? @links : $links[0];
   }
 
   return wantarray ? @links : $links[0];
@@ -240,32 +252,26 @@ SYNOPSIS
   use SL::DB::Helper::LinkedRecords;
 
   # later in consumer code
   use SL::DB::Helper::LinkedRecords;
 
   # later in consumer code
-  # retrieve all links
-  my @linked_objects = $order->linked_records(
-    direction => 'both',
-  );
+  # retrieve all links in both directions
+  my @linked_objects = $order->linked_records;
 
   # only links to Invoices
   my @linked_objects = $order->linked_records(
 
   # only links to Invoices
   my @linked_objects = $order->linked_records(
-    direction => 'to',
     to        => 'Invoice',
   );
 
   # more than one target
   my @linked_objects = $order->linked_records(
     to        => 'Invoice',
   );
 
   # more than one target
   my @linked_objects = $order->linked_records(
-    direction => 'to',
     to        => [ 'Invoice', 'Order' ],
   );
 
   # more than one direction
   my @linked_objects = $order->linked_records(
     to        => [ 'Invoice', 'Order' ],
   );
 
   # more than one direction
   my @linked_objects = $order->linked_records(
-    direction => 'both',
     both      => 'Invoice',
   );
 
   # more than one direction and different targets
   my @linked_objects = $order->linked_records(
     both      => 'Invoice',
   );
 
   # more than one direction and different targets
   my @linked_objects = $order->linked_records(
-    direction => 'both',
     to        => 'Invoice',
     from      => 'Order',
   );
     to        => 'Invoice',
     from      => 'Order',
   );
@@ -277,6 +283,13 @@ SYNOPSIS
     via       => 'DeliveryOrder',
   );
 
     via       => 'DeliveryOrder',
   );
 
+  # limit direction when further params contain additional keys
+  my %params = (to => 'Invoice', from => 'Order');
+  my @linked_objects = $order->linked_records(
+    direction => 'to',
+    %params,
+  );
+
   # add a new link
   $order->link_to_record($invoice);
   $order->link_to_record($purchase_order, bidirectional => 1);
   # add a new link
   $order->link_to_record($invoice);
   $order->link_to_record($purchase_order, bidirectional => 1);
@@ -288,11 +301,13 @@ SYNOPSIS
 
 =item C<linked_records %params>
 
 
 =item C<linked_records %params>
 
-Retrieves records linked from or to C<$self> via the table C<record_links>. The
-mandatory parameter C<direction> (either C<from>, C<to> or C<both>) determines
-whether the function retrieves records that link to C<$self> (for C<direction>
-= C<to>) or that are linked from C<$self> (for C<direction> = C<from>). For
-C<direction = both> all records linked from or to C<$self> are returned.
+Retrieves records linked from or to C<$self> via the table C<record_links>.
+
+The optional parameter C<direction> (either C<from>, C<to> or C<both>)
+determines whether the function retrieves records that link to C<$self> (for
+C<direction> = C<to>) or that are linked from C<$self> (for C<direction> =
+C<from>). For C<direction = both> all records linked from or to C<$self> are
+returned.
 
 The optional parameter C<from> or C<to> (same as C<direction>) contains the
 package names of Rose models for table limitation (the prefix C<SL::DB::> is
 
 The optional parameter C<from> or C<to> (same as C<direction>) contains the
 package names of Rose models for table limitation (the prefix C<SL::DB::> is
@@ -300,6 +315,10 @@ optional). It can be a single model name as a single scalar or multiple model
 names in an array reference in which case all links matching any of the model
 names will be returned.
 
 names in an array reference in which case all links matching any of the model
 names will be returned.
 
+If no parameter C<direction> is given, but any of C<to>, C<from> or C<both>,
+then C<direction> is infered accordingly. If neither are given, C<direction> is
+set to C<both>.
+
 The optional parameter C<via> can be used to retrieve all documents that may
 have intermediate documents inbetween. It is an array reference of Rose package
 names for the models that may be intermediate link targets. One example is
 The optional parameter C<via> can be used to retrieve all documents that may
 have intermediate documents inbetween. It is an array reference of Rose package
 names for the models that may be intermediate link targets. One example is
index 856277644619a41220f0a6be87ba3bce63fc0752..851f9cb5ce10a739f6b733b3edda831367412616 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More;
+use Test::More tests => 43;
 
 use strict;
 
 
 use strict;
 
@@ -113,6 +113,9 @@ is $link->to_table, 'ar', 'to_table';
 is $link->to_id, $i->id, 'to_id';
 
 # retrieve link
 is $link->to_id, $i->id, 'to_id';
 
 # retrieve link
+$links = $o1->linked_records;
+is $links->[0]->id, $i->id, 'simple retrieve';
+
 $links = $o1->linked_records(direction => 'to', to => 'Invoice');
 is $links->[0]->id, $i->id, 'direct retrieve 1';
 
 $links = $o1->linked_records(direction => 'to', to => 'Invoice');
 is $links->[0]->id, $i->id, 'direct retrieve 1';
 
@@ -186,6 +189,11 @@ is @$links, 1, 'double link is only added once 2';
 $links = $o2->linked_records(direction => 'both');
 is @$links, 2, 'links without from/to get all';
 
 $links = $o2->linked_records(direction => 'both');
 is @$links, 2, 'links without from/to get all';
 
+# doc states you can limit with direction when giving excess params
+$links = $d->linked_records(direction => 'to', to => 'Invoice', from => 'Order');
+is $links->[0]->id, $i->id, 'direction to limit params  1';
+is @$links, 1, 'direction to limit params 2';
+
 # doc says there will be special values set... lets see
 $links = $o1->linked_records(direction => 'to', to => 'Order');
 is $links->[0]->{_record_link_direction}, 'to',  '_record_link_direction to';
 # doc says there will be special values set... lets see
 $links = $o1->linked_records(direction => 'to', to => 'Order');
 is $links->[0]->{_record_link_direction}, 'to',  '_record_link_direction to';
@@ -196,10 +204,8 @@ is $links->[0]->{_record_link_direction}, 'from',  '_record_link_direction from'
 is $links->[0]->{_record_link}->to_id, $o1->id,  '_record_link from';
 
 # check if bidi returns an array of links
 is $links->[0]->{_record_link}->to_id, $o1->id,  '_record_link from';
 
 # check if bidi returns an array of links
-{ local $TODO = 'does not work as advertised';
 my @links = $d->link_to_record($o2, bidirectional => 1);
 is @links, 2, 'bidi returns array of links in array context';
 my @links = $d->link_to_record($o2, bidirectional => 1);
 is @links, 2, 'bidi returns array of links in array context';
-}
 
 #  via
 $links = $o2->linked_records(direction => 'to', to => 'Invoice', via => 'DeliveryOrder');
 
 #  via
 $links = $o2->linked_records(direction => 'to', to => 'Invoice', via => 'DeliveryOrder');
@@ -272,6 +278,4 @@ is_deeply $sorted, [$o2, $i, $o1, $d], 'sorting by transdate';
 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('date', 0, @records);
 is_deeply $sorted, [$d, $o1, $i, $o2], 'sorting by transdate desc';
 
 $sorted = SL::DB::Helper::LinkedRecords->sort_linked_records('date', 0, @records);
 is_deeply $sorted, [$d, $o1, $i, $o2], 'sorting by transdate desc';
 
-done_testing();
-
 1;
 1;