Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision a341d959

Von Moritz Bunkus vor fast 12 Jahren hinzugefügt

  • ID a341d959ab798890746200efd0d5983c66f67db7
  • Vorgänger f12b3596
  • Nachfolger 64ed7be3

ActsAsList: mit remove_from_list() entfernte Items auf position = -1 setzen

position ist in allen Tabellen NOT NULL deklariert. Nur in der einen,
die ich zum Testen genutzt hatte, nicht. Das habe ich nun
nachgeholt. Jetzt wird -1 als magischer "not in list"-Wert benutzt.

Unterschiede anzeigen:

SL/DB/Helper/ActsAsList.pm
41 41
  my $worker = sub {
42 42
    remove_position($self);
43 43

  
44
    # Set to NULL manually because $self->update_attributes() would
44
    # Set to -1 manually because $self->update_attributes() would
45 45
    # trigger the before_save() hook from this very plugin assigning a
46 46
    # number at the end of the list again.
47 47
    my $table           = $self->meta->table;
......
49 49
    my $primary_key_col = ($self->meta->primary_key)[0];
50 50
    my $sql             = <<SQL;
51 51
      UPDATE ${table}
52
      SET ${column} = NULL
52
      SET ${column} = -1
53 53
      WHERE ${primary_key_col} = ?
54 54
SQL
55 55
    $self->db->dbh->do($sql, undef, $self->$primary_key_col);
......
165 165
sub set_position {
166 166
  my ($self) = @_;
167 167
  my $column = column_name($self);
168
  my $value  = $self->$column;
168 169

  
169
  return 1 if defined $self->$column;
170
  return 1 if defined($value) && ($value != -1);
170 171

  
171 172
  my $table               = $self->meta->table;
172 173
  my ($group_by, @values) = get_group_by_where($self);
173
  my $where               = $group_by ? " WHERE ${group_by}" : '';
174
  $group_by               = " AND ${group_by}" if $group_by;
174 175
  my $sql                 = <<SQL;
175 176
    SELECT COALESCE(MAX(${column}), 0)
176 177
    FROM ${table}
177
    ${where}
178
    WHERE (${column} <> -1)
179
      ${group_by}
178 180
SQL
179 181

  
180 182
  my $max_position = $self->db->dbh->selectrow_arrayref($sql, undef, @values)->[0];
......
188 190
  my $column = column_name($self);
189 191

  
190 192
  $self->load;
191
  return 1 unless defined $self->$column;
193
  my $value = $self->$column;
194
  return 1 unless defined($value) && ($value != -1);
192 195

  
193 196
  my $table               = $self->meta->table;
194
  my $value               = $self->$column;
195 197
  my ($group_by, @values) = get_group_by_where($self);
196 198
  $group_by               = ' AND ' . $group_by if $group_by;
197 199
  my $sql                 = <<SQL;
......
208 210

  
209 211
sub do_move {
210 212
  my ($self, $direction) = @_;
211
  my $column             = column_name($self);
212 213

  
213 214
  croak "Object has not been saved yet" unless $self->id;
214
  croak "No position set yet"           unless defined $self->$column;
215

  
216
  my $column       = column_name($self);
217
  my $old_position = $self->$column;
218
  croak "No position set yet" unless defined($old_position) && ($old_position != -1);
215 219

  
216 220
  my $table                                        = $self->meta->table;
217
  my $old_position                                 = $self->$column;
218 221
  my ($comp_sel, $comp_upd, $min_max, $plus_minus) = $direction eq 'up' ? ('<', '>=', 'MAX', '+') : ('>', '<=', 'MIN', '-');
219 222
  my ($group_by, @values)                          = get_group_by_where($self);
220 223
  $group_by                                        = ' AND ' . $group_by if $group_by;
221 224
  my $sql                                          = <<SQL;
222 225
    SELECT ${min_max}(${column})
223 226
    FROM ${table}
224
    WHERE (${column} ${comp_sel} ?)
227
    WHERE (${column} <>          -1)
228
      AND (${column} ${comp_sel} ?)
225 229
      ${group_by}
226 230
SQL
227 231

  
......
365 369

  
366 370
=item C<remove_from_list>
367 371

  
368
Sets this items positional column to C<undef>, saves it and moves all
372
Sets this items positional column to C<-1>, saves it and moves all
369 373
following items up by 1.
370 374

  
371 375
=item C<reorder_list @ids>
t/db_helper/acts_as_list.t
139 139
# Aus Liste entfernen
140 140
reset_state();
141 141
get_item(3)->remove_from_list;
142
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 ];
142
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 ];
143 143

  
144 144
reset_state();
145 145
get_item(4)->remove_from_list;
146
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 ];
146
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 ];
147 147

  
148 148
reset_state();
149 149
get_item(5)->remove_from_list;
150
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 ];
150
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 ];
151 151

  
152 152
reset_state();
153 153
get_item(8)->remove_from_list;
154
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 ];
154
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 ];
155 155

  
156 156
reset_state();
157 157
$item = get_item(3); $item->remove_from_list; $item->delete;

Auch abrufbar als: Unified diff