Skip to content

Commit

Permalink
No longer use rel_info($rel)->{class} in the cond resolver
Browse files Browse the repository at this point in the history
It turns out there are a lot of codebases there containing garbage in the
rel definition. Punt for after 0.082800 to lean up that mess.

Also be less strict on checking the foreign_values contents - downgrade
mismatches to a warning (but still hard-require ::Row ancestry)
  • Loading branch information
ribasushi committed Sep 8, 2014
1 parent d758a25 commit 7e5a0e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
18 changes: 14 additions & 4 deletions lib/DBIx/Class/ResultSource.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,7 @@ Internals::SvREADONLY($UNRESOLVABLE_CONDITION => 1);
## self-explanatory API, modeled on the custom cond coderef:
# rel_name => (scalar)
# foreign_alias => (scalar)
# foreign_values => (either not supplied or a hashref)
# foreign_values => (either not supplied, or a hashref, or a foreign ResultObject (to be ->get_columns()ed), or plain undef )
# self_alias => (scalar)
# self_result_object => (either not supplied or a result object)
# require_join_free_condition => (boolean, throws on failure to construct a JF-cond)
Expand Down Expand Up @@ -1893,8 +1893,15 @@ sub _resolve_relationship_condition {

if (exists $args->{foreign_values}) {
if (defined blessed $args->{foreign_values}) {
$self->throw_exception( "Object supplied as 'foreign_values' ($args->{foreign_values}) must be of class '$rel_info->{class}'" )
unless $args->{foreign_values}->isa($rel_info->{class});

$self->throw_exception( "Objects supplied as 'foreign_values' ($args->{foreign_values}) must inherit from DBIx::Class::Row" )
unless $args->{foreign_values}->isa('DBIx::Class::Row');

carp_unique(
"Objects supplied as 'foreign_values' ($args->{foreign_values}) "
. "usually should inherit from the related ResultClass ('@{[ $rel_rsrc->result_class ]}'), "
. "perhaps you've made a mistake invoking the condition resolver?"
) unless $args->{foreign_values}->isa($rel_rsrc->result_class);

$args->{foreign_values} = { $args->{foreign_values}->get_columns };
}
Expand All @@ -1905,7 +1912,10 @@ sub _resolve_relationship_condition {
) for keys %{ $args->{foreign_values} ||= {} };
}
else {
$self->throw_exception( "Argument 'foreign_values' must be either an object inheriting from '$rel_info->{class}' or a hash reference or undef" );
$self->throw_exception(
"Argument 'foreign_values' must be either an object inheriting from '@{[ $rel_rsrc->result_class ]}', "
. "or a hash reference, or undef"
);
}
}

Expand Down
8 changes: 5 additions & 3 deletions t/cdbi/06-hasa.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use strict;
use warnings;
use Test::More;
use Test::Exception;
use DBIx::Class::_Util 'sigwarn_silencer';

@YA::Film::ISA = 'Film';

Expand Down Expand Up @@ -105,7 +107,8 @@ sub taste_bad {

sub fail_with_bad_object {
my ($dir, $codir) = @_;
eval {
throws_ok {
local $SIG{__WARN__} = sigwarn_silencer( qr/\Qusually should inherit from the related ResultClass ('Director')/ );
YA::Film->create(
{
Title => 'Tastes Bad',
Expand All @@ -115,8 +118,7 @@ sub fail_with_bad_object {
NumExplodingSheep => 23
}
);
};
ok $@, $@;
} qr/isn't a Director/;
}

package Foo;
Expand Down
21 changes: 12 additions & 9 deletions t/cdbi/18-has_a.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use strict;
use warnings;
use Test::More;
use Test::Exception;
use DBIx::Class::_Util 'sigwarn_silencer';

use lib 't/cdbi/testlib';
use Film;
Expand Down Expand Up @@ -45,8 +47,8 @@ my $sj = Director->create({
});

{
eval { $btaste->Director($btaste) };
like $@, qr/Director/, "Can't set film as director";
throws_ok { $btaste->Director($btaste) }
qr/isn't a Director/, "Can't set film as director";
is $btaste->Director->id, $pj->id, "PJ still the director";

# drop from cache so that next retrieve() is from db
Expand All @@ -69,8 +71,7 @@ my $sj = Director->create({
is $sj->id, 'Skippy Jackson', 'Create new director - Skippy';
Film->has_a('CoDirector' => 'Director');
{
eval { $btaste->CoDirector("Skippy Jackson") };
is $@, "", "Auto inflates";
lives_ok { $btaste->CoDirector("Skippy Jackson") };
isa_ok $btaste->CoDirector, "Director";
is $btaste->CoDirector->id, $sj->id, "To skippy";
}
Expand All @@ -96,16 +97,16 @@ is(
$pj = Director->retrieve('Peter Jackson');

my $fail;
eval {
throws_ok {
local $SIG{__WARN__} = sigwarn_silencer( qr/\Qusually should inherit from the related ResultClass ('Director')/ );
$fail = YA::Film->create({
Title => 'Tastes Bad',
Director => $sj,
codirector => $btaste,
Rating => 'R',
NumExplodingSheep => 23
});
};
ok $@, "Can't have film as codirector: $@";
} qr/isn't a Director/, "Can't have film as codirector";
is $fail, undef, "We didn't get anything";

my $tastes_bad = YA::Film->create({
Expand Down Expand Up @@ -226,8 +227,10 @@ SKIP: {
}

{ # Broken has_a declaration
eval { Film->has_a(driector => "Director") };
like $@, qr/driector/, "Sensible error from has_a with incorrect column: $@";
throws_ok{ Film->has_a(driector => "Director") }
qr/No such column driector/,
"Sensible error from has_a with incorrect column"
;
}

done_testing;

0 comments on commit 7e5a0e7

Please sign in to comment.