-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPerRequestSchema.pm
85 lines (56 loc) · 2.01 KB
/
PerRequestSchema.pm
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
package Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema;
use Moose::Role;
use namespace::autoclean;
with 'Catalyst::Component::InstancePerContext';
=head1 NAME
Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema - Clone the schema
with attributes for each requests
=head1 SYNOPSIS
__PACKAGE__->config({
traits => ['PerRequestSchema'],
});
sub per_request_schema_attributes {
my ($self, $c) = @_;
return (restricting_object => $c->user->obj);
}
### OR ###
sub per_request_schema {
my ($self, $c) = @_;
return $self->schema->schema_method($c->user->obj)
}
=head1 DESCRIPTION
Clones the schema for each new request with the attributes retrieved from your
C<per_request_schema_attributes> method, which you must implement. This method
is passed the context.
Alternatively, you could also override the C<per_request_schema> method if you
need access to the schema clone and/or need to separate out the Model/Schema
methods. (See examples above and the defaults in the code.)
=cut
sub build_per_context_instance {
my ( $self, $ctx ) = @_;
return $self unless blessed($ctx);
my $new = bless {%$self}, ref $self;
$new->schema($new->per_request_schema($ctx));
return $new;
}
# Thanks to Matt Trout for this idea
sub per_request_schema {
my ($self, $c) = @_;
return $self->schema->clone($self->per_request_schema_attributes($c));
}
### TODO: This should probably be more elegant ###
sub per_request_schema_attributes {
confess "Either per_request_schema_attributes needs to be created, or per_request_schema needs to be overridden!";
}
=head1 SEE ALSO
L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class::Schema>
=head1 AUTHOR
See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
=head1 COPYRIGHT
See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
=head1 LICENSE
This program is free software, you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;