9 package My::Schema::Result::User;
13 use base qw/DBIx::Class::Core/;
15 ### Define what our admin class is, for ensure_class_loaded()
16 my $admin_class = __PACKAGE__ . '::Admin';
18 __PACKAGE__->table('users');
20 __PACKAGE__->add_columns(
21 qw/user_id email password
22 firstname lastname active
26 __PACKAGE__->set_primary_key('user_id');
30 my $ret = $self->next::method(@_);
31 if ( $ret->admin ) { ### If this is an admin, rebless for extra functions
32 $self->ensure_class_loaded($admin_class);
33 bless $ret, $admin_class;
39 return "I am a regular user.";
42 package My::Schema::Result::User::Admin;
46 use base qw/My::Schema::Result::User/;
48 # This line is important
49 __PACKAGE__->table('users');
52 return "I am an admin.";
56 return "I am doing admin stuff";
61 use base qw/DBIx::Class::Schema/;
63 My::Schema->register_class( Admin => 'My::Schema::Result::User::Admin' );
64 My::Schema->register_class( User => 'My::Schema::Result::User' );
70 email => 'someguy@place.com',
76 email => 'someadmin@adminplace.com',
81 ok( my $schema = My::Schema->connect(DBICTest->_database) );
84 $schema->storage->dbh->do(
85 "create table users (user_id, email, password, firstname, lastname, active, admin)"
90 local $TODO = 'New objects should also be inflated';
91 my $user = $schema->resultset('User')->create($user_data);
92 my $admin = $schema->resultset('User')->create($admin_data);
94 is( ref $user, 'My::Schema::Result::User' );
95 is( ref $admin, 'My::Schema::Result::User::Admin' );
98 my $user = $schema->resultset('User')->single($user_data);
99 my $admin = $schema->resultset('User')->single($admin_data);
101 is( ref $user, 'My::Schema::Result::User' );
102 is( ref $admin, 'My::Schema::Result::User::Admin' );
104 is( $user->password, 'pass1' );
105 is( $admin->password, 'pass2' );
106 is( $user->hello, 'I am a regular user.' );
107 is( $admin->hello, 'I am an admin.' );
109 ok( !$user->can('do_admin_stuff') );
110 ok( $admin->can('do_admin_stuff') );