Permalink
Browse files

use DBIx::Class::MaterializedPath \o/

  • Loading branch information...
1 parent c3f4fec commit 6a8f7d8a4a5964ace7b22792547b4cdd0c32ec7b @frioux committed Jun 29, 2012
Showing with 20 additions and 88 deletions.
  1. +20 −88 lib/DU/Schema/Result/Ingredient.pm
@@ -1,8 +1,6 @@
package DU::Schema::Result::Ingredient;
-use DU::Schema::Candy -components => [
- 'Helper::Row::OnColumnChange'
-];
+use DU::Schema::Candy;
primary_column id => {
data_type => 'int',
@@ -32,94 +30,28 @@ column description => {
is_nullable => 1,
};
-belongs_to direct_kind_of => '::Ingredient', 'kind_of_id', { join_type => 'left' };
-has_many direct_kinds => '::Ingredient', 'kind_of_id';
-has_many inventory_items => '::InventoryItem', 'ingredient_id';
-has_many links_to_drink_ingredients => '::Drink_Ingredient', 'ingredient_id';
-
-sub _set_materialized_path {
- my $self = shift;
-
- if ($self->kind_of_id) {
- $self->discard_changes;
-
- $self->_materialized_path(
- $self->direct_kind_of->_materialized_path . q(/) . $self->id
- );
- } else {
- $self->_materialized_path($self->id)
+__PACKAGE__->load_components('MaterializedPath');
+
+sub materialized_path_columns {
+ return {
+ kind_of => {
+ parent_column => 'kind_of_id',
+ parent_fk_column => 'id',
+ materialized_path_column => 'materialized_path',
+ parent_relationship => 'direct_kind_of',
+ children_relationship => 'direct_kinds',
+ full_path => 'kind_of',
+ reverse_full_path => 'kinds',
+ include_self_in_path => 1,
+ include_self_in_reverse_path => 1,
+ },
}
- $self->update
}
-sub _fix_for_updates {
- my ( $self, $old, $new ) = @_;
-
- $self->_set_materialized_path;
-
- $_->_fix_for_updates for $self->direct_kinds->all
-}
-
-sub insert {
- my $self = shift;
-
- my $ret = $self->next::method;
-
- $ret->_set_materialized_path;
- return $ret;
-}
-
-after_column_change kind_of_id => {
- txn_wrap => 1,
- method => '_fix_for_updates',
-} for qw(kind_of_id materialized_path);
-
-has_many
- kind_of => '::Ingredient',
- sub {
- my $args = shift;
-
- my $path_separator = q(/);
- my $rest = "$path_separator%";
-
- my $me = {
- "$args->{self_alias}.id" => { -ident => "$args->{foreign_alias}.id" }
- };
- return ([{
- "$args->{self_alias}.materialized_path" => {
- -like => \["$args->{foreign_alias}.materialized_path" . ' || ' . '?',
- [ {} => $rest ]
- ],
- }
- },
- $me
- ],
- $args->{self_rowobj} && {
- "$args->{foreign_alias}.id" => {
- -in => [ split qr(/), $args->{self_rowobj}->_materialized_path ]
- },
- });
- };
-
-has_many
- kinds => '::Ingredient',
- sub {
- my $args = shift;
-
- my $path_separator = q(/);
- my $rest = "$path_separator%";
-
- my $me = {
- "$args->{self_alias}.id" => { -ident => "$args->{foreign_alias}.id" }
- };
- return [{
- "$args->{foreign_alias}.materialized_path" => {
- -like => \["$args->{self_alias}.materialized_path" . ' || ' . '?',
- [ {} => $rest ]
- ],
- }
- }, $me ]
- };
+belongs_to direct_kind_of => '::Ingredient', 'kind_of_id', { join_type => 'left' };
+has_many direct_kinds => '::Ingredient', 'kind_of_id';
+has_many inventory_items => '::InventoryItem', 'ingredient_id';
+has_many links_to_drink_ingredients => '::Drink_Ingredient', 'ingredient_id';
1;

0 comments on commit 6a8f7d8

Please sign in to comment.