[Jifty-commit] r3500 - in apps/CASPlus/trunk: lib/CASPlus/Model
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Thu Jun 14 23:57:51 EDT 2007
Author: sterling
Date: Thu Jun 14 23:57:51 2007
New Revision: 3500
Modified:
apps/CASPlus/trunk/ (props changed)
apps/CASPlus/trunk/lib/CASPlus/Model/ProfileRelationship.pm
Log:
r7640 at riddle: andrew | 2007-06-14 22:57:13 -0500
Added comments to code paragraphs. Drop any values that may be set in link_table, link_table_parent, and link_table_child if relationships are one-to-one or one-to-many. Added code to convert numeric IDs in relation_parent and relation_child to Profile objects.
Modified: apps/CASPlus/trunk/lib/CASPlus/Model/ProfileRelationship.pm
==============================================================================
--- apps/CASPlus/trunk/lib/CASPlus/Model/ProfileRelationship.pm (original)
+++ apps/CASPlus/trunk/lib/CASPlus/Model/ProfileRelationship.pm Thu Jun 14 23:57:51 2007
@@ -6,6 +6,9 @@
use constant CLASS_UUID => 'F88DC88A-FFBC-11DB-A0E9-5947F1458521';
+use Carp;
+use Scalar::Util qw( looks_like_number );
+
=head1 NAME
CASPlus::Model::ProfileRelationship - Specify relationships between profiles
@@ -207,18 +210,38 @@
my $self = shift;
my $args = shift;
+ # Make the names into identifiers
my $child_name = CASPlus::Util->convert_to_identifier($args->{child_name});
my $parent_name = CASPlus::Util->convert_to_identifier($args->{parent_name});
+ # Load the relation_parent and relation_child if given by ID
+ for my $relation_name ( qw( relation_parent relation_child ) ) {
+ if (looks_like_number($args->{ $relation_name })) {
+ my $relation = CASPlus::Model::Profile->new;
+ $relation->load($args->{ $relation_name });
+
+ if (!$relation->id) {
+ croak "Could not find a profile matching ID $args->{$relation_name} for $relation_name.";
+ }
+
+ $args->{ $relation_name } = $relation;
+ }
+ }
+
+ # Handle many-to-many relationships specially
if ($args->{many_parents} and $args->{many_children}) {
+
+ # Prepare the name of the linking table
my $name = CASPlus::Util->convert_to_camel_case($args->{name});
+ # Create the link table
my $link_table = Jifty::Model::ModelClass->new;
$link_table->create(
name => $name,
super_classes => 'CASPlus::ProfileBase',
);
+ # Create the link table parent column
my $link_table_parent = Jifty::Model::ModelClassColumn->new;
$link_table_parent->create(
model_class => $link_table,
@@ -226,6 +249,7 @@
refers_to_class => $args->{relation_parent}->model_class->name,
);
+ # Create the link table child column
my $link_table_child = Jifty::Model::ModelClassColumn->new;
$link_table_child->create(
model_class => $link_table,
@@ -233,6 +257,7 @@
refers_to_class => $args->{relation_child}->model_class->name,
);
+ # Create the relationship to the children via the link table
my $parent_column = Jifty::Model::ModelClassColumn->new;
$parent_column->create(
model_class => $args->{relation_parent}->model_class,
@@ -241,6 +266,7 @@
refers_to_by => 'parent',
);
+ # Create the relationship to the parents via the link table
my $child_column = Jifty::Model::ModelClassColumn->new;
$child_column->create(
model_class => $args->{relation_child}->model_class,
@@ -249,14 +275,21 @@
refers_to_by => 'child',
);
+ # Associate all these tables and columns with the new object
$args->{link_table} = $link_table;
$args->{link_table_parent} = $link_table_parent;
$args->{link_table_child} = $link_table_child;
$args->{parent_column} = $parent_column;
$args->{child_column} = $child_column;
}
+
+ # Handle one-to-one or one-to-many relationships
else {
+
+ # Create the child column linking back to the parent
my $child_column = Jifty::Model::ModelClassColumn->new;
+
+ # One-to-many relation with many parents
if ($args->{many_parents}) {
$child_column->create(
model_class => $args->{relation_child}->model_class,
@@ -265,6 +298,8 @@
refers_to_by => $child_name,
);
}
+
+ # One-to-one or One-to-Many with only a single parent
else {
$child_column->create(
model_class => $args->{relation_child}->model_class,
@@ -273,7 +308,10 @@
);
}
+ # Create the parent column linking back to the children
my $parent_column = Jifty::Model::ModelClassColumn->new;
+
+ # One-to-many relationship with many children
if ($args->{many_children}) {
$parent_column->create(
model_class => $args->{relation_parent}->model_class,
@@ -282,6 +320,8 @@
refers_to_by => $parent_name,
);
}
+
+ # One-to-one or one-to-many relationship with one child
else {
$parent_column->create(
model_class => $args->{relation_parent}->model_class,
@@ -290,8 +330,14 @@
);
}
+ # Associate these columns with the new object
$args->{parent_column} = $parent_column;
$args->{child_column} = $child_column;
+
+ # Clear the linking table columns. They must be unset.
+ delete $args->{link_table};
+ delete $args->{link_table_parent};
+ delete $args->{link_table_child};
}
return 1;
More information about the Jifty-commit
mailing list