fix code running 2x in dynamic schema_base_class
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / RelBuilder.pm
index 9b75e80..43810ac 100644 (file)
@@ -6,7 +6,7 @@ use base 'Class::Accessor::Grouped';
 use mro 'c3';
 use Carp::Clan qw/^DBIx::Class/;
 use Scalar::Util 'weaken';
-use DBIx::Class::Schema::Loader::Utils qw/split_name slurp_file/;
+use DBIx::Class::Schema::Loader::Utils qw/split_name slurp_file array_eq/;
 use Try::Tiny;
 use List::MoreUtils qw/apply uniq any/;
 use namespace::clean;
@@ -17,7 +17,7 @@ use String::ToIdentifier::EN::Unicode ();
 use Class::Unload ();
 use Class::Inspector ();
 
-our $VERSION = '0.07010';
+our $VERSION = '0.07011';
 
 # Glossary:
 #
@@ -268,15 +268,12 @@ sub _relationship_attrs {
     return \%composite;
 }
 
-sub _array_eq {
-    my ($self, $a, $b) = @_;
+sub _strip_id_postfix {
+    my ($self, $name) = @_;
 
-    return unless @$a == @$b;
+    $name =~ s/_?(?:id|ref|cd|code|num)\z//i;
 
-    for (my $i = 0; $i < @$a; $i++) {
-        return unless $a->[$i] eq $b->[$i];
-    }
-    return 1;
+    return $name;
 }
 
 sub _remote_attrs {
@@ -296,7 +293,9 @@ sub _remote_attrs {
 sub _sanitize_name {
     my ($self, $name) = @_;
 
-    $name =~ s/\W+/_/g;
+    $name = $self->loader->_to_identifier('relationships', $name, '_');
+
+    $name =~ s/\W+/_/g; # if naming >= 8 to_identifier takes care of it
 
     return $name;
 }
@@ -306,7 +305,7 @@ sub _normalize_name {
 
     $name = $self->_sanitize_name($name);
 
-    my @words = split_name $name;
+    my @words = split_name $name, $self->loader->_get_naming_v('relationships');
 
     return join '_', map lc, @words;
 }
@@ -319,8 +318,7 @@ sub _remote_relname {
     # name, to make filter accessors work, but strip trailing _id
     if(scalar keys %{$cond} == 1) {
         my ($col) = values %{$cond};
-        $col = $self->_normalize_name($col);
-        $col =~ s/_id$//;
+        $col = $self->_strip_id_postfix($self->_normalize_name($col));
         ($remote_relname) = $self->_inflect_singular($col);
     }
     else {
@@ -676,8 +674,8 @@ sub _relnames_and_method {
     my $remote_method = 'has_many';
 
     # If the local columns have a UNIQUE constraint, this is a one-to-one rel
-    if ($self->_array_eq([ $local_source->primary_columns ], $local_cols) ||
-            grep { $self->_array_eq($_->[1], $local_cols) } @$uniqs) {
+    if (array_eq([ $local_source->primary_columns ], $local_cols) ||
+            grep { array_eq($_->[1], $local_cols) } @$uniqs) {
         $remote_method   = 'might_have';
         ($local_relname) = $self->_inflect_singular($local_relname_uninflected);
     }
@@ -707,7 +705,7 @@ sub _relnames_and_method {
                 my $rel_cols = [ sort { $a cmp $b } apply { s/^foreign\.//i }
                     (keys %{ $class->relationship_info($local_relname)->{cond} }) ];
 
-                $relationship_exists = 1 if $self->_array_eq([ sort @$local_cols ], $rel_cols);
+                $relationship_exists = 1 if array_eq([ sort @$local_cols ], $rel_cols);
             }
         }
 
@@ -715,8 +713,7 @@ sub _relnames_and_method {
             my $colnames = q{_} . $self->_normalize_name(join '_', @$local_cols);
             $remote_relname .= $colnames if keys %$cond > 1;
 
-            $local_relname = $self->_normalize_name($local_table . $colnames);
-            $local_relname =~ s/_id$//;
+            $local_relname = $self->_strip_id_postfix($self->_normalize_name($local_table . $colnames));
 
             $local_relname_uninflected = $local_relname;
             ($local_relname) = $self->_inflect_plural($local_relname);