parse actionrole params and pass them to attributes
John Napiorkowski [Tue, 10 Aug 2010 20:59:08 +0000 (16:59 -0400)]
lib/CatalystX/Declare/Keyword/Action.pm
t/100_complex.t
t/lib/TestApp/Controller/ActionParams.pm [new file with mode: 0644]

index dd89924..a348bbe 100644 (file)
@@ -231,7 +231,15 @@ class CatalystX::Declare::Keyword::Action {
         # we need to fish for aliases here since we are still unclean
         my @roles = ();
         for my $role_with_arg(@roles_with_args) {
-            my $role = $role_with_arg->[0];
+            my ($role, $params) = @{$role_with_arg};
+            if($params) {
+                my ($first, @rest) = eval $params;
+                my %params = ref $first eq 'HASH' ? %$first : ($first, @rest);
+                for my $key (keys %params) {
+                    $attrs->{$key} = [$params{$key}];
+                }
+            }          
+
             if (defined(my $alias = $self->_check_for_available_import($ctx, $role))) {
                 $role = $alias;
             }
index c982ce5..001756f 100644 (file)
@@ -67,5 +67,8 @@ is get('/foo/wants_integer/butdoesntgetone'), 'no integer', 'validation error ca
 # fix bug with capture args below under { }
 is get('/foo/lower/down/the/param/3/road/5'), 8, 'capture args and block under work together';
 
+# Make sure action roles with parameters pass params to attributes
+is get('/actionparams/first'), 'action_args_first: 100,101', 'actionrole with params';
+is get('/actionparams/second'), 'action_args_second: 200,201', 'actionrole with params (part two)';
 
 done_testing;
diff --git a/t/lib/TestApp/Controller/ActionParams.pm b/t/lib/TestApp/Controller/ActionParams.pm
new file mode 100644 (file)
index 0000000..41b8bee
--- /dev/null
@@ -0,0 +1,38 @@
+use CatalystX::Declare;
+namespace TestApp;
+
+role hasActionParams {
+    has [qw/p1 p2/] => (is=>'ro', lazy_build=>1);
+
+    method _build_p1 {
+        $self->attributes->{p1}->[0];
+    }
+    method _build_p2 {
+        $self->attributes->{p2}->[0];
+    }
+}
+
+controller ::Controller::ActionParams {
+
+    action base
+    under '/base'
+    as 'actionparams';
+
+    action first under base
+    with hasActionParams(p1=>100,p2=>101) 
+    is final { 
+        my $p1 = $ctx->controller->action_for('first')->p1;
+        my $p2 = $ctx->controller->action_for('first')->p2;
+        $ctx->response->body("action_args_first: $p1,$p2");
+    }
+
+    action second under base
+    with hasActionParams({p1=>200,p2=>201}) 
+    is final {
+        my $p1 = $ctx->controller->action_for('second')->p1;
+        my $p2 = $ctx->controller->action_for('second')->p2;
+        $ctx->response->body("action_args_second: $p1,$p2");
+    }
+
+}
+