[Rt-commit] rt branch, 4.0/loadable-object-for-attribute-initialdata, created. rt-4.0.19-62-g079b8d9

Todd Wade todd at bestpractical.com
Wed Apr 9 06:45:43 EDT 2014


The branch, 4.0/loadable-object-for-attribute-initialdata has been created
        at  079b8d9dfc95dab0b2bedc017009562b60b50304 (commit)

- Log -----------------------------------------------------------------
commit 079b8d9dfc95dab0b2bedc017009562b60b50304
Author: Todd Wade <todd at bestpractical.com>
Date:   Thu Apr 3 09:19:16 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 11069df..b50bca1 100644
--- a/docs/initialdata.pod
+++ b/docs/initialdata.pod
@@ -409,8 +409,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 e6ecdda..1bac679 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -1105,7 +1105,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 ) {

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


More information about the rt-commit mailing list