]> wagnertech.de Git - mfinanz.git/commitdiff
ActsAsList: add_to_list() auch aufrufen können, wenn 'position' noch gesetzt ist
authorMoritz Bunkus <m.bunkus@linet-services.de>
Thu, 7 Mar 2013 12:09:51 +0000 (13:09 +0100)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Thu, 7 Mar 2013 12:12:31 +0000 (13:12 +0100)
SL/DB/Helper/ActsAsList.pm
t/db_helper/acts_as_list.t

index 49f39a9bcfeba55540ad06c7b6833fb576fb3b53..8f3333ef41513fdf4b1820ac21d9d5ec8856531a 100644 (file)
@@ -65,6 +65,10 @@ sub add_to_list {
 
   croak "Invalid parameter 'position'" unless ($params{position} || '') =~ m/^ (?: before | after | first | last ) $/x;
 
 
   croak "Invalid parameter 'position'" unless ($params{position} || '') =~ m/^ (?: before | after | first | last ) $/x;
 
+  my $column = column_name($self);
+
+  $self->remove_from_list if ($self->$column // -1) != -1;
+
   if ($params{position} eq 'last') {
     set_position($self);
     $self->save;
   if ($params{position} eq 'last') {
     set_position($self);
     $self->save;
@@ -73,7 +77,6 @@ sub add_to_list {
 
   my $table               = $self->meta->table;
   my $primary_key_col     = ($self->meta->primary_key)[0];
 
   my $table               = $self->meta->table;
   my $primary_key_col     = ($self->meta->primary_key)[0];
-  my $column              = column_name($self);
   my ($group_by, @values) = get_group_by_where($self);
   $group_by               = " AND ${group_by}" if $group_by;
   my $new_position;
   my ($group_by, @values) = get_group_by_where($self);
   $group_by               = " AND ${group_by}" if $group_by;
   my $new_position;
@@ -409,6 +412,10 @@ one. The current item will then be inserted either before or after the
 referenced item by shifting all the appropriate item positions up by
 one.
 
 referenced item by shifting all the appropriate item positions up by
 one.
 
+If C<$self>'s positional column is already set when this function is
+called then L</remove_from_list> will be called first before anything
+else is done.
+
 After this function C<$self>'s positional column has been set and
 saved to the database.
 
 After this function C<$self>'s positional column has been set and
 saved to the database.
 
index 0d47bb2537492a413e1ae0ef528c3e1b66c251f3..2e7b7086473ec4e53af30b7b607d79ed2d9ac344 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More tests => 50;
+use Test::More tests => 51;
 use Test::Exception;
 
 use strict;
 use Test::Exception;
 
 use strict;
@@ -206,6 +206,10 @@ reset_state();
 $item = get_item(8); $item->remove_from_list; $item->parent_id(3); $item->add_to_list(position => 'first');
 test_positions "add_to_list position 'first' in empty", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 3, 1 ];
 
 $item = get_item(8); $item->remove_from_list; $item->parent_id(3); $item->add_to_list(position => 'first');
 test_positions "add_to_list position 'first' in empty", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 3, 1 ];
 
+reset_state();
+$item = get_item(5); $item->add_to_list(position => 'after', reference => 3);
+test_positions "add_to_list without prior remove_from_list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 5, 1, 2 ], [ 4, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
+
 reset_state();
 $item = get_item(4);
 is($item->get_next_in_list->id,                           5, 'Next of 4 is 5');
 reset_state();
 $item = get_item(4);
 is($item->get_next_in_list->id,                           5, 'Next of 4 is 5');