From: Matt S Trout Date: Fri, 9 Jan 2009 03:28:44 +0000 (+0000) Subject: fixup source registration bugs where schema-specific things were being set on class... X-Git-Tag: v0.08240~198 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0e6c5d584d334aa157caf244e062b4a24c19090c;p=dbsrgits%2FDBIx-Class.git fixup source registration bugs where schema-specific things were being set on class' result_source_instance objects --- diff --git a/lib/DBIx/Class/DB.pm b/lib/DBIx/Class/DB.pm index eadb5ad..1f1ffd8 100644 --- a/lib/DBIx/Class/DB.pm +++ b/lib/DBIx/Class/DB.pm @@ -152,27 +152,47 @@ Returns an instance of the result source for this class __PACKAGE__->mk_classdata('_result_source_instance' => []); +# Yep. this is horrific. Basically what's happening here is that +# (with good reason) DBIx::Class::Schema copies the result source for +# registration. Because we have a retarded setup order forced on us we need +# to actually make our ->result_source_instance -be- the source used, and we +# need to get the source name and schema into ourselves. So this makes it +# happen. + +sub _maybe_attach_source_to_schema { + my ($class, $source) = @_; + if (my $meth = $class->can('schema_instance')) { + my $schema = $class->$meth; + $schema->register_class($class, $class); + my $new_source = $schema->source($class); + %$source = %$new_source; + $schema->source_registrations->{$class} = $source; + } +} + sub result_source_instance { my $class = shift; $class = ref $class || $class; - return $class->_result_source_instance([$_[0], $class]) if @_; + if (@_) { + my $source = $_[0]; + $class->_result_source_instance([$source, $class]); + $class->_maybe_attach_source_to_schema($source); + return $source; + } my($source, $result_class) = @{$class->_result_source_instance}; return unless Scalar::Util::blessed($source); if ($result_class ne $class) { # new class # Give this new class it's own source and register it. - $source = $source->new({ %$source, source_name => $class, result_class => $class } ); $class->_result_source_instance([$source, $class]); - if (my $coderef = $class->can('schema_instance')) { - $coderef->($class)->register_class($class, $class); - } + $class->_maybe_attach_source_to_schema($source); } return $source; } diff --git a/lib/DBIx/Class/ResultSourceProxy/Table.pm b/lib/DBIx/Class/ResultSourceProxy/Table.pm index a1abb1b..5cfe4f3 100644 --- a/lib/DBIx/Class/ResultSourceProxy/Table.pm +++ b/lib/DBIx/Class/ResultSourceProxy/Table.pm @@ -40,11 +40,6 @@ sub _init_result_source_instance { $class->result_source_instance($table); - if ($class->can('schema_instance') && $class->schema_instance) { - $class =~ m/([^:]+)$/; - $class->schema_instance->register_class($class, $class); - } - return $table; } @@ -95,10 +90,6 @@ sub table { $class->result_source_instance($table); - if ($class->can('schema_instance')) { - $class =~ m/([^:]+)$/; - $class->schema_instance->register_class($class, $class); - } return $class->result_source_instance->name; } diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index cf054d0..102e259 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -211,7 +211,6 @@ sub load_namespaces { foreach my $result (keys %results) { my $result_class = $results{$result}; $class->ensure_class_loaded($result_class); - $result_class->source_name($result) unless $result_class->source_name; my $rs_class = delete $resultsets{$result}; my $rs_set = $result_class->resultset_class; @@ -226,7 +225,9 @@ sub load_namespaces { $result_class->resultset_class($rs_class); } - push(@to_register, [ $result_class->source_name, $result_class ]); + my $source_name = $result_class->source_name || $result; + + push(@to_register, [ $source_name, $result_class ]); } } @@ -1228,7 +1229,7 @@ sub register_extra_source { sub _register_source { my ($self, $moniker, $source, $params) = @_; - %$source = %{ $source->new( { %$source, source_name => $moniker }) }; + $source = $source->new({ %$source, source_name => $moniker }); my %reg = %{$self->source_registrations}; $reg{$moniker} = $source; diff --git a/t/lib/DBICNSTest/Result/B.pm b/t/lib/DBICNSTest/Result/B.pm index 1d2c779..e9cdc37 100644 --- a/t/lib/DBICNSTest/Result/B.pm +++ b/t/lib/DBICNSTest/Result/B.pm @@ -1,5 +1,5 @@ package DBICNSTest::Result::B; -use base qw/DBICNSTest::Result::A/; +use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto Core/); __PACKAGE__->table('b'); __PACKAGE__->add_columns('b');