[Rt-commit] r2252 - in rtfm/branches/2.1-TESTING: . lib/RT/FM t

jesse at bestpractical.com jesse at bestpractical.com
Wed Feb 23 03:04:18 EST 2005


Author: jesse
Date: Wed Feb 23 03:04:17 2005
New Revision: 2252

Modified:
   rtfm/branches/2.1-TESTING/   (props changed)
   rtfm/branches/2.1-TESTING/lib/RT/FM/Article_Overlay.pm
   rtfm/branches/2.1-TESTING/lib/RT/FM/Class_Overlay.pm
   rtfm/branches/2.1-TESTING/t/2basic_api.t
Log:
 r5759 at hualien:  jesse | 2005-02-23 03:01:39 -0500
 basic api-level custom field functionality works


Modified: rtfm/branches/2.1-TESTING/lib/RT/FM/Article_Overlay.pm
==============================================================================
--- rtfm/branches/2.1-TESTING/lib/RT/FM/Article_Overlay.pm	(original)
+++ rtfm/branches/2.1-TESTING/lib/RT/FM/Article_Overlay.pm	Wed Feb 23 03:04:17 2005
@@ -25,9 +25,14 @@
 use RT::FM::ObjectTopicCollection;
 use RT::FM::ClassCollection;
 use RT::Links;
+use RT::CustomFields;
 use RT::URI::fsck_com_rtfm;
 use RT::Transactions;
 
+# This object takes custom fields
+
+RT::CustomField->_ForObjectType( _LookupTypes() => 'RTFM Articles' );    #loc
+
 # {{{ Create
 
 =item Create PARAMHASH
@@ -909,69 +914,6 @@
 
 # }}}
 
-# {{{ _NewTransaction
-
-=head2 _NewTransaction PARAMHASH
-
-
-Takes a hash of:
-
-Type
-Field
-OldContent
-NewContent
-Data 
-
-
-=cut
-
-sub _NewTransaction {
-    my $self = shift;
-    my %args = ( Type       => undef,
-                 Field      => '',
-                 OldContent => '',
-                 NewContent => '',
-                 ChangeLog  => '',
-                 @_ );
-
-    my $trans = RT::Transaction->new( $self->CurrentUser );
-    my ($id,$msg) = $trans->Create( ObjectId    => $self->id,
-                    ObjectType => ref($self),
-                    Type       => $args{'Type'},
-                    Field      => $args{'Field'},
-                    OldContent => $args{'OldContent'},
-                    NewContent => $args{'NewContent'});
-
-    #something bad happened;
-    unless ( $id ) {
-        $RT::Logger->crit( $self . " could not create a transaction for " . %args );
-        return ( undef, $msg, $trans );
-    }
-
-    return ( $trans->id, $self->loc("Transaction recorded"), $trans );
-}
-
-# }}}
-
-=head2 Transactions
-
-Returns an RT::FM::TransactionCollection pre-loaded with all the transactions for tthis Article. If the current user doesn't have the right to 'ShowArticleHistory',
-this object is an _empty_ TransactionCollection
-
-=cut
-
-sub Transactions {
-    my $self         = shift;
-    my $transactions = RT::Transactions->new( $self->CurrentUser );
-
-    if ( $self->CurrentUserHasRight('ShowArticleHistory') ) {
-         $transactions->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article');
-         $transactions->Limit( FIELD    => 'ObjectId', OPERATOR => '=', VALUE    => $self->Id );
-    }
-
-    return ($transactions);
-
-}
 
 # {{{ _Set
 
@@ -1038,4 +980,16 @@
 
 # }}}
 
+sub _LookupTypes {
+    "RT::FM::Class-RT::FM::Article";
+}
+
+# _LookupId is the id of the toplevel type object the customfield is joined to
+# in this case, that's an RT::FM::Class.
+
+sub _LookupId {
+    my $self = shift;
+    return $self->ClassObj->id;
+
+}
 1;

Modified: rtfm/branches/2.1-TESTING/lib/RT/FM/Class_Overlay.pm
==============================================================================
--- rtfm/branches/2.1-TESTING/lib/RT/FM/Class_Overlay.pm	(original)
+++ rtfm/branches/2.1-TESTING/lib/RT/FM/Class_Overlay.pm	Wed Feb 23 03:04:17 2005
@@ -1,19 +1,19 @@
 # BEGIN LICENSE BLOCK
-# 
+#
 #  Copyright (c) 2002-2003 Jesse Vincent <jesse at bestpractical.com>
-#  
+#
 #  This program is free software; you can redistribute it and/or modify
-#  it under the terms of version 2 of the GNU General Public License 
+#  it under the terms of version 2 of the GNU General Public License
 #  as published by the Free Software Foundation.
-# 
+#
 #  A copy of that license should have arrived with this
 #  software, but in any event can be snarfed from www.gnu.org.
-# 
+#
 #  This program is distributed in the hope that it will be useful,
 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 #  GNU General Public License for more details.
-# 
+#
 # END LICENSE BLOCK
 
 package RT::FM::Class;
@@ -21,10 +21,10 @@
 no warnings qw/redefine/;
 use strict;
 
-
 use RT::FM::System;
 use RT::CustomFields;
 use RT::ACL;
+use RT::FM::ArticleCollection;
 
 
 =head2 Load IDENTIFIER
@@ -35,33 +35,32 @@
 
 sub Load {
     my $self = shift;
-    my $id = shift;
+    my $id   = shift;
 
-    if ($id =~/^\d+$/) {
+    if ( $id =~ /^\d+$/ ) {
         $self->SUPER::Load($id);
     }
     else {
-        $self->LoadByCols(Name => $id);
+        $self->LoadByCols( Name => $id );
     }
 }
 
-
-
 # {{{ This object provides ACLs
 
 use vars qw/$RIGHTS/;
 $RIGHTS = {
 
-    SeeClass            => 'See that this class exists', #loc_pair
-    CreateArticle       => 'Create articles in this class', #loc_pair
-    ShowArticle         => 'See articles in this class', #loc_pair
-    ShowArticleHistory  => 'See articles in this class', #loc_pair
-    ModifyArticle       => 'Modify or delete articles in this class', #loc_pair
+    SeeClass            => 'See that this class exists',               #loc_pair
+    CreateArticle       => 'Create articles in this class',            #loc_pair
+    ShowArticle         => 'See articles in this class',               #loc_pair
+    ShowArticleHistory  => 'See articles in this class',               #loc_pair
+    ModifyArticle       => 'Modify or delete articles in this class',  #loc_pair
     ModifyArticleTopics => 'Modify topics for articles in this class', #loc_pair
-    AdminClass          => 'Modify metadata and custom fields for this class', #loc_pair
-    AdminTopics         => 'Modify topic hierarchy associated with this class', #loc_pair
-    ShowACL             => 'Display Access Control List', # loc_pair
-    ModifyACL           => 'Modify Access Control List', # loc_pair
+    AdminClass  => 'Modify metadata and custom fields for this class', #loc_pair
+    AdminTopics =>
+      'Modify topic hierarchy associated with this class',             #loc_pair
+    ShowACL   => 'Display Access Control List',    # loc_pair
+    ModifyACL => 'Modify Access Control List',     # loc_pair
 };
 
 # TODO: This should be refactored out into an RT::ACLedObject or something
@@ -69,13 +68,11 @@
 
 # Tell RT::ACE that this sort of object can get acls granted
 $RT::ACE::OBJECT_TYPES{'RT::FM::Class'} = 1;
- 
 
 foreach my $right ( keys %{$RIGHTS} ) {
     $RT::ACE::LOWERCASERIGHTNAMES{ lc $right } = $right;
 }
 
-
 =head2 AvailableRights
 
 Returns a hash of available rights for this object. The keys are the right names and the values are a description of what t
@@ -85,16 +82,11 @@
 
 sub AvailableRights {
     my $self = shift;
-    return($RIGHTS);
+    return ($RIGHTS);
 }
 
 # }}}
 
-# This object takes custom fields
-
-RT::CustomField->_ForObjectType(_LookupTypes() => 'RTFM Articles'); #loc
-
-
 
 # {{{ Create
 
@@ -161,40 +153,46 @@
 
 =cut
 
-
 sub Create {
     my $self = shift;
-    my %args = ( 
-                Name => '',
-                Description => '',
-                SortOrder => '0',
-    @_);
+    my %args = (
+        Name        => '',
+        Description => '',
+        SortOrder   => '0',
+        @_
+    );
 
-    unless ($self->CurrentUser->HasRight(Right => 'AdminClass', Object => $RT::FM::System) ) {
-        return(0, $self->loc('Permission Denied'));
+    unless (
+        $self->CurrentUser->HasRight(
+            Right  => 'AdminClass',
+            Object => $RT::FM::System
+        )
+      )
+    {
+        return ( 0, $self->loc('Permission Denied') );
     }
 
     $self->SUPER::Create(
-                         Name => $args{'Name'},
-                         Description => $args{'Description'},
-                         SortOrder => $args{'SortOrder'},
+        Name        => $args{'Name'},
+        Description => $args{'Description'},
+        SortOrder   => $args{'SortOrder'},
     );
 
 }
 
 sub ValidateName {
-    my $self = shift;
+    my $self   = shift;
     my $newval = shift;
 
     return undef unless ($newval);
     my $obj = RT::FM::Class->new($RT::SystemUser);
     $obj->Load($newval);
-    return undef if ($obj->Id);
-    return 1;     
+    return undef if ( $obj->Id );
+    return 1;
 
 }
-# }}}
 
+# }}}
 
 # }}}
 
@@ -227,19 +225,29 @@
 # }}}
 
 sub CurrentUserHasRight {
-    my $self = shift;
+    my $self  = shift;
     my $right = shift;
 
-    return ($self->CurrentUser->HasRight( Right => $right,
-                                          Object => $self, 
-                                          EquivObjects => [$RT::System, $RT::FM::System] ));
+    return (
+        $self->CurrentUser->HasRight(
+            Right        => $right,
+            Object       => $self,
+            EquivObjects => [ $RT::System, $RT::FM::System ]
+        )
+    );
 
 }
 
-sub _LookupTypes {
-    "RT::FM::Class-RT::FM::Article";
-}
+sub ArticleCustomFields {
+    my $self = shift;
 
+    my $cfs = RT::CustomFields->new( $self->CurrentUser );
+    if ( $self->CurrentUserHasRight('SeeClass') ) {
+        $cfs->LimitToGlobalOrObjectId( $self->Id );
+        $cfs->LimitToLookupType( RT::FM::Article->_LookupTypes );
+    }
+    return ($cfs);
+}
 
 1;
 

Modified: rtfm/branches/2.1-TESTING/t/2basic_api.t
==============================================================================
--- rtfm/branches/2.1-TESTING/t/2basic_api.t	(original)
+++ rtfm/branches/2.1-TESTING/t/2basic_api.t	Wed Feb 23 03:04:17 2005
@@ -40,7 +40,7 @@
 
 ($id,$msg) = $cf->Create( Name => 'FM::Sample-'.$$,
              Description => 'Test text cf',
-             LookupType => RT::FM::Class->_LookupTypes,
+             LookupType => RT::FM::Article->_LookupTypes,
              Type => 'Text'
              );
 
@@ -55,7 +55,7 @@
 
 # Does our class have a custom field?
 
-my $cfs = $class->CustomFields;
+my $cfs = $class->ArticleCustomFields;
 isa_ok($cfs, 'RT::CustomFields');
 is($cfs->Count, 1, "We only have one custom field");
 my $found_cf = $cfs->First;
@@ -63,7 +63,7 @@
 
 ($id,$msg) = $cf->RemoveFromObject($class);
 
-is($class->CustomFields->Count, 0, "All gone!");
+is($class->ArticleCustomFields->Count, 0, "All gone!");
 
 # Put it back. we want to go forward.
 
@@ -93,5 +93,25 @@
 is ($txn->Type, 'Create', "It's a create!");
 
 
-# Test some custom fields
+my $art_cfs = $art->CustomFields();
+is ($art_cfs->Count, 1, "It has a custom field");
+my $values = $art->CustomFieldValues($art_cfs->First);
+is ($values->Count, 0);
+
+$art->AddCustomFieldValue(Field => $cf->id, Value => 'Testing');
+$values = $art->CustomFieldValues($art_cfs->First->id);
+
+# We added the custom field
+is ($values->Count, 1);
+is ($values->First->Content, 'Testing', "We added the CF");
+
+is ($art->Transactions->Count,2, "We added a second transaction for the custom field addition");
+my $txn2 = $art->Transactions->Last;
+is ($txn2->ObjectId, $art->id);
+is ($txn2->id, ($txn->id +1));
+is ($txn2->Type, 'CustomField');
+is($txn2->NewValue, 'Testing');
+is ($txn2->OldValue, '');
+
+
 


More information about the Rt-commit mailing list