[Catalyst-commits] r12690 - in Catalyst-Runtime/5.80/trunk: lib/Catalyst t/aggregate t/lib/TestApp/Action t/lib/TestApp/Controller/Action

rafl at dev.catalyst.perl.org rafl at dev.catalyst.perl.org
Tue Jan 19 16:07:46 GMT 2010


Author: rafl
Date: 2010-01-19 16:07:45 +0000 (Tue, 19 Jan 2010)
New Revision: 12690

Added:
   Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Action/TestExtraArgsAction.pm
Modified:
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/Controller.pm
   Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_action.t
   Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Action.pm
Log:
Merge branch 'action_args'

* action_args:
  And another minor tweak.
  Some more doc tweaking.
  tweaked docs based on IRC suggestions
  added documentation for the configuration option "action_args".
  Allow passing extra args to action constructors using action_args config.
  Add tests for passing extra arguments to action constructors.
  Create branch action_args

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/Controller.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/Controller.pm	2010-01-19 16:02:17 UTC (rev 12689)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/Controller.pm	2010-01-19 16:07:45 UTC (rev 12690)
@@ -255,9 +255,15 @@
     my $class = (exists $args{attributes}{ActionClass}
                     ? $args{attributes}{ActionClass}[0]
                     : $self->_action_class);
+    Class::MOP::load_class($class);
 
-    Class::MOP::load_class($class);
-    return $class->new( \%args );
+    my $action_args = $self->config->{action_args};
+    my %extra_args = (
+        %{ $action_args->{'*'}           || {} },
+        %{ $action_args->{ $args{name} } || {} },
+    );
+
+    return $class->new({ %extra_args, %args });
 }
 
 sub _parse_attrs {
@@ -440,6 +446,26 @@
 
 Sets 'path_prefix', as described below.
 
+=head2 action_args
+
+Allows you to set constructor arguments on your actions. You can set arguments
+globally (for all actions of the controller) and specifically (for a single
+action). This is particularly useful when using C<ActionRole>s
+(L<Catalyst::Controller::ActionRole>) and custom C<ActionClass>es.
+
+    __PACKAGE__->config(
+        action_args => {
+            '*' => { globalarg1 => 'hello', globalarg2 => 'goodbye' },
+            'specific_action' => { customarg => 'arg1' },
+        },
+     );
+
+In the case above the action class associated with C<specific_action> would get
+passed the following arguments, in addition to the normal action constructor
+arguments, when it is instantiated:
+
+  (globalarg1 => 'hello', globalarg2 => 'goodbye', customarg => 'arg1')
+
 =head1 METHODS
 
 =head2 BUILDARGS ($app, @args)

Modified: Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_action.t
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_action.t	2010-01-19 16:02:17 UTC (rev 12689)
+++ Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_action.t	2010-01-19 16:07:45 UTC (rev 12690)
@@ -10,7 +10,7 @@
 
 BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
 
-use Test::More tests => 42 * $iters;
+use Test::More;
 use Catalyst::Test 'TestApp';
 
 if ( $ENV{CAT_BENCHMARK} ) {
@@ -147,4 +147,25 @@
         );
     }
 
+    {
+        ok( my $response = request('http://localhost/action_action_seven'),
+            'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->content_type, 'text/plain', 'Response Content-Type' );
+        is( $response->header('X-Catalyst-Action'),
+            'action_action_seven', 'Test Action' );
+        is(
+            $response->header('X-Test-Class'),
+            'TestApp::Controller::Action::Action',
+            'Test Class'
+        );
+        is( $response->header('X-TestExtraArgsAction'), '42,23', 'Extra args get passed to action contstructor' );
+        like(
+            $response->content,
+            qr/^bless\( .* 'Catalyst::Request' \)$/s,
+            'Content is a serialized Catalyst::Request'
+        );
+    }
 }
+
+done_testing;

Added: Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Action/TestExtraArgsAction.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Action/TestExtraArgsAction.pm	                        (rev 0)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Action/TestExtraArgsAction.pm	2010-01-19 16:07:45 UTC (rev 12690)
@@ -0,0 +1,17 @@
+package TestApp::Action::TestExtraArgsAction;
+
+use Moose;
+use namespace::autoclean;
+
+extends 'Catalyst::Action';
+
+has [qw/extra_arg another_extra_arg/] => (is => 'ro');
+
+after execute => sub {
+    my ($self, $controller, $ctx) = @_;
+    $ctx->response->header('X-TestExtraArgsAction' => join q{,} => $self->extra_arg, $self->another_extra_arg);
+};
+
+__PACKAGE__->meta->make_immutable;
+
+1;

Modified: Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Action.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Action.pm	2010-01-19 16:02:17 UTC (rev 12689)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Action.pm	2010-01-19 16:07:45 UTC (rev 12690)
@@ -3,7 +3,15 @@
 use strict;
 use base 'TestApp::Controller::Action';
 
-__PACKAGE__->config( actions => { action_action_five => { ActionClass => '+Catalyst::Action::TestBefore' } } );
+__PACKAGE__->config(
+    actions => {
+        action_action_five => { ActionClass => '+Catalyst::Action::TestBefore' },
+    },
+    action_args => {
+        '*'                 => { extra_arg         => 42 },
+        action_action_seven => { another_extra_arg => 23 },
+    },
+);
 
 sub action_action_one : Global : ActionClass('TestBefore') {
     my ( $self, $c ) = @_;
@@ -38,4 +46,9 @@
     $c->forward('TestApp::View::Dump::Request');
 }
 
+sub action_action_seven : Global : ActionClass('~TestExtraArgsAction') {
+    my ( $self, $c ) = @_;
+    $c->forward('TestApp::View::Dump::Request');
+}
+
 1;




More information about the Catalyst-commits mailing list