[Jifty-commit] jifty-dbi branch, master, updated. 0.60-37-ga85e7e2

Jifty commits jifty-commit at lists.jifty.org
Tue Jan 11 15:37:41 EST 2011


The branch, master has been updated
       via  a85e7e22a19791f482a5c1028ace8545cb437d4b (commit)
      from  1253d159f3b8ead5d974287f5fe60bc5196826de (commit)

Summary of changes:
 lib/Jifty/DBI/Record.pm |   17 ++++++++----
 t/19reference.t         |   60 +++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 61 insertions(+), 16 deletions(-)

- Log -----------------------------------------------------------------
commit a85e7e22a19791f482a5c1028ace8545cb437d4b
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Tue Jan 11 15:23:26 2011 -0500

    Respect the 'by' attribute for refers_to columns in create and set
    
    Record create and the autogenerated setter assumed that refers_to
    columns always referred to other records by their id column when passed
    a Jifty::DBI::Record object as a value.

diff --git a/lib/Jifty/DBI/Record.pm b/lib/Jifty/DBI/Record.pm
index 50dce33..bb5c421 100755
--- a/lib/Jifty/DBI/Record.pm
+++ b/lib/Jifty/DBI/Record.pm
@@ -349,8 +349,12 @@ sub _init_methods_for_column {
                         my $self = shift;
                         my $val  = shift;
 
-                        $val = $val->id
-                            if UNIVERSAL::isa( $val, 'Jifty::DBI::Record' );
+                        if (UNIVERSAL::isa( $val, 'Jifty::DBI::Record' )) {
+                            my $col = $self->column($column_name);
+                            my $by  = defined $col->by ? $col->by : 'id';
+                            $val = $val->$by;
+                        }
+
                         return (
                             $self->_set(
                                 column => $column_name,
@@ -1404,11 +1408,12 @@ sub __create {
         }
         if (    $column->readable
             and $column->refers_to
-            and UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Record" ) )
+            and UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Record" )
+            and UNIVERSAL::isa( $attribs{$column_name}, 'Jifty::DBI::Record' ) )
         {
-            $attribs{$column_name} = $attribs{$column_name}->id
-                if UNIVERSAL::isa( $attribs{$column_name},
-                'Jifty::DBI::Record' );
+            # lookup the column referenced or default to id
+            my $by = defined $column->by ? $column->by : 'id';
+            $attribs{$column_name} = $attribs{$column_name}->$by;
         }
 
         $self->_apply_input_filters(
diff --git a/t/19reference.t b/t/19reference.t
index d263455..51586af 100644
--- a/t/19reference.t
+++ b/t/19reference.t
@@ -6,7 +6,7 @@ use Test::More;
 BEGIN { require "t/utils.pl" }
 our (@available_drivers);
 
-use constant TESTS_PER_DRIVER => 13;
+use constant TESTS_PER_DRIVER => 29;
 
 my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -34,17 +34,21 @@ SKIP: {
         {my $ret = init_schema( 'TestApp::Food', $handle );
         isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back" );}
 
-        my $rec = TestApp::Currency->new( handle => $handle );
-        isa_ok($rec, 'Jifty::DBI::Record');
+        my $usd = TestApp::Currency->new( handle => $handle );
+        isa_ok($usd, 'Jifty::DBI::Record');
+
+        my ($id) = $usd->create( name => "USD" );
 
-        my ($id) = $rec->create( name => "USD" );
+        ok($id, "got id");
+        ok($usd->load($id), "loaded the just created currency record");
 
-        $rec = TestApp::Food->new( handle => $handle );
+        my $rec = TestApp::Food->new( handle => $handle );
         isa_ok($rec, 'Jifty::DBI::Record');
 
         my ($paella) = $rec->create( name => "paella" );
         $rec->create( name => "nigiri" );
 
+        # create using currency string
         $rec = TestApp::User->new( handle => $handle );
         ($id) = $rec->create( currency => 'USD' );
 
@@ -54,12 +58,48 @@ SKIP: {
         is($rec->currency->name, 'USD');
 
         is( $rec->food, undef, 'null_reference option in effect' );
+        
+        {
+            no warnings 'once';
+            local *TestApp::User::null_reference = sub {0};
+            $rec->load($id);
+            isa_ok($rec->food, 'TestApp::Food', 'referee is null but shuold still return an object');
+            is($rec->food->id, undef);
+        }
+
+        # create using currency object
+        $rec = TestApp::User->new( handle => $handle );
+        ($id) = $rec->create( currency => $usd );
+
+        ok($id);
+        ok($rec->load($id), "Loaded the record");
+        isa_ok($rec->currency, 'TestApp::Currency');
+        is($rec->currency->name, 'USD');
+
+        my $food = TestApp::Food->new( handle => $handle );
+        $food->load($paella);
+
+        # create with undef, set using currency string
+        $rec = TestApp::User->new( handle => $handle );
+        ($id) = $rec->create(food => $food);
+
+        ok($id);
+        ok($rec->load($id), "Loaded the record");
+        is($rec->currency, undef, 'No currency object');
+        $rec->set_currency('USD');
+        isa_ok($rec->currency, 'TestApp::Currency');
+        is($rec->currency->name, 'USD');
 
-        no warnings 'once';
-        local *TestApp::User::null_reference = sub {0};
-        $rec->load($id);
-        isa_ok($rec->food, 'TestApp::Food', 'referee is null but shuold still return an object');
-        is($rec->food->id, undef);
+        # create with undef, set using currency object
+        $rec = TestApp::User->new( handle => $handle );
+        ($id) = $rec->create(food => $food);
+
+        ok($id);
+        ok($rec->load($id), "Loaded the record");
+        is($rec->currency, undef, 'No currency object');
+        $rec->set_currency($usd);
+        isa_ok($rec->currency, 'TestApp::Currency');
+        is($rec->currency->name, 'USD');
 }
 }
 

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list