aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Catalyst/Model/Reaction/InterfaceModel/DBIC.pm
blob: 6b59e295fb554914815101b80d97c8c88af5d379 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package Catalyst::Model::Reaction::InterfaceModel::DBIC;

use Reaction::Class;

use Catalyst::Utils;
use Catalyst::Component;
use Class::MOP;

#XXX so yeah, thisis kinda hacky. big whop though, i need it.
#this may just all together go away in the future

class DBIC, is 'Reaction::Object', is 'Catalyst::Component', which {

  has  '_schema' => (isa => 'DBIx::Class::Schema', is => 'ro', required => 1);

  implements 'COMPONENT' => as {
    my ($class, $app, $args) = @_;
    my %cfg = %{ Catalyst::Utils::merge_hashes($class->config, $args) };

    my $im_class = $cfg{im_class};
    Class::MOP::load_class($im_class);

    #XXXthis could be cut out later for a more elegant method
    my @domain_models = $im_class->domain_models;
    confess "Unable to locate domain model in ${im_class}"
      if @domain_models < 1;
    confess 'ModelBase does not yet support multiple domain models'
      if @domain_models > 1;
    my $domain_model = shift @domain_models;
    my $schema_class = $domain_model->_isa_metadata;
    Class::MOP::load_class($schema_class);

    my $params = $cfg{db_params} || {};
    my $schema = $schema_class
      ->connect($cfg{db_dsn}, $cfg{db_user}, $cfg{db_password}, $params);

    return $class->new(_schema => $schema);
  };

  implements 'ACCEPT_CONTEXT' => as {
    my ($self, $ctx) = @_;
    return $self->CONTEXTUAL_CLONE($ctx) unless ref $ctx;
    return $ctx->stash->{ref($self)} ||= $self->CONTEXTUAL_CLONE($ctx);
  };

  #XXXto do build in support for RestrictByUser natively or by subclass
  implements 'CONTEXTUAL_CLONE' => as {
    my ($self, $ctx) = @_;
    my $schema = $self->_schema->clone;

    my $im_class = $self->config->{im_class};

    #XXXthis could be cut out later for a more elegant method
    my @domain_models = $im_class->domain_models;
    confess "Unable to locate domain model in ${im_class}"
      if @domain_models < 1;
    confess 'ModelBase does not yet support multiple domain models'
      if @domain_models > 1;
    my $domain_model = shift @domain_models;

    return $im_class->new($domain_model->name => $schema);
  };

};


1;

=head1 NAME

Catalyst::Model::Reaction::InterfaceModel::DBIC

=head1 DESCRIPTION

=head2 COMPONENT

=head2 ACCEPT_CONTEXT

=head2 CONTEXTUAL_CLONE

=head1 CONFIG OPTIONS

=head2 db_dsn

=head2 db_user

=head2 db_password

=head2 db_params

=head2 im_class

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut