use warnings;
use strict;
+use Carp qw/ confess /;
our $VERSION = '0.11';
package MyApp::Schema;
- __PACKAGE__->mk_group_accessors('simple' => qw/current_user_id/);
+ __PACKAGE__->mk_group_accessors('simple' => qw/current_user/);
package MyApp::Model::MyAppDB;
use namespace::autoclean;
extends 'Catalyst::Model::DBIC::Schema';
- with 'Catalyst::Component::InstancePerContext';
-
- sub build_per_context_instance {
- my ($meth, $self) = (shift, shift);
- my ($c) = @_; # There are other params but we dont care about them
- my $new = bless({ %$self }, ref($self));
- my $user_info = $c->_user_in_session;
- $new->schema($new->schema->clone);
- my $user = $new->schema->resultset('User')->new_result({ %$user_info });
- $new->schema->current_user_id($user->id) if (defined $user_info);
- return $new;
- }
+ __PACKAGE->config(
+ traits => ['WithCurrentUser'], # Requires Catalyst::TraitFor::Model::DBIC::Schema::WithCurrentUser
+ );
package MyApp::Schema::SomeTable;
=head2 get_current_user_id
-This method is meant to be overridden. The default is to return a
-schema accessor called current_user_id which should be populated as such.
+This method is meant to be overridden. The default is to look for a
+'current_user' accessor on the schema, and call an ->id method
+on it.
=cut
-sub get_current_user_id { shift->result_source->schema->current_user_id }
+
+sub get_current_user_id {
+ my $self = shift;
+ my $schema = $self->result_source->schema;
+ if ($schema->can('current_user')) {
+ return $schema->current_user->id
+ if $schema->current_user;
+ }
+ elsif ($schema->can('current_user_id')) {
+ return $schema->current_user_id;
+ }
+ else {
+ confess("Your schema $schema does not have either a 'current_user' or 'current_user_id' accessors needed by " . __PACKAGE__);
+ }
+}
=head1 AUTHOR