[Rt-commit] rt 01/05: Allow articles with same name in different classes

Steven Burr steve at bestpractical.com
Fri Jul 9 11:54:20 EDT 2021


This is an automated email from the git hooks/post-receive script.

jbrandt pushed a commit to branch 5.0/articles-unique-only-within-own-class2
in repository rt.

commit 3f4dd664d2eff4e1d0910ce547867a50ef339b7b
Author: Steven Burr <steve at bestpractical.com>
AuthorDate: Thu May 20 11:47:22 2021 -0400

    Allow articles with same name in different classes
    
    Previously, Article names had to be globally unique. This change
    allows articles of the same name to exist as long as they belong to
    different classes.
    
    Note that the RT::Article::ValidateName() method is changed to allow
    a class id to be passed as a second parameter, thereby altering the
    behavior of the uniqueness check. If the RT::Article::Create()
    method was called, it will always use the scoped uniqueness logic. If
    the object was populated another way e.g. Load() then the caller is
    responsible for supplying the second argument herself if that is the
    desired behavior.
    
    Fixes: I#37102
---
 lib/RT/Article.pm | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/lib/RT/Article.pm b/lib/RT/Article.pm
index dc24c9ba70..4ba5bb83eb 100644
--- a/lib/RT/Article.pm
+++ b/lib/RT/Article.pm
@@ -117,8 +117,13 @@ sub Create {
 
     return ( undef, $self->loc('Name is required') ) unless $args{Name};
 
+    # Explicitly store the class as object data because ValidateName is run
+    # via DBIx::SearchBuilder and at this point in create, the
+    # object doesn't exist in the DB yet, so ->ClassObj doesn't get the class
+    $self->{'_creating_class'} = $class->id;
+
     return ( undef, $self->loc('Name in use') )
-      unless $self->ValidateName( $args{'Name'} );
+      unless $self->ValidateName( $args{'Name'}, $class->id );
 
     $RT::Handle->BeginTransaction();
     my ( $id, $msg ) = $self->SUPER::Create(
@@ -233,30 +238,36 @@ sub Create {
 
 =head2 ValidateName NAME
 
-Takes a string name. Returns true if that name isn't in use by another article
-
-Empty names are not permitted.
+Takes a name (string, required) and an optional class id. Returns true if that
+name is not in use by another article of that class.
 
+If no class is supplied and the class can't be derived from the article
+object, returns true if that name isn't used by any other article at all.
 
 =cut
 
 sub ValidateName {
     my $self = shift;
     my $name = shift;
+    my $class_id = shift || ($self->ClassObj && $self->ClassObj->id) || $self->{'_creating_class'};
 
     if ( !$name ) {
         return (0);
     }
 
-    my $temp = RT::Article->new($RT::SystemUser);
-    $temp->LoadByCols( Name => $name );
-    if ( $temp->id && 
-         (!$self->id || ($temp->id != $self->id ))) {
+    my $article = RT::Article->new( RT->SystemUser );
+    if ( $class_id ) {
+        $article->LoadByCols( Name => $name, Class => $class_id );
+    }
+    else {
+        $article->LoadByCols( Name => $name );
+    }
+
+    if ( $article->id && ( !$self->id || ($article->id != $self->id )) ) {
         return (undef);
     }
 
     return (1);
-
 }
 
 # }}}

-- 
To stop receiving notification emails like this one, please contact
sysadmin at bestpractical.com.


More information about the rt-commit mailing list