Revision 3dc3b056
Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt
SL/DB/Helper/ActsAsList.pm | ||
---|---|---|
65 | 65 |
|
66 | 66 |
croak "Invalid parameter 'position'" unless ($params{position} || '') =~ m/^ (?: before | after | first | last ) $/x; |
67 | 67 |
|
68 |
my $column = column_name($self); |
|
69 |
|
|
70 |
$self->remove_from_list if ($self->$column // -1) != -1; |
|
71 |
|
|
68 | 72 |
if ($params{position} eq 'last') { |
69 | 73 |
set_position($self); |
70 | 74 |
$self->save; |
... | ... | |
73 | 77 |
|
74 | 78 |
my $table = $self->meta->table; |
75 | 79 |
my $primary_key_col = ($self->meta->primary_key)[0]; |
76 |
my $column = column_name($self); |
|
77 | 80 |
my ($group_by, @values) = get_group_by_where($self); |
78 | 81 |
$group_by = " AND ${group_by}" if $group_by; |
79 | 82 |
my $new_position; |
... | ... | |
409 | 412 |
referenced item by shifting all the appropriate item positions up by |
410 | 413 |
one. |
411 | 414 |
|
415 |
If C<$self>'s positional column is already set when this function is |
|
416 |
called then L</remove_from_list> will be called first before anything |
|
417 |
else is done. |
|
418 |
|
|
412 | 419 |
After this function C<$self>'s positional column has been set and |
413 | 420 |
saved to the database. |
414 | 421 |
|
t/db_helper/acts_as_list.t | ||
---|---|---|
1 |
use Test::More tests => 50;
|
|
1 |
use Test::More tests => 51;
|
|
2 | 2 |
use Test::Exception; |
3 | 3 |
|
4 | 4 |
use strict; |
... | ... | |
206 | 206 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(3); $item->add_to_list(position => 'first'); |
207 | 207 |
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 ]; |
208 | 208 |
|
209 |
reset_state(); |
|
210 |
$item = get_item(5); $item->add_to_list(position => 'after', reference => 3); |
|
211 |
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 ]; |
|
212 |
|
|
209 | 213 |
reset_state(); |
210 | 214 |
$item = get_item(4); |
211 | 215 |
is($item->get_next_in_list->id, 5, 'Next of 4 is 5'); |
Auch abrufbar als: Unified diff
ActsAsList: add_to_list() auch aufrufen können, wenn 'position' noch gesetzt ist