From: Matt S Trout Date: Wed, 3 Jun 2009 19:42:36 +0000 (+0100) Subject: initial tests and some fixups X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7abe3af2b03bc2dd077403ef7e59d9252a5f5817;p=dbsrgits%2FDBIx-Class-ResultSource-MultipleTableInheritance.git initial tests and some fixups --- diff --git a/lib/DBIx/Class/ResultSource/MultipleTableInheritance.pm b/lib/DBIx/Class/ResultSource/MultipleTableInheritance.pm index cb9edce..f27eab6 100644 --- a/lib/DBIx/Class/ResultSource/MultipleTableInheritance.pm +++ b/lib/DBIx/Class/ResultSource/MultipleTableInheritance.pm @@ -6,6 +6,7 @@ use parent qw(DBIx::Class::ResultSource::View); use Method::Signatures::Simple; use Carp::Clan qw/^DBIx::Class/; use aliased 'DBIx::Class::ResultSource::Table'; +use aliased 'DBIx::Class::ResultClass::HashRefInflator'; use namespace::autoclean; # how this works: @@ -29,14 +30,23 @@ method new ($class: @args) { my $new = $class->next::method(@args); my $rc = $new->result_class; if (my $meth = $rc->can('result_source_instance')) { - $new->parent_source($rc->$meth); - } - if ($new->schema) { - $new->_attach_additional_sources; + my $source = $rc->$meth; + if ($source->result_class ne $new->result_class + && $new->result_class->isa($source->result_class)) { + $new->parent_source($source); + } } return $new; } +method schema (@args) { + my $ret = $self->next::method(@args); + if (@args) { + $self->_attach_additional_sources; + } + return $ret; +} + method _attach_additional_sources () { my $raw_name = $self->_raw_source_name; my $schema = $self->schema; @@ -67,10 +77,10 @@ method _attach_additional_sources () { foreach my $pri ($self->primary_columns) { my %info = %{$self->column_info($pri)}; delete @info{qw(is_auto_increment sequence auto_nextval)}; - $self->add_column($pri => \%info); + $table->add_column($pri => \%info); $join{"foreign.${pri}"} = "self.${pri}"; } - $self->add_relationship('parent', $parent->source_name, \%join); + $table->add_relationship('parent', $parent->source_name, \%join); } $table->set_primary_key($self->primary_columns); $schema->register_source($raw_name => $table); diff --git a/t/01load.t b/t/01load.t new file mode 100644 index 0000000..a913536 --- /dev/null +++ b/t/01load.t @@ -0,0 +1,19 @@ +use strict; +use warnings; +use lib 't/lib'; +use Test::More qw(no_plan); +use Data::Dumper; $Data::Dumper::Indent = 1; + +BEGIN { use_ok 'MTITest'; } + +warn MTITest->sources; + +my $raw_foo = MTITest->source('Raw::Foo'); + +is_deeply( + [ $raw_foo->columns ], + [ qw(id a) ], + 'Columns for raw foo ok' +); + +warn Dumper $raw_foo->_columns; diff --git a/t/lib/MTITest.pm b/t/lib/MTITest.pm new file mode 100644 index 0000000..91471fd --- /dev/null +++ b/t/lib/MTITest.pm @@ -0,0 +1,9 @@ +package MTITest; + +use strict; +use warnings; +use parent qw(DBIx::Class::Schema); + +__PACKAGE__->load_namespaces; + +1; diff --git a/t/lib/MTITest/Result/Bar.pm b/t/lib/MTITest/Result/Bar.pm new file mode 100644 index 0000000..206b340 --- /dev/null +++ b/t/lib/MTITest/Result/Bar.pm @@ -0,0 +1,13 @@ +package MTITest::Result::Bar; + +use strict; +use warnings; +use parent qw(MTITest::Result::Foo); + +__PACKAGE__->table('bar'); + +__PACKAGE__->add_columns( + b => { data_type => 'integer' } +); + +1; diff --git a/t/lib/MTITest/Result/Foo.pm b/t/lib/MTITest/Result/Foo.pm new file mode 100644 index 0000000..7d5f82d --- /dev/null +++ b/t/lib/MTITest/Result/Foo.pm @@ -0,0 +1,19 @@ +package MTITest::Result::Foo; + +use strict; +use warnings; +use parent qw(DBIx::Class::Core); +use aliased 'DBIx::Class::ResultSource::MultipleTableInheritance'; + +__PACKAGE__->table_class(MultipleTableInheritance); + +__PACKAGE__->table('foo'); + +__PACKAGE__->add_columns( + id => { data_type => 'integer', is_auto_increment => 1 }, + a => { data_type => 'text' } +); + +__PACKAGE__->set_primary_key('id'); + +1;