Correctly select the root source of the inner part of a complex prefetch
[dbsrgits/DBIx-Class.git] / t / row / inflate_result.t
CommitLineData
4bea1fe7 1use warnings;
2use strict;
3
4use Test::More;
5
6use lib qw(t/lib);
7use DBICTest;
8
db4e9221 9package My::Schema::Result::User;
10
11use strict;
12use warnings;
13use base qw/DBIx::Class::Core/;
14
15### Define what our admin class is, for ensure_class_loaded()
16my $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
28sub 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
38sub hello {
39 return "I am a regular user.";
40}
41
42package My::Schema::Result::User::Admin;
43
44use strict;
45use warnings;
46use base qw/My::Schema::Result::User/;
47
48# This line is important
49__PACKAGE__->table('users');
50
51sub hello {
52 return "I am an admin.";
53}
54
55sub do_admin_stuff {
56 return "I am doing admin stuff";
57}
58
59package My::Schema;
60
61use base qw/DBIx::Class::Schema/;
62
63My::Schema->register_class( Admin => 'My::Schema::Result::User::Admin' );
64My::Schema->register_class( User => 'My::Schema::Result::User' );
65
661;
67
68package main;
db4e9221 69my $user_data = {
70 email => 'someguy@place.com',
71 password => 'pass1',
72 admin => 0
73};
74
75my $admin_data = {
76 email => 'someadmin@adminplace.com',
77 password => 'pass2',
78 admin => 1
79};
80
d9c17594 81ok( my $schema = My::Schema->connection(DBICTest->_database) );
db4e9221 82
83ok(
84 $schema->storage->dbh->do(
85"create table users (user_id, email, password, firstname, lastname, active, admin)"
86 )
87);
88
89TODO: {
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
98my $user = $schema->resultset('User')->single($user_data);
99my $admin = $schema->resultset('User')->single($admin_data);
100
101is( ref $user, 'My::Schema::Result::User' );
102is( ref $admin, 'My::Schema::Result::User::Admin' );
103
104is( $user->password, 'pass1' );
105is( $admin->password, 'pass2' );
106is( $user->hello, 'I am a regular user.' );
107is( $admin->hello, 'I am an admin.' );
108
109ok( !$user->can('do_admin_stuff') );
110ok( $admin->can('do_admin_stuff') );
111
112done_testing;