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