-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made it work in roles (under Moose 2.x).
Now attributes with 'add_column' option can be defined in roles and then the role can be applied to DBIC result classes (but strictly after a ->table() call..).
- Loading branch information
Showing
4 changed files
with
112 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Test::Most; | ||
|
||
use FindBin; | ||
use Path::Class; | ||
use lib dir($FindBin::Bin)->subdir('lib')->stringify; | ||
|
||
use Moose::Util qw(apply_all_roles); | ||
|
||
# must be a BEGIN b/c the first test is in a BEGIN, too :-( | ||
BEGIN { | ||
require Moose; | ||
plan skip_all => 'Using DBIx::Class::MooseColumns in roles is not supported under Moose 1.x' | ||
if $Moose::VERSION < 1.99; | ||
} | ||
|
||
# test for the role being applied smoothly | ||
|
||
BEGIN { | ||
require TestSchema::Result::CD; | ||
lives_and { | ||
warnings_are { | ||
apply_all_roles('TestSchema::Result::CD', 'TestSchema::Role::HasTitle'); | ||
} []; | ||
} "applying the role to a result class does not throw nor warn"; | ||
} | ||
|
||
use Test::DBIx::Class; | ||
|
||
fixtures_ok 'basic', 'installed the basic fixtures from configuration files'; | ||
|
||
# tests for ->add_column() being called for an attribute defined in a role | ||
|
||
{ | ||
lives_and { | ||
cmp_deeply( | ||
Schema->resultset('CD')->result_source->column_info('title'), | ||
superhashof({ | ||
is_nullable => 1, | ||
}) | ||
); | ||
} "column_info of 'title' contains ('is_nullable' => 1)"; | ||
} | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package TestSchema::Result::CD; | ||
|
||
use Moose; | ||
use MooseX::NonMoose; | ||
use namespace::autoclean; | ||
|
||
extends 'DBIx::Class::Core'; | ||
|
||
__PACKAGE__->table('cd'); | ||
|
||
__PACKAGE__->add_column('cd_id'); | ||
|
||
# this class is intentionally left immutable (so that we can apply the role | ||
# later) | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package TestSchema::Role::HasTitle; | ||
|
||
use Moose::Role; | ||
use namespace::autoclean; | ||
|
||
use DBIx::Class::MooseColumns; | ||
|
||
# used for testing if the attribute works on the class this role was applied to | ||
has title => ( | ||
isa => 'Maybe[Str]', | ||
is => 'rw', | ||
add_column => { | ||
is_nullable => 1, | ||
}, | ||
); | ||
|
||
1; |