Commit | Line | Data |
db4e9221 |
1 | package My::Schema::Result::User; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base qw/DBIx::Class::Core/; |
6 | |
7 | ### Define what our admin class is, for ensure_class_loaded() |
8 | my $admin_class = __PACKAGE__ . '::Admin'; |
9 | |
10 | __PACKAGE__->table('users'); |
11 | |
12 | __PACKAGE__->add_columns( |
13 | qw/user_id email password |
14 | firstname lastname active |
15 | admin/ |
16 | ); |
17 | |
18 | __PACKAGE__->set_primary_key('user_id'); |
19 | |
20 | sub inflate_result { |
21 | my $self = shift; |
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; |
26 | } |
27 | return $ret; |
28 | } |
29 | |
30 | sub hello { |
31 | return "I am a regular user."; |
32 | } |
33 | |
34 | package My::Schema::Result::User::Admin; |
35 | |
36 | use strict; |
37 | use warnings; |
38 | use base qw/My::Schema::Result::User/; |
39 | |
40 | # This line is important |
41 | __PACKAGE__->table('users'); |
42 | |
43 | sub hello { |
44 | return "I am an admin."; |
45 | } |
46 | |
47 | sub do_admin_stuff { |
48 | return "I am doing admin stuff"; |
49 | } |
50 | |
51 | package My::Schema; |
52 | |
53 | use base qw/DBIx::Class::Schema/; |
54 | |
55 | My::Schema->register_class( Admin => 'My::Schema::Result::User::Admin' ); |
56 | My::Schema->register_class( User => 'My::Schema::Result::User' ); |
57 | |
58 | 1; |
59 | |
60 | package main; |
61 | |
62 | use lib qw(t/lib); |
63 | use DBICTest; |
64 | |
65 | use Test::More; |
66 | |
67 | my $user_data = { |
68 | email => 'someguy@place.com', |
69 | password => 'pass1', |
70 | admin => 0 |
71 | }; |
72 | |
73 | my $admin_data = { |
74 | email => 'someadmin@adminplace.com', |
75 | password => 'pass2', |
76 | admin => 1 |
77 | }; |
78 | |
d9c17594 |
79 | ok( my $schema = My::Schema->connection(DBICTest->_database) ); |
db4e9221 |
80 | |
81 | ok( |
82 | $schema->storage->dbh->do( |
83 | "create table users (user_id, email, password, firstname, lastname, active, admin)" |
84 | ) |
85 | ); |
86 | |
87 | TODO: { |
88 | local $TODO = 'New objects should also be inflated'; |
d4a20f38 |
89 | my $user = $schema->resultset('User')->create($user_data); |
90 | my $admin = $schema->resultset('User')->create($admin_data); |
db4e9221 |
91 | |
92 | is( ref $user, 'My::Schema::Result::User' ); |
93 | is( ref $admin, 'My::Schema::Result::User::Admin' ); |
db4e9221 |
94 | } |
95 | |
96 | my $user = $schema->resultset('User')->single($user_data); |
97 | my $admin = $schema->resultset('User')->single($admin_data); |
98 | |
99 | is( ref $user, 'My::Schema::Result::User' ); |
100 | is( ref $admin, 'My::Schema::Result::User::Admin' ); |
101 | |
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.' ); |
106 | |
107 | ok( !$user->can('do_admin_stuff') ); |
108 | ok( $admin->can('do_admin_stuff') ); |
109 | |
110 | done_testing; |