Hello all,
I've attached a patch against DBIx::Class::Ordered that adds
a move_to_group method. The Ordered functionality is a god-send
for me in my current project, but it's even more useful when a
row can be moved between groups easily. I think there was a
request for this on the list early this month too.
There are some basic tests included in the patch file, let me
know if something more involved is required. Ditto for patch
format.
Best,
Brian Kirkbride
--- lib/DBIx/Class/Ordered.pm 2006-06-29 16:27:23.000000000 -0500
+++ lib/DBIx/Class/Ordered.pm 2006-07-01 16:03:21.415470344 -0500
@@ -294,6 +294,49 @@
return 1;
}
+=head2 move_to_group
+
+ $item->move_to_group( $group, $position );
+
+Moves the object to the specified position of the specified
+group, or to the end of the group if $position is undef.
+1 is returned on success, and 0 is returned if the object is
+already at the specified position of the specified group.
+
+=cut
+
+sub move_to_group {
+ my( $self, $to_group, $to_position ) = @_;
+ my $position_column = $self->position_column;
+ my $grouping_column = $self->grouping_column;
+
+ return 0 if ( ! defined $to_group );
+ return 0 if ( defined $to_position and $to_position < 1 );
+ return 0 if ( $self->$grouping_column==$to_group and
$self->$position_column==$to_position );
+
+ # Move to end of current group and adjust siblings
+ $self->move_last;
+
+ $self->$grouping_column($to_group);
+ my $new_group_count =
$self->result_source->resultset->search({$self->_grouping_clause()})->count();
+ if (!defined $to_position or $to_position > $new_group_count) {
+ $self->update({ $position_column => $new_group_count + 1});
+ }
+ else {
+ my @between = ($to_position, $new_group_count);
+
+ my $rs = $self->result_source->resultset->search({
+ $position_column => { -between => [ @between ] },
+ $self->_grouping_clause(),
+ });
+ $rs->update({ $position_column => \"$position_column + 1" });
+
+ $self->update({ $position_column => $to_position });
+ }
+
+ return 1;
+}
+
=head2 insert
Overrides the DBIC insert() method by providing a default
--- t/87ordered.t 2006-06-29 16:27:23.000000000 -0500
+++ t/87ordered.t 2006-07-01 16:18:05.671861588 -0500
@@ -8,7 +8,7 @@
my $schema = DBICTest->init_schema();
-plan tests => 321;
+plan tests => 630;
my $employees = $schema->resultset('Employee');
$employees->delete();
@@ -37,6 +37,21 @@
hammer_rs( $group_employees );
}
+my $group_3_employees = $employees->search({group_id=>3});
+my $to_group = 1;
+my $to_pos = undef;
+while (my $employee = $group_3_employees->next) {
+ $employee->move_to_group($to_group, $to_pos);
+ $to_pos++;
+ $to_group = $to_group==1 ? 2 : 1;
+}
+foreach my $group_id (1..3) {
+ my $group_employees = $employees->search({group_id=>$group_id});
+ $group_employees->all();
+ ok( check_rs($group_employees), "group positions after move_to_group" );
+ hammer_rs( $group_employees );
+}
+
sub hammer_rs {
my $rs = shift;
my $employee;
_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive: http://www.mail-archive.com/[email protected]/