File Coverage

blib/lib/DBIx/Class/Helper/Schema/Verifier/RelationshipColumnName.pm
Criterion Covered Total %
statement 26 26 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 38 38 100.0


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::Schema::Verifier::RelationshipColumnName;
2             $DBIx::Class::Helper::Schema::Verifier::RelationshipColumnName::VERSION = '2.034002';
3             # ABSTRACT: Verify that relationships and column names are distinct
4              
5 1     1   83620 use strict;
  1         9  
  1         24  
6 1     1   5 use warnings;
  1         1  
  1         21  
7              
8 1     1   390 use MRO::Compat;
  1         1410  
  1         26  
9 1     1   6 use mro 'c3';
  1         1  
  1         4  
10              
11 1     1   23 use base 'DBIx::Class::Helper::Schema::Verifier';
  1         2  
  1         433  
12              
13             sub result_verifiers {
14             (
15             sub {
16 3     3   13 my ($s, $result) = @_;
17              
18 3         15 my @columns = $result->columns;
19 3         17 my %relationships = map { $_ => 1 } $result->relationships;
  9         28  
20              
21 3         6 my @mistakes = grep { $relationships{$_} } @columns;
  9         16  
22              
23              
24 3         7 my $exp = 'See DBIx::Class::Helper::Schema::Verifier::RelationshipColumnName for more details';
25 3 100       11 if (@mistakes == 1) {
    100          
26 1         12 die "$result has a relationship name that is the same as a column name: @mistakes, $exp"
27             } elsif (@mistakes) {
28 1         11 die "$result has relationship names that are the same as column names: @mistakes, $exp"
29             }
30             },
31             shift->next::method,
32             )
33 3     3 1 1411 }
34              
35             1;
36              
37             __END__
38              
39             =pod
40              
41             =head1 NAME
42              
43             DBIx::Class::Helper::Schema::Verifier::RelationshipColumnName - Verify that relationships and column names are distinct
44              
45             =head1 SYNOPSIS
46              
47             package MyApp::Schema;
48              
49             __PACKAGE__->load_components('Helper::Schema::Verifier::RelationshipColumnName');
50              
51             =head1 DESCRIPTION
52              
53             C<DBIx::Class::Helper::Schema::Verifier::RelationshipColumnName> verifies that
54             none of your columns have the same name as a relationship. If you create a
55             relationship that has the same name as a column, to access the column you will
56             be forced to use C<get_column>, additionally it is just confusing having them
57             be the same name. What I tend to do is define the columns to be something like
58             C<user_id> and have the relationship then be simply C<user>.
59              
60             =head1 AUTHOR
61              
62             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
63              
64             =head1 COPYRIGHT AND LICENSE
65              
66             This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
67              
68             This is free software; you can redistribute it and/or modify it under
69             the same terms as the Perl 5 programming language system itself.
70              
71             =cut