[Bast-commits] r4636 - DBIx-Class-Tree-NestedSet/lib/DBIx/Class/Tree
rafl at dev.catalyst.perl.org
rafl at dev.catalyst.perl.org
Fri Jul 25 01:44:27 BST 2008
Author: rafl
Date: 2008-07-25 01:44:26 +0100 (Fri, 25 Jul 2008)
New Revision: 4636
Modified:
DBIx-Class-Tree-NestedSet/lib/DBIx/Class/Tree/NestedSet.pm
Log:
Make the relation names configurable.
Also don't modify the users col data in create_related.
Modified: DBIx-Class-Tree-NestedSet/lib/DBIx/Class/Tree/NestedSet.pm
===================================================================
--- DBIx-Class-Tree-NestedSet/lib/DBIx/Class/Tree/NestedSet.pm 2008-07-25 00:44:19 UTC (rev 4635)
+++ DBIx-Class-Tree-NestedSet/lib/DBIx/Class/Tree/NestedSet.pm 2008-07-25 00:44:26 UTC (rev 4636)
@@ -11,6 +11,14 @@
my ($class, $args) = @_;
if (defined $args) {
+ $args = {
+ root_rel => 'root',
+ nodes_rel => 'nodes',
+ children_rel => 'children',
+ parents_rel => 'parents',
+ %{ $args },
+ };
+
my ($root, $left, $right) = map {
$args->{"${_}_column"}
} qw/root left right/;
@@ -19,18 +27,18 @@
my %join_cond = ( "foreign.$root" => "self.$root" );
$class->belongs_to(
- root => $class,
+ $args->{root_rel} => $class,
\%join_cond,
{ where => \"me.$left = 1", },
);
$class->has_many(
- nodes => $class,
+ $args->{nodes_rel} => $class,
\%join_cond,
);
$class->has_many(
- children => $class,
+ $args->{children_rel} => $class,
\%join_cond,
{ where => \"me.$left > parent.$left AND me.$right < parent.$right",
order_by => "me.$left",
@@ -38,7 +46,7 @@
);
$class->has_many(
- parents => $class,
+ $args->{parents_rel} => $class,
{ %join_cond, },
{ where => \"child.$left > me.$left AND child.$right < me.$right",
order_by => "me.$right",
@@ -87,7 +95,8 @@
sub create_related {
my ($self, $rel, $col_data) = @_;
- if ($rel eq 'children') {
+ my %col_data = %{ $col_data };
+ if ($rel eq $self->tree_columns->{children_rel}) {
my ($root, $left, $right) = map {
$self->tree_columns->{"${_}_column"}
} qw/root left right/;
@@ -100,10 +109,10 @@
$right => \"CASE WHEN $right >= $p_rgt THEN $right + 2 ELSE $right END",
});
- @$col_data{$root, $left, $right} = ($self->$root, $p_rgt, $p_rgt + 1);
+ @col_data{$root, $left, $right} = ($self->$root, $p_rgt, $p_rgt + 1);
}
- return $self->next::method($rel => $col_data);
+ return $self->next::method($rel => \%col_data);
}
sub search_related {
@@ -111,10 +120,10 @@
my $pk = ($self->result_source->primary_columns)[0];
$cond ||= {};
- if ($rel eq 'children') {
+ if ($rel eq $self->tree_columns->{children_rel}) {
$cond->{"parent.$pk"} = $self->$pk,
}
- elsif ($rel eq 'parents') {
+ elsif ($rel eq $self->tree_columns->{parents_rel}) {
$cond->{"child.$pk"} = $self->$pk,
}
More information about the Bast-commits
mailing list