1 package My::Schema::Result::User;
5 use base qw/DBIx::Class::Core/;
7 ### Define what our admin class is, for ensure_class_loaded()
8 my $admin_class = __PACKAGE__ . '::Admin';
10 __PACKAGE__->table('users');
12 __PACKAGE__->add_columns(
13 qw/user_id email password
14 firstname lastname active
18 __PACKAGE__->set_primary_key('user_id');
22 my $ret = $self->next::method(@_);
23 if ( $ret->admin ) { ### If this is an admin, rebless for extra functions
24 $self->ensure_class_loaded($admin_class);
25 bless $ret, $admin_class;
31 return "I am a regular user.";
34 package My::Schema::Result::User::Admin;
38 use base qw/My::Schema::Result::User/;
40 # This line is important
41 __PACKAGE__->table('users');
44 return "I am an admin.";
48 return "I am doing admin stuff";
53 use base qw/DBIx::Class::Schema/;
55 My::Schema->register_class( Admin => 'My::Schema::Result::User::Admin' );
56 My::Schema->register_class( User => 'My::Schema::Result::User' );
68 email => 'someguy@place.com',
74 email => 'someadmin@adminplace.com',
79 ok( my $schema = My::Schema->connection(DBICTest->_database) );
82 $schema->storage->dbh->do(
83 "create table users (user_id, email, password, firstname, lastname, active, admin)"
88 local $TODO = 'New objects should also be inflated';
89 my $user = $schema->resultset('User')->create($user_data);
90 my $admin = $schema->resultset('User')->create($admin_data);
92 is( ref $user, 'My::Schema::Result::User' );
93 is( ref $admin, 'My::Schema::Result::User::Admin' );
96 my $user = $schema->resultset('User')->single($user_data);
97 my $admin = $schema->resultset('User')->single($admin_data);
99 is( ref $user, 'My::Schema::Result::User' );
100 is( ref $admin, 'My::Schema::Result::User::Admin' );
102 is( $user->password, 'pass1' );
103 is( $admin->password, 'pass2' );
104 is( $user->hello, 'I am a regular user.' );
105 is( $admin->hello, 'I am an admin.' );
107 ok( !$user->can('do_admin_stuff') );
108 ok( $admin->can('do_admin_stuff') );