[Jifty-commit] r2916 - in jifty/branches/virtual-models: . lib/Jifty t/TestApp/t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Mar 6 16:22:17 EST 2007


Author: sterling
Date: Tue Mar  6 16:22:16 2007
New Revision: 2916

Added:
   jifty/branches/virtual-models/t/TestApp/lib/TestApp/Model/Address.pm
   jifty/branches/virtual-models/t/TestApp/t/00-model-Address.t   (contents, props changed)
   jifty/branches/virtual-models/t/TestApp/t/create-by-uuid.t   (contents, props changed)
Modified:
   jifty/branches/virtual-models/   (props changed)
   jifty/branches/virtual-models/lib/Jifty/Handle.pm
   jifty/branches/virtual-models/lib/Jifty/Record.pm

Log:
Added the ability to create using UUIDs on refers_to columns.

Modified: jifty/branches/virtual-models/lib/Jifty/Handle.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Handle.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Handle.pm	Tue Mar  6 16:22:16 2007
@@ -336,6 +336,43 @@
     return $uuid;
 }
 
+=head2 lookup_record($uuid)
+
+Performs the reverse operation of the above. It returns an instance of the record represented by the given UUID. Returns C<undef> if no record matching the given UUID is found.
+
+=cut
+
+# XXX sterling: Is this really implemented in the best way? Particularly the
+# record class lookup?
+
+# XXX sterling: Should this return a Class::ReturnValue or something else?
+sub lookup_record {
+    my $self = shift;
+    my $uuid = shift;
+
+    # Lookup the table name and ID
+    my ($table, $id) = $self->fetch_result(qq[ SELECT row_table, row_id FROM _jifty_uuids WHERE uuid = ? ], $uuid);
+
+    return unless defined $table and defined $id;
+
+    # Find the record class for the table
+    my ($record_class) = grep { $_->table eq $table } 
+                              Jifty->class_loader->models;
+
+    return unless defined $record_class;
+    
+    # Only return a value when load succeeds
+    my $instance = $record_class->new;
+    if ($instance->load($id)) {
+        return $instance;
+    }
+
+    # Otherwise, return undef
+    else {
+        return;
+    }
+}
+
 =head1 AUTHOR
 
 Various folks at BestPractical Solutions, LLC.

Modified: jifty/branches/virtual-models/lib/Jifty/Record.pm
==============================================================================
--- jifty/branches/virtual-models/lib/Jifty/Record.pm	(original)
+++ jifty/branches/virtual-models/lib/Jifty/Record.pm	Tue Mar  6 16:22:16 2007
@@ -3,6 +3,8 @@
 
 package Jifty::Record;
 
+use Data::UUID;
+
 use Jifty::Config;
 
 =head1 NAME
@@ -47,6 +49,8 @@
 
 =item Remove C<id> values unless they are truly numeric
 
+=item Convert UUIDs to the records they represent
+
 =item Automatically load by id after create
 
 =item actually stop creating the new record if a field fails to validate.
@@ -72,6 +76,37 @@
         wantarray ? return ( 0, _('Permission denied') ) : return (0);
     }
 
+    # XXX sterling: using Data::UUID to identify UUIDs may be overkill
+    my $ug = Data::UUID->new;
+    foreach my $column_name ( keys %attribs ) {
+        my $column = $self->column($column_name);
+        if (    $column
+            and $column->readable
+            and $column->refers_to
+            and UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Record" )
+            and eval { $ug->from_string($attribs{$column_name}) }) {
+
+            # Load the record
+            my $record = Jifty->handle->lookup_record($attribs{$column_name});
+            
+            # Make sure the returned record is the right thing
+            if (UNIVERSAL::isa( $record, $column->refers_to )) {
+
+                # Set the attribute
+                $attribs{$column_name} = $record;
+            }
+
+            # XXX sterling: this should log something and quit
+            else {
+                $self->log->error("The UUID for $column_name was not found in the database.");
+                if ($class) {
+                    return($self);
+                } else {
+                    return (0, "UUID for $column_name was not found in the database.");
+                }
+            }
+        }
+    }    
     foreach my $key ( keys %attribs ) {
         my $attr = $attribs{$key};
         my $method = "canonicalize_$key";

Added: jifty/branches/virtual-models/t/TestApp/lib/TestApp/Model/Address.pm
==============================================================================
--- (empty file)
+++ jifty/branches/virtual-models/t/TestApp/lib/TestApp/Model/Address.pm	Tue Mar  6 16:22:16 2007
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+
+package TestApp::Model::Address;
+use Jifty::DBI::Schema;
+
+use constant CLASS_UUID => '0C51A3B2-CC25-11DB-AC90-0B3329635B38';
+
+use TestApp::Record schema {
+    column person =>
+        refers_to TestApp::Model::User;
+    column name =>
+        type is 'text',
+        label is 'Name';
+    column street =>
+        type is 'text',
+        label is 'Street';
+};
+
+# Your model-specific methods go here.
+
+1;
+

Added: jifty/branches/virtual-models/t/TestApp/t/00-model-Address.t
==============================================================================
--- (empty file)
+++ jifty/branches/virtual-models/t/TestApp/t/00-model-Address.t	Tue Mar  6 16:22:16 2007
@@ -0,0 +1,52 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+=head1 DESCRIPTION
+
+A basic test harness for the Address model.
+
+=cut
+
+use lib 't/lib';
+use Jifty::SubTest;
+
+use Jifty::Test tests => 11;
+
+# Make sure we can load the model
+use_ok('TestApp::Model::Address');
+
+# Grab a system user
+my $system_user = TestApp::CurrentUser->superuser;
+ok($system_user, "Found a system user");
+
+# Try testing a create
+my $o = TestApp::Model::Address->new(current_user => $system_user);
+my ($id) = $o->create();
+ok($id, "Address create returned success");
+ok($o->id, "New Address has valid id set");
+is($o->id, $id, "Create returned the right id");
+
+# And another
+$o->create();
+ok($o->id, "Address create returned another value");
+isnt($o->id, $id, "And it is different from the previous one");
+
+# Searches in general
+my $collection =  TestApp::Model::AddressCollection->new(current_user => $system_user);
+$collection->unlimit;
+is($collection->count, 2, "Finds two records");
+
+# Searches in specific
+$collection->limit(column => 'id', value => $o->id);
+is($collection->count, 1, "Finds one record with specific id");
+
+# Delete one of them
+$o->delete;
+$collection->redo_search;
+is($collection->count, 0, "Deleted row is gone");
+
+# And the other one is still there
+$collection->unlimit;
+is($collection->count, 1, "Still one left");
+

Added: jifty/branches/virtual-models/t/TestApp/t/create-by-uuid.t
==============================================================================
--- (empty file)
+++ jifty/branches/virtual-models/t/TestApp/t/create-by-uuid.t	Tue Mar  6 16:22:16 2007
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use lib 't/lib';
+use Jifty::SubTest;
+
+use Jifty::Test tests => 6;
+
+use_ok('TestApp::Model::User');
+use_ok('TestApp::Model::Address');
+
+my $system_user = TestApp::CurrentUser->superuser;
+ok($system_user, 'got a system user');
+
+my $user = TestApp::Model::User->new( current_user => $system_user );
+$user->create( name => $$, email => $$, password => $$ );
+ok($user->id, 'created a user');
+
+my $address = TestApp::Model::Address->new( current_user => $system_user );
+$address->create( person => $user->__uuid, name => $$, street => $$ );
+ok($address->id, 'created an address');
+
+is($address->person->id, $user->id, 'created address with correct user object');


More information about the Jifty-commit mailing list