sub previous_sibling {
my( $self ) = @_;
my $position_column = $self->position_column;
+ my $position = $self->get_column( $position_column );
+ return 0 if ($position==1);
return ($self->search(
{
- $position_column => { '<' => $self->get_column($position_column) },
+ $position_column => $position - 1,
$self->_collection_clause(),
- },
- { rows=>1, order_by => $position_column.' DESC' },
+ }
)->all())[0];
}
sub next_sibling {
my( $self ) = @_;
my $position_column = $self->position_column;
+ my $position = $self->get_column( $position_column );
+ my $count = $self->result_source->resultset->search({$self->_collection_clause()})->count();
+ return 0 if ($position==$count);
return ($self->result_source->resultset->search(
{
- $position_column => { '>' => $self->get_column($position_column) },
+ $position_column => $position + 1,
$self->_collection_clause(),
},
- { rows=>1, order_by => $position_column },
)->all())[0];
}
sub run_tests {
- plan tests => 96;
+ plan tests => 56;
my $schema = shift;
my $employees = $schema->resultset('Employee::Positioned');
+ $employees->delete();
- if ($employees->result_class->can('position_column')) {
-
- $employees->delete();
- foreach (1..5) {
- $employees->create({ name=>'temp' });
- }
+ foreach (1..5) {
+ $employees->create({ name=>'temp' });
+ }
+ $employees = $employees->search(undef,{order_by=>'position'});
+ ok( check_positions($employees), "$class: intial positions" );
- $employees = $employees->search(undef,{order_by=>'position'});
- ok( check_positions($employees), "$class: intial positions" );
+ my $employee;
- my $employee;
+ foreach my $position (1..$employees->count()) {
- foreach my $position (1..$employees->count()) {
+ $employee = $employees->find({ position=>$position });
+ $employee->move_previous();
+ ok( check_positions($employees), "$class: move_previous( $position )" );
- $employee = $employees->find({ position=>$position });
- $employee->move_previous();
- ok( check_positions($employees), "$class: move_previous( $position )" );
+ $employee = $employees->find({ position=>$position });
+ $employee->move_next();
+ ok( check_positions($employees), "$class: move_next( $position )" );
- $employee = $employees->find({ position=>$position });
- $employee->move_next();
- ok( check_positions($employees), "$class: move_next( $position )" );
+ $employee = $employees->find({ position=>$position });
+ $employee->move_first();
+ ok( check_positions($employees), "$class: move_first( $position )" );
- $employee = $employees->find({ position=>$position });
- $employee->move_first();
- ok( check_positions($employees), "$class: move_first( $position )" );
+ $employee = $employees->find({ position=>$position });
+ $employee->move_last();
+ ok( check_positions($employees), "$class: move_last( $position )" );
+ foreach my $to_position (1..$employees->count()) {
$employee = $employees->find({ position=>$position });
- $employee->move_last();
- ok( check_positions($employees), "$class: move_last( $position )" );
-
- foreach my $to_position (1..$employees->count()) {
- $employee = $employees->find({ position=>$position });
- $employee->move_to($to_position);
- ok( check_positions($employees), "$class: move_to( $position => $to_position )" );
- }
+ $employee->move_to($to_position);
+ ok( check_positions($employees), "$class: move_to( $position => $to_position )" );
+ }
+ $employee = $employees->find({ position=>$position });
+ if ($position==1) {
+ ok( !$employee->previous_sibling(), 'no previous sibling' );
+ ok( $employee->next_sibling(), 'next sibling' );
+ }
+ elsif ($position==$employees->count()) {
+ ok( $employee->previous_sibling(), 'previous sibling' );
+ ok( !$employee->next_sibling(), 'no next sibling' );
}
+ else {
+ ok( $employee->previous_sibling(), 'previous sibling' );
+ ok( $employee->next_sibling(), 'next sibling' );
+ }
+
}
}