[Rt-commit] rt branch, 3.9-trunk, updated. rt-3.8.8-177-g409adf7

? sunnavy sunnavy at bestpractical.com
Wed Jul 21 00:21:05 EDT 2010


The branch, 3.9-trunk has been updated
       via  409adf7af3e1949ab2986fa36780d4090f67f3da (commit)
      from  683e7442f3f11f33346778ca47274b995e27b32c (commit)

Summary of changes:
 lib/RT/Config.pm |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 t/api/config.t   |   33 ++++++++++++++++++++++
 2 files changed, 114 insertions(+), 0 deletions(-)
 create mode 100644 t/api/config.t

- Log -----------------------------------------------------------------
commit 409adf7af3e1949ab2986fa36780d4090f67f3da
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Jul 21 12:21:33 2010 +0800

    (Add|Delete|Update)Option for Config

diff --git a/lib/RT/Config.pm b/lib/RT/Config.pm
index 2e1bd70..2cf66f3 100644
--- a/lib/RT/Config.pm
+++ b/lib/RT/Config.pm
@@ -939,6 +939,87 @@ sub Options {
     return @res;
 }
 
+=head2 AddOption( Name => '', Section => '', ... )
+
+=cut
+
+sub AddOption {
+    my $self = shift;
+    my %args = (
+        Name            => undef,
+        Section         => undef,
+        Overridable     => 0,
+        SortOrder       => undef,
+        Widget          => '/Widgets/Form/String',
+        WidgetArguments => {},
+        @_
+    );
+
+    unless ( $args{Name} ) {
+        $RT::Logger->error("Need Name to add a new config");
+        return;
+    }
+
+    unless ( $args{Section} ) {
+        $RT::Logger->error("Need Section to add a new config option");
+        return;
+    }
+
+    $META{ delete $args{Name} } = \%args;
+}
+
+=head2 DeleteOption( Name => '' )
+
+=cut
+
+sub DeleteOption {
+    my $self = shift;
+    my %args = (
+        Name            => undef,
+        @_
+        );
+    if ( $args{Name} ) {
+        delete $META{$args{Name}};
+    }
+    else {
+        $RT::Logger->error("Need Name to remove a config option");
+        return;
+    }
+}
+
+=head2 UpdateOption( Name => '' ), Section => '', ... )
+
+=cut
+
+sub UpdateOption {
+    my $self = shift;
+    my %args = (
+        Name            => undef,
+        Section         => undef,
+        Overridable     => undef,
+        SortOrder       => undef,
+        Widget          => undef,
+        WidgetArguments => undef,
+        @_
+    );
+
+    unless ( $args{Name} ) {
+        $RT::Logger->error("Need Name to update a new config");
+        return;
+    }
+
+    unless ( exists $META{ $args{Name} } ) {
+        $RT::Logger->error("Config $args{Name} doesn't exist");
+        return;
+    }
+
+    for my $type (qw/Section Overridable SortOrder Widget WidgetArguments/) {
+        next unless defined $args{$type};
+        $META{ $args{Name} }{$type} = $args{$type};
+    }
+    return 1;
+}
+
 eval "require RT::Config_Vendor";
 if ($@ && $@ !~ qr{^Can't locate RT/Config_Vendor.pm}) {
     die $@;
diff --git a/t/api/config.t b/t/api/config.t
new file mode 100644
index 0000000..8e24be0
--- /dev/null
+++ b/t/api/config.t
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+use RT;
+use RT::Test tests => 9;
+
+ok(
+    RT::Config->AddOption(
+        Name    => 'foo',
+        Section => 'bar',
+    ),
+    'added option foo'
+);
+
+my $meta = RT::Config->Meta('foo');
+is( $meta->{Section}, 'bar', 'Section is bar' );
+is( $meta->{Widget}, '/Widgets/Form/String', 'default Widget is string' );
+is_deeply( $meta->{WidgetArguments},
+    {},, 'default WidgetArguments is empty hashref' );
+
+ok(
+    RT::Config->UpdateOption(
+        Name    => 'foo',
+        Section => 'baz',
+        Widget => '/Widgets/Form/Boolean',
+    ),
+    'updated option foo to section baz'
+);
+is( $meta->{Section}, 'baz', 'section is updated to baz' );
+is( $meta->{Widget}, '/Widgets/Form/Boolean', 'widget is updated to boolean' );
+
+ok( RT::Config->DeleteOption( Name => 'foo' ), 'removed option foo' );
+is( RT::Config->Meta('foo'), undef, 'foo is indeed deleted' );
+

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


More information about the Rt-commit mailing list