1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
package DBIx::Class::Tree::AdjacencyList::Ordered;
# vim: ts=8:sw=4:sts=4:et
use strict;
use warnings;
use base qw( DBIx::Class );
use Carp qw( croak );
__PACKAGE__->load_components(qw(
Ordered
Tree::AdjacencyList
));
=head1 NAME
DBIx::Class::Tree::AdjacencyList::Ordered - Glue DBIx::Class::Ordered and DBIx::Class::Tree::AdjacencyList together.
=head1 SYNOPSIS
Create a table for your tree data.
CREATE TABLE items (
item_id INTEGER PRIMARY KEY AUTOINCREMENT,
parent_id INTEGER NOT NULL DEFAULT 0,
position INTEGER NOT NULL,
name TEXT NOT NULL
);
In your Schema or DB class add Tree::AdjacencyList::Ordered
to the front of the component list.
__PACKAGE__->load_components(qw( Tree::AdjacencyList::Ordered ... ));
Specify the column that contains the parent ID and position of each row.
package My::Employee;
__PACKAGE__->position_column('position');
__PACKAGE__->parent_column('parent_id');
This module provides a few extra methods beyond what
L<DBIx::Class::Ordered> and L<DBIx::Class::Tree::AdjacencyList>
already provide.
my $parent = $item->parent();
$item->parent( $parent_obj );
$item->parent( $parent_id );
my $children_rs = $item->children();
my @children = $item->children();
$parent->append_child( $child );
$parent->prepend_child( $child );
$this->attach_before( $that );
$this->attach_after( $that );
=head1 DESCRIPTION
This module provides methods for working with adjacency lists and ordered
rows. All of the methods that L<DBIx::Class::Ordered> and
L<DBIx::Class::Tree::AdjacencyList> provide are available with this module.
=head1 METHODS
=head2 parent_column
__PACKAGE__->parent_column('parent_id');
Works the same as AdjacencyList's parent_column() method, but it
declares the children() has many relationship to be ordered by the
position column.
=cut
sub parent_column {
my $class = shift;
my $position_col = $class->position_column() || croak('You must call position_column() before calling parent_column()');
if (@_) {
$class->grouping_column( @_ );
$class->next::method( @_ );
$class->relationship_info('children')->{attrs}->{order_by} = $position_col;
return 1;
}
return $class->grouping_column;
}
=head2 parent
my $parent = $item->parent();
$item->parent( $parent_obj );
$item->parent( $parent_id );
This method overrides AdjacencyList's parent() method but
modifies it so that the object is moved to the last position,
then the parent is changed, and then it is moved to the last
position of the new list, thus maintaining the intergrity of
the ordered lists.
=cut
sub parent {
my $self = shift;
if (@_) {
my $new_parent = shift;
my $parent_col = $self->_parent_column();
if (ref($new_parent)) {
$new_parent = $new_parent->id() || croak('Parent object does not have an ID');;
}
return 0 if ($new_parent == ($self->get_column($parent_col)||0));
$self->move_last;
$self->set_column( $parent_col => $new_parent );
$self->set_column(
$self->position_column() =>
$self->result_source->resultset->search(
{$self->_grouping_clause()}
)->count() + 1
);
$self->update();
return 1;
}
return $self->_parent();
}
=head2 children
my $children_rs = $item->children();
my @children = $item->children();
This method works just like it does in the
DBIx::Class::Tree::AdjacencyList module except it
orders the children by there position.
=head2 append_child
$parent->append_child( $child );
Sets the child to have the specified parent and moves the
child to the last position.
=cut
sub append_child {
my( $self, $child ) = @_;
$child->parent( $self );
}
=head2 prepend_child
$parent->prepend_child( $child );
Sets the child to have the specified parent and moves the
child to the first position.
=cut
sub prepend_child {
my( $self, $child ) = @_;
$child->parent( $self );
$child->move_first();
}
=head2 attach_before
$this->attach_before( $that );
Attaches the object at the position just before the
calling object's position.
=cut
sub attach_before {
my( $self, $sibling ) = @_;
$sibling->parent( $self->parent() );
$sibling->move_to( $self->get_column($self->position_column()) );
}
=head2 attach_after
$this->attach_after( $that );
Attaches the object at the position just after the
calling object's position.
=cut
sub attach_after {
my( $self, $sibling ) = @_;
$sibling->parent( $self->parent() );
$sibling->move_to( $self->get_column($self->position_column()) + 1 );
}
1;
__END__
=head1 INHERITED METHODS
=head2 DBIx::Class::Ordered
=over 4
=item *
L<siblings|DBIx::Class::Ordered/siblings>
=item *
L<first_sibling|DBIx::Class::Ordered/first_sibling>
=item *
L<last_sibling|DBIx::Class::Ordered/last_sibling>
=item *
L<previous_sibling|DBIx::Class::Ordered/previous_sibling>
=item *
L<next_sibling|DBIx::Class::Ordered/next_sibling>
=item *
L<move_previous|DBIx::Class::Ordered/move_previous>
=item *
L<move_next|DBIx::Class::Ordered/move_next>
=item *
L<move_first|DBIx::Class::Ordered/move_first>
=item *
L<move_last|DBIx::Class::Ordered/move_last>
=item *
L<move_to|DBIx::Class::Ordered/move_to>
=item *
L<insert|DBIx::Class::Ordered/insert>
=item *
L<delete|DBIx::Class::Ordered/delete>
=back
=head2 DBIx::Class::Tree::AdjacencyList
=over 4
=item *
L<parent_column|DBIx::Class::Tree::AdjacencyList/parent_column>
=item *
L<parent|DBIx::Class::Tree::AdjacencyList/parent>
=item *
L<attach_child|DBIx::Class::Tree::AdjacencyList/attach_child>
=item *
L<siblings|DBIx::Class::Tree::AdjacencyList/siblings>
=item *
L<attach_sibling|DBIx::Class::Tree::AdjacencyList/attach_sibling>
=back
=head2 DBIx::Class
=over 4
=item *
L<mk_classdata|DBIx::Class/mk_classdata>
=item *
L<component_base_class|DBIx::Class/component_base_class>
=back
=head2 DBIx::Class::Componentised
=over 4
=item *
L<inject_base|DBIx::Class::Componentised/inject_base>
=item *
L<load_components|DBIx::Class::Componentised/load_components>
=item *
L<load_own_components|DBIx::Class::Componentised/load_own_components>
=back
=head2 Class::Data::Accessor
=over 4
=item *
L<mk_classaccessor|Class::Data::Accessor/mk_classaccessor>
=back
=head1 AUTHOR
Aran Clary Deltac <bluefeet@cpan.org>
=head1 LICENSE
You may distribute this code under the same terms as Perl itself.
|