Revision history for DBIx::Class
0.08099_04 2008-07-24 01:00:00
+ - Added register_extra_source methods for additional sources
- Added datetime_undef_if_invalid for InflateColumn::DateTime to
return undef on invalid date/time values
- Added search_related_rs method to ResultSet
=cut
sub register_source {
- my ($self, $moniker, $source) = @_;
+ my $self = shift;
+
+ $self->_register_source(@_);
+}
+
+=head2 register_extra_source
+
+=over 4
+
+=item Arguments: $moniker, $result_source
+
+=back
+
+As L</register_source> but should be used if the result class already
+has a source and you want to register an extra one.
+
+=cut
+
+sub register_extra_source {
+ my $self = shift;
+
+ $self->_register_source(@_, { extra => 1 });
+}
+
+sub _register_source {
+ my ($self, $moniker, $source, $params) = @_;
%$source = %{ $source->new( { %$source, source_name => $moniker }) };
$source->schema($self);
+ return if ($params->{extra});
+
weaken($source->{schema}) if ref($self);
if ($source->result_class) {
my %map = %{$self->class_mappings};
+ if (exists $map{$source->result_class}) {
+ warn $source->result_class . ' already has a source, use register_extra_source for additional sources';
+ }
$map{$source->result_class} = $moniker;
$self->class_mappings(\%map);
}
foreach my $moniker ($self->sources) {
my $source = $self->source($moniker);
my $new = $source->new($source);
+ $clone->_unregister_source($moniker);
$clone->register_source($moniker => $new);
}
$clone->storage->set_schema($clone) if $clone->storage;
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+{
+ package DBICTest::ResultSource::OtherSource;
+ use strict;
+ use warnings;
+ use base qw/DBIx::Class::ResultSource::Table/;
+}
+
+plan tests => 3;
+
+my $schema = DBICTest->init_schema();
+my $artist_source = $schema->source('Artist');
+
+my $new_source = DBICTest::ResultSource::OtherSource->new({
+ %$artist_source,
+ name => 'artist_preview',
+ _relationships => Storable::dclone( $artist_source->_relationships ),
+});
+
+$new_source->add_column('other_col' => { data_type => 'integer', default_value => 1 });
+
+my $warn = '';
+$SIG{__WARN__} = sub { $warn = shift };
+
+{
+ $schema->register_extra_source( 'artist->extra' => $new_source );
+
+ my $source = $schema->source('DBICTest::Artist');
+ is($source->source_name, 'Artist', 'original source still primary source');
+}
+
+{
+ my $new_source_name = 'Artist->preview(artist_preview)';
+ $schema->register_source( $new_source_name => $new_source );
+
+ ok(($warn =~ /DBICTest::Artist already has a source, use register_extra_source for additional sources/), 'registering extra source causes errors');
+
+ my $source = $schema->source('DBICTest::Artist');
+ is($source->source_name, $new_source_name, 'original source still primary source');
+}
+
+1;