Fixes to Tree::AdjacencyList, and working tests.
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Positioned.pm
index b9370b2..5266f9d 100644 (file)
@@ -17,6 +17,7 @@ Create a table for your positionable data.
     name TEXT NOT NULL,
     position INTEGER NOT NULL
   );
+  # Optional: group_id INTEGER NOT NULL
 
 In your Schema or DB class add Positioned to the top 
 of the component list.
@@ -28,6 +29,7 @@ each row.
 
   package My::Employee;
   __PACKAGE__->position_column('position');
+  __PACKAGE__->collection_column('group_id'); # optional
 
 Thats it, now you can change the position of your objects.
 
@@ -35,6 +37,8 @@ Thats it, now you can change the position of your objects.
   use My::Employee;
   
   my $employee = My::Employee->create({ name=>'Matt S. Trout' });
+  # If using collection_column:
+  my $employee = My::Employee->create({ name=>'Matt S. Trout', group_id=>1 });
   
   my $rs = $employee->siblings();
   my @siblings = $employee->siblings();
@@ -100,7 +104,7 @@ excluding the one you called it on.
 sub siblings {
     my( $self ) = @_;
     my $position_column = $self->position_column;
-    my $rs = $self->search(
+    my $rs = $self->result_source->resultset->search(
         {
             $position_column => { '!=' => $self->get_column($position_column) },
             $self->_collection_clause(),
@@ -115,15 +119,19 @@ sub siblings {
 
   my $sibling = $employee->first_sibling();
 
-Returns the first sibling object.
+Returns the first sibling object, or 0 if the first sibling 
+is this sibliing.
 
 =cut
 
 sub first_sibling {
     my( $self ) = @_;
-    return ($self->search(
-        { $self->_collection_clause() },
-        { rows=>1, order_by => $self->position_column },
+    return 0 if ($self->get_column($self->position_column())==1);
+    return ($self->result_source->resultset->search(
+        {
+            $self->position_column => 1,
+            $self->_collection_clause(),
+        },
     )->all())[0];
 }
 
@@ -131,15 +139,20 @@ sub first_sibling {
 
   my $sibling = $employee->last_sibling();
 
-Return the last sibling.
+Return the last sibling, or 0 if the last sibling is this 
+sibling.
 
 =cut
 
 sub last_sibling {
     my( $self ) = @_;
-    return ($self->search(
-        { $self->_collection_clause() },
-        { rows=>1, order_by => $self->position_column.' DESC' },
+    my $count = $self->result_source->resultset->search({$self->_collection_clause()})->count();
+    return 0 if ($self->get_column($self->position_column())==$count);
+    return ($self->result_source->resultset->search(
+        {
+            $self->position_column => $count,
+            $self->_collection_clause(),
+        },
     )->all())[0];
 }
 
@@ -295,7 +308,7 @@ sub insert {
     my $position_column = $self->position_column;
     $self->set_column( $position_column => $self->result_source->resultset->search( {$self->_collection_clause()} )->count()+1 ) 
         if (!$self->get_column($position_column));
-    $self->next::method( @_ );
+    return $self->next::method( @_ );
 }
 
 =head2 delete
@@ -309,7 +322,7 @@ integrity of the positions.
 sub delete {
     my $self = shift;
     $self->move_last;
-    $self->next::method( @_ );
+    return $self->next::method( @_ );
 }
 
 =head1 PRIVATE METHODS
@@ -326,7 +339,7 @@ defined then this will return an empty list.
 =cut
 
 sub _collection_clause {
-    my $self = shift;
+    my( $self ) = @_;
     if ($self->collection_column()) {
         return ( $self->collection_column() => $self->get_column($self->collection_column()) );
     }