[Jifty-commit] jifty-dbi branch, master, updated. 0.64-4-g50c543c
Jifty commits
jifty-commit at lists.jifty.org
Fri Jan 14 18:02:37 EST 2011
The branch, master has been updated
via 50c543c7a52483d9f73536b7d19a85509a09fcc5 (commit)
from b16a52fd8b36caafd37d534de20e24f05f6d38c2 (commit)
Summary of changes:
lib/Jifty/DBI/Collection.pm | 12 ++++++++--
lib/Jifty/DBI/Record.pm | 5 +--
t/19reference.t | 46 ++++++++++++++++++++++++++++++++++++++----
3 files changed, 52 insertions(+), 11 deletions(-)
- Log -----------------------------------------------------------------
commit 50c543c7a52483d9f73536b7d19a85509a09fcc5
Author: Thomas Sibley <trs at bestpractical.com>
Date: Fri Jan 14 18:01:04 2011 -0500
Use the right FK when using a record object in load_by_cols or limit
These used to assume id was the foreign key. Resolves rt.cpan.org #64779.
diff --git a/lib/Jifty/DBI/Collection.pm b/lib/Jifty/DBI/Collection.pm
index a2f9298..ea50a43 100755
--- a/lib/Jifty/DBI/Collection.pm
+++ b/lib/Jifty/DBI/Collection.pm
@@ -1275,17 +1275,23 @@ sub limit {
if ( ( $value_ref ne 'ARRAY' )
&& $args{value}->isa('Jifty::DBI::Record') )
{
- $args{value} = $args{value}->id;
+ my $by = (defined $column_obj and defined $column_obj->by)
+ ? $column_obj->by
+ : 'id';
+ $args{value} = $args{value}->$by;
} elsif ( $value_ref eq 'ARRAY' ) {
# Don't modify the original reference, it isn't polite
$args{value} = [ @{ $args{value} } ];
map {
+ my $by = (defined $column_obj and defined $column_obj->by)
+ ? $column_obj->by
+ : 'id';
$_ = (
( ref $_ && $_->isa('Jifty::DBI::Record') )
- ? ( $_->id )
+ ? ( $_->$by )
: $_
- )
+ )
} @{ $args{value} };
}
}
diff --git a/lib/Jifty/DBI/Record.pm b/lib/Jifty/DBI/Record.pm
index bb5c421..91bbf2a 100755
--- a/lib/Jifty/DBI/Record.pm
+++ b/lib/Jifty/DBI/Record.pm
@@ -1134,9 +1134,8 @@ sub load_by_cols {
}
if ( blessed $value && $value->isa('Jifty::DBI::Record') ) {
-
- # XXX TODO: check for proper foriegn keyness here
- $value = $value->id;
+ my $by = defined $column_obj->by ? $column_obj->by : 'id';
+ $value = $value->$by;
}
$self->_apply_input_filters(
diff --git a/t/19reference.t b/t/19reference.t
index 51586af..8be6d74 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 => 29;
+use constant TESTS_PER_DRIVER => 40;
my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
plan tests => $total;
@@ -34,14 +34,24 @@ SKIP: {
{my $ret = init_schema( 'TestApp::Food', $handle );
isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back" );}
+ # USD
my $usd = TestApp::Currency->new( handle => $handle );
isa_ok($usd, 'Jifty::DBI::Record');
my ($id) = $usd->create( name => "USD" );
-
ok($id, "got id");
- ok($usd->load($id), "loaded the just created currency record");
+ ok($usd->load($id), "loaded the just created currency record (USD)");
+ is($usd->name, "USD", "same name");
+
+ # GBP
+ my $gbp = TestApp::Currency->new(handle=>$handle);
+ isa_ok($usd, 'Jifty::DBI::Record');
+ my ($gid) = $gbp->create( name => "GBP" );
+ ok($gid, "got id");
+ ok($gbp->load($gid), "loaded the just created currency record (GBP)");
+ is($gbp->name, "GBP", "same name");
+
my $rec = TestApp::Food->new( handle => $handle );
isa_ok($rec, 'Jifty::DBI::Record');
@@ -97,9 +107,32 @@ SKIP: {
ok($id);
ok($rec->load($id), "Loaded the record");
is($rec->currency, undef, 'No currency object');
- $rec->set_currency($usd);
+ $rec->set_currency($gbp);
isa_ok($rec->currency, 'TestApp::Currency');
- is($rec->currency->name, 'USD');
+ is($rec->currency->name, 'GBP');
+
+ # load_by_cols with object
+ $rec = TestApp::User->new(handle=>$handle);
+ $rec->load_by_cols( currency => $usd );
+
+ ok($rec->id, "got id");
+ is($rec->currency->name, "USD", "got currency");
+
+ # limit with object
+ my $users = TestApp::UserCollection->new(handle => $handle);
+ $users->limit( column => 'currency', value => $usd );
+
+ is($users->count, 3, "got 3 users");
+ is($users->first->currency->name, "USD", "got USD");
+
+ $users = TestApp::UserCollection->new(handle => $handle);
+ $users->limit( column => 'currency', value => $gbp );
+ is($users->count, 1, "got 1 users");
+
+ # limit with mixed array
+ $users = TestApp::UserCollection->new(handle => $handle);
+ $users->limit( column => 'currency', value => [$gbp, 'USD'] );
+ is($users->count, 4, "got 4 users");
}
}
@@ -239,4 +272,7 @@ column food =>
};
+package TestApp::UserCollection;
+use base qw/Jifty::DBI::Collection/;
+
1;
-----------------------------------------------------------------------
More information about the Jifty-commit
mailing list