File: inflate_result.t

package info (click to toggle)
libdbix-class-perl 0.082844-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (112 lines) | stat: -rw-r--r-- 2,448 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use warnings;
use strict;

use Test::More;

use lib qw(t/lib);
use DBICTest;

package My::Schema::Result::User;

use strict;
use warnings;
use base qw/DBIx::Class::Core/;

### Define what our admin class is, for ensure_class_loaded()
my $admin_class = __PACKAGE__ . '::Admin';

__PACKAGE__->table('users');

__PACKAGE__->add_columns(
  user_id => { retrieve_on_insert => 1 },
  qw( email password firstname lastname active admin ),
);

__PACKAGE__->set_primary_key('user_id');

sub inflate_result {
    my $self = shift;
    my $ret  = $self->next::method(@_);
    if ( $ret->admin ) {    ### If this is an admin, rebless for extra functions
        $self->ensure_class_loaded($admin_class);
        bless $ret, $admin_class;
    }
    return $ret;
}

sub hello {
    return "I am a regular user.";
}

package My::Schema::Result::User::Admin;

use strict;
use warnings;
use base qw/My::Schema::Result::User/;

# This line is important
__PACKAGE__->table('users');

sub hello {
    return "I am an admin.";
}

sub do_admin_stuff {
    return "I am doing admin stuff";
}

package My::Schema;

use base qw/DBIx::Class::Schema/;

My::Schema->register_class( Admin => 'My::Schema::Result::User::Admin' );
My::Schema->register_class( User  => 'My::Schema::Result::User' );

1;

package main;
my $user_data = {
    email    => 'someguy@place.com',
    password => 'pass1',
    admin    => 0
};

my $admin_data = {
    email    => 'someadmin@adminplace.com',
    password => 'pass2',
    admin    => 1
};

ok( my $schema = My::Schema->connect(DBICTest->_database) );

ok(
    $schema->storage->dbh->do(
"create table users (user_id, email, password, firstname, lastname, active,  admin)"
    )
);

{
  my $user  = $schema->resultset('User')->create($user_data);
  my $admin = $schema->resultset('User')->create($admin_data);

  is( ref $user,  'My::Schema::Result::User' );

  local $TODO = 'New objects should also be inflated';
  is( ref $admin, 'My::Schema::Result::User::Admin' );
}

my $user  = $schema->resultset('User')->single($user_data);
my $admin = $schema->resultset('User')->single($admin_data);

is( ref $user,  'My::Schema::Result::User' );
is( ref $admin, 'My::Schema::Result::User::Admin' );

is( $user->password,  'pass1' );
is( $admin->password, 'pass2' );
is( $user->hello,     'I am a regular user.' );
is( $admin->hello,    'I am an admin.' );

ok( !$user->can('do_admin_stuff') );
ok( $admin->can('do_admin_stuff') );

done_testing;