my $worker = sub {
remove_position($self);
- # Set to NULL manually because $self->update_attributes() would
+ # Set to -1 manually because $self->update_attributes() would
# trigger the before_save() hook from this very plugin assigning a
# number at the end of the list again.
my $table = $self->meta->table;
my $primary_key_col = ($self->meta->primary_key)[0];
my $sql = <<SQL;
UPDATE ${table}
- SET ${column} = NULL
+ SET ${column} = -1
WHERE ${primary_key_col} = ?
SQL
$self->db->dbh->do($sql, undef, $self->$primary_key_col);
sub set_position {
my ($self) = @_;
my $column = column_name($self);
+ my $value = $self->$column;
- return 1 if defined $self->$column;
+ return 1 if defined($value) && ($value != -1);
my $table = $self->meta->table;
my ($group_by, @values) = get_group_by_where($self);
- my $where = $group_by ? " WHERE ${group_by}" : '';
+ $group_by = " AND ${group_by}" if $group_by;
my $sql = <<SQL;
SELECT COALESCE(MAX(${column}), 0)
FROM ${table}
- ${where}
+ WHERE (${column} <> -1)
+ ${group_by}
SQL
my $max_position = $self->db->dbh->selectrow_arrayref($sql, undef, @values)->[0];
my $column = column_name($self);
$self->load;
- return 1 unless defined $self->$column;
+ my $value = $self->$column;
+ return 1 unless defined($value) && ($value != -1);
my $table = $self->meta->table;
- my $value = $self->$column;
my ($group_by, @values) = get_group_by_where($self);
$group_by = ' AND ' . $group_by if $group_by;
my $sql = <<SQL;
sub do_move {
my ($self, $direction) = @_;
- my $column = column_name($self);
croak "Object has not been saved yet" unless $self->id;
- croak "No position set yet" unless defined $self->$column;
+
+ my $column = column_name($self);
+ my $old_position = $self->$column;
+ croak "No position set yet" unless defined($old_position) && ($old_position != -1);
my $table = $self->meta->table;
- my $old_position = $self->$column;
my ($comp_sel, $comp_upd, $min_max, $plus_minus) = $direction eq 'up' ? ('<', '>=', 'MAX', '+') : ('>', '<=', 'MIN', '-');
my ($group_by, @values) = get_group_by_where($self);
$group_by = ' AND ' . $group_by if $group_by;
my $sql = <<SQL;
SELECT ${min_max}(${column})
FROM ${table}
- WHERE (${column} ${comp_sel} ?)
+ WHERE (${column} <> -1)
+ AND (${column} ${comp_sel} ?)
${group_by}
SQL
=item C<remove_from_list>
-Sets this items positional column to C<undef>, saves it and moves all
+Sets this items positional column to C<-1>, saves it and moves all
following items up by 1.
=item C<reorder_list @ids>
# Aus Liste entfernen
reset_state();
get_item(3)->remove_from_list;
-test_positions "remove_from_list on top", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, undef ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
+test_positions "remove_from_list on top", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, -1 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
reset_state();
get_item(4)->remove_from_list;
-test_positions "remove_from_list on middle", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, undef ], [ 5, 1, 2 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
+test_positions "remove_from_list on middle", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, -1 ], [ 5, 1, 2 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
reset_state();
get_item(5)->remove_from_list;
-test_positions "remove_from_list on bottom", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, undef ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
+test_positions "remove_from_list on bottom", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, -1 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
reset_state();
get_item(8)->remove_from_list;
-test_positions "remove_from_list on only item in list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, undef ];
+test_positions "remove_from_list on only item in list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, -1 ];
reset_state();
$item = get_item(3); $item->remove_from_list; $item->delete;