extends 'Catalyst::Model';
with 'CatalystX::Component::Traits';
-our $VERSION = '0.40';
+our $VERSION = '0.41';
$VERSION = eval $VERSION;
use namespace::autoclean;
Traits you used resolved to full class names.
+=head1 CONFIGURING YOUR SCHEMA AND RESULTSETS
+
+See the documentation for
+L<Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy> for instructions on how
+to pass config values from your L<Catalyst> config to your
+L<DBIx::Class::Schema> and/or L<DBIx::Class::ResultSet> classes.
+
=head1 METHODS
=head2 new
=head2 storage
Provides an accessor for the connected schema's storage object.
-Used often for debugging and controlling transactions.
+
+See L<DBIx::Class::Storage> and L<DBIx::Class::Storage::DBI>.
=cut
has schema => (is => 'rw', isa => Schema);
+my $app_class;
+
+before COMPONENT => sub {
+ $app_class = ref $_[1] || $_[1];
+};
+
+sub app_class { $app_class }
+
sub BUILD {
my ($self, $args) = @_;
my $class = $self->_original_class_name;
=head1 COPYRIGHT
-Copyright (c) 2006 - 2009
+Copyright (c) 2006 - 2010
the Catalyst::Model::DBIC::Schema L</AUTHOR> and L</CONTRIBUTORS>
as listed above.
=cut
1;
-# vim:sts=4 sw=4 et:
+# vim:sts=4 sw=4 et tw=80:
=head1 DESCRIPTION
Allows you to call your L<DBIx::Class::Schema> methods directly on the Model
-instance, and passes config options to the C<Schema> attributes at C<BUILD>
-time.
+instance, and passes config options to your L<DBIx::Class::Schema> and
+L<DBIx::Class::ResultSet> attributes at C<BUILD> time.
-Methods and attributes local to your C<Model> take precedence over C<Schema>
-methods and attributes.
+Methods and attributes local to your C<Model> take precedence over
+L<DBIx::Class::Schema> or L<DBIx::Class::ResultSet> methods and attributes.
+
+=head1 CREATING SCHEMA CONFIG ATTRIBUTES
+
+To create attributes in your C<Schema.pm>, use either Moose or
+L<Class::Accessor::Grouped>, which is inherited from by all L<DBIx::Class>
+classes automatically. E.g.:
+
+ __PACKAGE__->mk_group_accessors(simple => qw/
+ config_key1
+ config_key2
+ ...
+ /);
+
+Or with L<Moose>:
+
+ use Moose;
+ has config_key1 => (is => 'rw', default => 'default_value');
+
+This code can be added after the md5sum on L<DBIx::Class::Schema::Loader>
+generated schemas.
+
+At app startup, any non-local options will be passed to these accessors, and can
+be accessed as usual via C<< $schema->config_key1 >>.
+
+These config values go into your C<Model::DB> block, along with normal config
+values.
+
+=head1 CREATING RESULTSET CONFIG ATTRIBUTES
+
+You can create classdata on L<DBIx::Class::ResultSet> classes to hold values
+from L<Catalyst> config.
+
+The code for this looks something like this:
+
+ package MySchema::ResultSet::Foo;
+
+ use base 'DBIx::Class::ResultSet';
+
+ __PACKAGE__->mk_group_accessors(inherited => qw/
+ rs_config_key1
+ rs_config_key2
+ ...
+ /);
+ __PACKAGE__->rs_config_key1('default_value');
+
+Or, if you prefer L<Moose>:
+
+ package MySchema::ResultSet::Foo;
+
+ use Moose;
+ use MooseX::NonMoose;
+ use MooseX::ClassAttribute;
+ extends 'DBIx::Class::ResultSet';
+
+ class_has rs_config_key1 => (is => 'rw', default => 'default_value');
+
+In your catalyst config, use the generated Model name as the config key, e.g.:
+
+ <Model::DB::Users>
+ strict_passwords 1
+ </Model::DB::Users>
=cut
my ($self, $args) = @_;
$self->_pass_options_to_schema($args);
+
+ for my $source ($self->schema->sources) {
+ my $config_key = 'Model::' . $self->model_name . '::' . $source;
+ my $config = $self->app_class->config->{$config_key};
+ next unless $config;
+ $self->_pass_options_to_resultset($source, $config);
+ }
};
sub _delegates {
for my $opt (keys %$args) {
if (not exists $attributes{$opt}) {
next unless $self->schema->can($opt);
- $self->schema->$opt($self->{$opt});
+ $self->schema->$opt($args->{$opt});
+ }
+ }
+}
+
+sub _pass_options_to_resultset {
+ my ($self, $source, $args) = @_;
+
+ my @attributes = map {
+ $_->init_arg || ()
+ } $self->meta->get_all_attributes;
+
+ my %attributes;
+ @attributes{@attributes} = ();
+
+ for my $opt (keys %$args) {
+ if (not exists $attributes{$opt}) {
+ my $rs_class = $self->schema->source($source)->resultset_class;
+ next unless $rs_class->can($opt);
+ $rs_class->$opt($args->{$opt});
}
}
}
use Test::Exception;
use Catalyst::Model::DBIC::Schema;
use ASchemaClass;
+use AnotherSchemaClass;
-plan tests => 4;
+# reusing the same app for 2 models, gets a redefined warning
+$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
ok((my $m = instance(a_schema_option => 'mtfnpy')), 'instance');
is $m->schema->a_schema_option, 'pass the crack pipe', 'delegation works';
+ok(($m = instance(schema_class => 'AnotherSchemaClass')), 'instance');
+
+is $m->resultset('Users')->rs_config_option, 'configured rs value',
+ 'ResultSet option passed from config';
+
+done_testing;
+
sub instance {
- Catalyst::Model::DBIC::Schema->COMPONENT('MyApp', {
+ MyApp::Model::DB->COMPONENT('MyApp', {
traits => 'SchemaProxy',
schema_class => 'ASchemaClass',
connect_info => ['dbi:SQLite:foo.db', '', ''],
})
}
-{ package MyApp; use Catalyst; }
+BEGIN {
+ package MyApp;
+ use Catalyst;
+ __PACKAGE__->config({
+ 'Model::DB::Users' => {
+ rs_config_option => 'configured rs value',
+ },
+ });
+}
+
+{
+ package MyApp::Model::DB;
+ use base 'Catalyst::Model::DBIC::Schema';
+}