[Rt-commit] rt branch, 4.2/loadable-object-for-attribute-initialdata, created. rt-4.2.6-36-ge7290b0

Todd Wade todd at bestpractical.com
Mon Aug 11 11:11:16 EDT 2014


The branch, 4.2/loadable-object-for-attribute-initialdata has been created
        at  e7290b0e29d1958509b5205b8fcef6eb6af528b8 (commit)

- Log -----------------------------------------------------------------
commit e7290b0e29d1958509b5205b8fcef6eb6af528b8
Author: Todd Wade <todd at bestpractical.com>
Date:   Mon Aug 11 11:09:43 2014 -0400

    add a way to give an attribute an ObjectType besides RT::System
    
    This code allows you to provide a code ref in the Object field in an Attribute
    initialdata hash that returns an object that can be used as the ObjectType for
    the Attribute:
    
    @Attributes = ({
        Name        => 'SavedSearch',
        Description => 'New Tickets in SomeQueue',
        Object      => sub {
            my $GroupName = 'SomeQueue Group';
            my $group     = RT::Group->new( RT->SystemUser );
    
            my( $ret, $msg ) = $group->LoadUserDefinedGroup( $GroupName );
            die $msg unless $ret;
    
            return $group;
        },
        Content     => {
            Format =>  <<'        END_OF_FORMAT',
    ....
            END_OF_FORMAT
            Query   => "Status = 'new' AND Queue = 'SomeQueue'",
            OrderBy => 'id',
            Order   => 'DESC'
        },
    });

diff --git a/docs/initialdata.pod b/docs/initialdata.pod
index 8e510dc..ac55dcf 100644
--- a/docs/initialdata.pod
+++ b/docs/initialdata.pod
@@ -427,8 +427,33 @@ L<RT::Template/Create> for the fields you can use.
 
 An array of L<RT::Attribute>s to create.  You likely don't need to mess with
 this.  If you do, know that the key C<Object> is expected to be an
-L<RT::Record> object on which to call C<AddAttribute>.  If you don't provide
-C<Object> or it's undefined, C<< RT->System >> will be used.
+L<RT::Record> object or a subroutine reference that returns an object on which
+to call C<AddAttribute>.  If you don't provide C<Object> or it's undefined,
+C<< RT->System >> will be used.
+
+Here is an example of using a subroutine reference as a value for Object:
+
+    @Attributes = ({
+        Name        => 'SavedSearch',
+        Description => 'New Tickets in SomeQueue',
+        Object      => sub {
+            my $GroupName = 'SomeQueue Group';
+            my $group     = RT::Group->new( RT->SystemUser );
+    
+            my( $ret, $msg ) = $group->LoadUserDefinedGroup( $GroupName );
+            die $msg unless $ret;
+    
+            return $group;
+        },
+        Content     => {
+            Format =>  <<'        END_OF_FORMAT',
+    ....
+            END_OF_FORMAT
+            Query   => "Status = 'new' AND Queue = 'SomeQueue'",
+            OrderBy => 'id',
+            Order   => 'DESC'
+        },
+    });
 
 =head2 C<@Initial>
 
diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index 5fc36f6..29802ad 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -1208,7 +1208,12 @@ sub InsertData {
         my $sys = RT::System->new(RT->SystemUser);
 
         for my $item (@Attributes) {
-            my $obj = delete $item->{Object}; # XXX: make this something loadable
+            my $obj = delete $item->{Object};
+
+            if ( ref $obj eq 'CODE' ) {
+                $obj = $obj->();
+            }
+
             $obj ||= $sys;
             my ( $return, $msg ) = $obj->AddAttribute (%$item);
             unless ( $return ) {
diff --git a/t/data/initialdata/initialdata b/t/data/initialdata/initialdata
index 4d45a4e..fb89479 100644
--- a/t/data/initialdata/initialdata
+++ b/t/data/initialdata/initialdata
@@ -69,3 +69,22 @@
       Right         => 'SeeCustomField',
     },
 );
+
+ at Attributes = ({
+    Name        => 'SavedSearch',
+    Description => 'New Tickets in Test Queue',
+    Object      => sub {
+        my $GroupName = 'Test Employees';
+        my $group     = RT::Group->new( RT->SystemUser );
+
+        my( $ret, $msg ) = $group->LoadUserDefinedGroup( $GroupName );
+        die $msg unless $ret;
+
+        return $group;
+    },
+    Content     => {
+        Query   => "Status = 'new' AND Queue = 'Test Queue'",
+        OrderBy => 'id',
+        Order   => 'DESC'
+    },
+});

-----------------------------------------------------------------------


More information about the rt-commit mailing list