[Rt-commit] r6082 - in RT-Extension-TicketAging: . t
schwern at bestpractical.com
schwern at bestpractical.com
Thu Sep 28 01:26:57 EDT 2006
Author: schwern
Date: Thu Sep 28 01:26:56 2006
New Revision: 6082
Modified:
RT-Extension-TicketAging/ (props changed)
RT-Extension-TicketAging/lib/RT/Extension/TicketAging.pm
RT-Extension-TicketAging/t/06.ticket-aging-map.t
Log:
r19405 at Master-Windhund-IV: schwern | 2006-09-27 22:21:22 -0700
Turn on strict and warnings.
Fix various variable typos.
Upcase global variables for easier identification.
Make @Ages a global so we can override it for testing purposes.
Rename %map to %Default_Map to better differenciate it.
Rename _PrepareMap to _CleanupAge which better describes what it does.
Rename $dst and $src arguments to _MergeMaps to $default and $overlay which
is more what they are. Besides, foo($dst, $src) is backwards.
Unnest the Condition loop in _MergeMaps()
Add a really basic test of ticket overlaying.
Modified: RT-Extension-TicketAging/lib/RT/Extension/TicketAging.pm
==============================================================================
--- RT-Extension-TicketAging/lib/RT/Extension/TicketAging.pm (original)
+++ RT-Extension-TicketAging/lib/RT/Extension/TicketAging.pm Thu Sep 28 01:26:56 2006
@@ -2,6 +2,9 @@
our $VERSION = '0.04';
+use strict;
+use warnings;
+
=head1 NAME
@@ -246,21 +249,21 @@
sub loc(@) { return $RT::SystemUser->loc(@_) }
-my @ages = ();
+our @Ages = ();
sub Ages {
- return @ages if @ages;
+ return @Ages if @Ages;
my $cf = RT::CustomField->new( $RT::SystemUser );
$cf->Load('Age');
die "Couldn't load the custom field" unless $cf->id;
my $values = $cf->Values;
while ( my $value = $values->Next ) {
- push @ages, $value->Name;
+ push @Ages, $value->Name;
}
- return @ages;
+ return @Ages;
}
-our %map = (
+our %Default_Map = (
Active => { },
Finished => {
Condition => {
@@ -332,52 +335,57 @@
sub PrepareMap {
my $self = shift || __PACKAGE__;
- my %res = %map;
+ my %res = %Default_Map;
my $user_map = RT->Config->Get('TicketAgingMap');
if ( $user_map ) {
$self->_MergeMaps( \%res, $user_map );
}
foreach my $age ( $self->Ages ) {
- my ($status, $msg) = $self->_PrepareMap( \%res, $age );
+ my ($status, $msg) = $self->_CleanupAge( \%res, $age );
return (undef, $msg) unless $status;
}
return (\%res);
}
sub _MergeMaps {
- my ($self, $dst, $src) = @_;
- my %age = map { $_ => 1 } $self->Ages;
- foreach my $age ( keys %src ) {
- next unless $src{ $age };
- unless ( ref $src->{ $age } eq 'HASH' ) {
+ my ($self, $default, $overlay) = @_;
+
+ foreach my $age ( keys %$overlay ) {
+ next unless $overlay->{ $age };
+ unless ( ref $overlay->{ $age } eq 'HASH' ) {
$RT::Logger->error( "TicketAgingMap -> $age is not a hash reference" );
next;
}
- if ( my $cond = $src->{ $age }{'Condition'} and ref $cond eq 'HASH' ) {
- foreach my $field ( qw(CallbackPre SQL CallbackPost Filter) ) {
- next unless defined $cond->{ $field };
- unless ( $cond->{$field} ) {
- $RT::Logger->info( "$age -> Condition -> $field is disabled due to TicketAgingMap" );
- delete $dst->{ $age }{'Condition'}{ $field };
- next;
- }
- if ( ref $cond->{$field} eq 'CODE' || ( $field eq 'SQL' && !ref $cond->{$field} )) {
- $dst->{ $age }{'Condition'}{ $field } = $cond->{$field};
- next;
- }
- $RT::Logger->info( "TicketAgingMap -> $age -> Condition -> $field has incorrect type" );
- }
+
+ my $cond = $overlay->{ $age }{'Condition'};
+ if( $cond && ref $cond ne 'HASH' ) {
+ $RT::Logger->error(
+ "TicketAgingMap -> $age -> Condition is not a hash reference"
+ );
+ next;
}
- elsif ( $src->{ $age }{'Condition'} ) {
- $RT::Logger->error( "TicketAgingMap -> $age -> Condition is not a hash reference" );
+
+ foreach my $field ( qw(CallbackPre SQL CallbackPost Filter) ) {
+ next unless defined $cond->{ $field };
+ unless ( $cond->{$field} ) {
+ $RT::Logger->info( "$age -> Condition -> $field is disabled due to TicketAgingMap" );
+ delete $default->{ $age }{'Condition'}{ $field };
+ next;
+ }
+ if ( ref $cond->{$field} eq 'CODE' || ( $field eq 'SQL' && !ref $cond->{$field} )) {
+ $default->{ $age }{'Condition'}{ $field } = $cond->{$field};
+ next;
+ }
+ $RT::Logger->info( "TicketAgingMap -> $age -> Condition -> $field has incorrect type" );
}
- if ( defined $src->{ $age }{'Action'} ) {
- unless ( $src->{ $age }{'Action'} ) {
+
+ if ( defined $overlay->{ $age }{'Action'} ) {
+ unless ( $overlay->{ $age }{'Action'} ) {
$RT::Logger->info( "$age -> Action is disabled due to TicketAgingMap" );
- delete $dst->{ $age }{'Condition'}{ $field };
+ delete $default->{ $age }{'Action'};
}
- elsif ( ref $src->{ $age }{'Action'} eq 'CODE' ) {
- $dst->{ $age }{'Action'} = $src->{ $age }{'Action'};
+ elsif ( ref $overlay->{ $age }{'Action'} eq 'CODE' ) {
+ $default->{ $age }{'Action'} = $overlay->{ $age }{'Action'};
}
else {
$RT::Logger->error( "TicketAgingMap -> $age -> Condition is not a code reference" );
@@ -386,7 +394,7 @@
}
}
-sub _PrepareMap {
+sub _CleanupAge {
my ($self, $map, $age) = @_;
unless( $map->{ $age } ) {
@@ -408,7 +416,7 @@
foreach my $callback ( qw(CallbackPre Filter CallbackPost) ) {
next unless my $code_ref = $condition->{$callback};
unless ( ref $code_ref eq 'CODE' ) {
- return 0, loc "Filter(age [_1]) '[_2]' is not code reference", $age, $filter;
+ return 0, loc "Filter(age [_1]) '[_2]' is not code reference", $age, $callback;
}
}
return 1;
Modified: RT-Extension-TicketAging/t/06.ticket-aging-map.t
==============================================================================
--- RT-Extension-TicketAging/t/06.ticket-aging-map.t (original)
+++ RT-Extension-TicketAging/t/06.ticket-aging-map.t Thu Sep 28 01:26:56 2006
@@ -21,3 +21,20 @@
RT->Config->Set('TicketAgingMap' => {});
is_deeply( $CLASS->PrepareMap, $Default_Map );
}
+
+
+verbose("New age");
+{
+ ok( !grep { $_ eq 'NewAgeHippies' } $CLASS->Ages );
+
+ my %new_map = (
+ NewAgeHippies => {
+ Condition => {
+ SQL => sub { return "Something" }
+ }
+ }
+ );
+
+ RT->Config->Set('TicketAgingMap' => \%new_map );
+ is_deeply( $CLASS->PrepareMap, { %$Default_Map, %new_map } );
+}
More information about the Rt-commit
mailing list