Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 6e25f5eb

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID 6e25f5ebaf82b5d17221e65363c0606853ed26e4
  • Vorgänger 37ffd321
  • Nachfolger 0cdbf7b0

ActsAsList: get_next_in_list() und get_previous_in_list()

Unterschiede anzeigen:

SL/DB/Helper/ActsAsList.pm
3 3
use strict;
4 4

  
5 5
use parent qw(Exporter);
6
our @EXPORT = qw(move_position_up move_position_down add_to_list remove_from_list reorder_list configure_acts_as_list);
6
our @EXPORT = qw(move_position_up move_position_down add_to_list remove_from_list reorder_list configure_acts_as_list
7
                 get_previous_in_list get_next_in_list);
7 8

  
8 9
use Carp;
9 10

  
......
112 113
  return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
113 114
}
114 115

  
116
sub get_next_in_list {
117
  my ($self) = @_;
118
  return get_previous_or_next($self, 'next');
119
}
120

  
121
sub get_previous_in_list {
122
  my ($self) = @_;
123
  return get_previous_or_next($self, 'previous');
124
}
125

  
115 126
sub reorder_list {
116 127
  my ($class_or_self, @ids) = @_;
117 128

  
......
245 256
  $self->update_attributes($column => $new_position);
246 257
}
247 258

  
259
sub get_previous_or_next {
260
  my ($self, $direction)  = @_;
261

  
262
  my $asc_desc            = $direction eq 'next' ? 'ASC' : 'DESC';
263
  my $comparator          = $direction eq 'next' ? '>'   : '<';
264
  my $table               = $self->meta->table;
265
  my $column              = column_name($self);
266
  my $primary_key_col     = ($self->meta->primary_key)[0];
267
  my ($group_by, @values) = get_group_by_where($self);
268
  $group_by               = " AND ${group_by}" if $group_by;
269
  my $sql                 = <<SQL;
270
    SELECT ${primary_key_col}
271
    FROM ${table}
272
    WHERE (${column} ${comparator} ?)
273
      ${group_by}
274
    ORDER BY ${column} ${asc_desc}
275
    LIMIT 1
276
SQL
277

  
278
  my $id = ($self->db->dbh->selectrow_arrayref($sql, undef, $self->$column, @values) || [])->[0];
279

  
280
  return $id ? $self->_get_manager_class->find_by(id => $id) : undef;
281
}
282

  
248 283
sub column_name {
249 284
  my ($self) = @_;
250 285
  my $column = get_spec(ref $self, 'column_name');
......
372 407
Sets this items positional column to C<-1>, saves it and moves all
373 408
following items up by 1.
374 409

  
410
=item C<get_previous_in_list>
411

  
412
Fetches the previous item in the list. Returns C<undef> if C<$self> is
413
already the first one.
414

  
415
=item C<get_next_in_list>
416

  
417
Fetches the next item in the list. Returns C<undef> if C<$self> is
418
already the last one.
419

  
375 420
=item C<reorder_list @ids>
376 421

  
377 422
Re-orders the objects given in C<@ids> by their position in C<@ids> by
t/db_helper/acts_as_list.t
1
use Test::More tests => 44;
1
use Test::More tests => 50;
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

  
209
reset_state();
210
$item = get_item(4);
211
is($item->get_next_in_list->id,                           5, 'Next of 4 is 5');
212
is($item->get_previous_in_list->id,                       3, 'Previous of 4 is 5');
213
is($item->get_next_in_list->get_previous_in_list->id,     4, 'Previous of Next of 4 is 4');
214
is($item->get_previous_in_list->get_next_in_list->id,     4, 'Next of Previous of 4 is 4');
215
is($item->get_next_in_list->get_next_in_list,         undef, 'Next of Next of 4 is undef');
216
is($item->get_previous_in_list->get_previous_in_list, undef, 'Previous of Previous of 4 is undef');
210 217

  
211 218
# Parametervalidierung
212 219
throws_ok { new_item()->move_position_up   } qr/not.*been.*saved/i, 'move up not saved yet';

Auch abrufbar als: Unified diff