added actions-paths-from-config
Matt S Trout [Fri, 2 Jun 2006 23:30:56 +0000 (23:30 +0000)]
Changes
lib/Catalyst/Base.pm
lib/Catalyst/Manual/Intro.pod
t/lib/TestApp/Controller/Action/Path.pm

diff --git a/Changes b/Changes
index ff419a4..191c627 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,6 @@
 This file documents the revision history for Perl extension Catalyst.
 
+        - added ability to set action attributes from controller config
         - added merge_config_hashes() as a convenience method
         - Swapped out CGI::Cookie in favour of CGI::Simple::Cookie
         - Removed test dependencies on Test::NoWarnings, Test::MockObject
index 16eae62..a90656c 100644 (file)
@@ -85,7 +85,7 @@ Determine the namespace for actions in this component.
 
 sub action_namespace {
     my ( $self, $c ) = @_;
-    my $hash = (ref $self ? $self : $self->config);
+    my $hash = (ref $self ? $self : $self->config); # hate app-is-class
     return $hash->{namespace} if exists $hash->{namespace};
     return Catalyst::Utils::class2prefix( ref($self) || $self,
         $c->config->{case_sensitive} )
@@ -184,11 +184,22 @@ sub _parse_attrs {
         }
     }
 
+    my $hash = (ref $self ? $self : $self->config); # hate app-is-class
+
+    if (exists $hash->{actions} || exists $hash->{action}) {
+      my $a = $hash->{actions} || $hash->{action};
+      %raw_attributes = ((exists $a->{'*'} ? %{$a->{'*'}} : ()),
+                         %raw_attributes,
+                         (exists $a->{$name} ? %{$a->{$name}} : ()));
+    }
+
     my %final_attributes;
 
     foreach my $key (keys %raw_attributes) {
 
-        foreach my $value (@{$raw_attributes{$key}}) {
+        my $raw = $raw_attributes{$key};
+
+        foreach my $value (ref($raw) ? @$raw : $raw) {
 
             my $meth = "_parse_${key}_attr";
             if ( $self->can($meth) ) {
index 0026402..8e0211c 100644 (file)
@@ -878,21 +878,48 @@ application.
 
     package MyApp::Controller::Login;
 
-    sub sign-in : Local { }
-    sub new-password : Local { }
-    sub sign-out : Local { }
+    use base qw/Catalyst::Controller/;
+
+    sub sign_in : Path("sign-in") { }
+    sub new_password : Path("new-password") { }
+    sub sign_out : Path("sign-out") { }
 
     package MyApp::Controller::Catalog;
 
+    use base qw/Catalyst::Controller/;
+
     sub view : Local { }
     sub list : Local { }
 
     package MyApp::Controller::Cart;
 
+    use base qw/Catalyst::Controller/;
+
     sub add : Local { }
     sub update : Local { }
     sub order : Local { }
 
+Note that you can also supply attributes via the Controller's config so long
+as you have at least one attribute on a subref to be exported (:Action is
+commonly used for this) - for example the following is equivalent to the same
+controller above
+
+    package MyApp::Controller::Login;
+
+    use base qw/Catalyst::Controller/;
+
+    __PACKAGE__->config(
+      actions => {
+        'sign_in' => { Path => 'sign-in' },
+        'new_password' => { Path => 'new-password' },
+        'sign_out' => { Path => 'sign-out' },
+      },
+    );
+
+    sub sign_in : Action { }
+    sub new_password : Action { }
+    sub sign_out : Action { }
+
 =head3 Testing
 
 Catalyst has a built-in http server for testing! (Later, you can easily
index 1c36537..3a93525 100644 (file)
@@ -3,12 +3,19 @@ package TestApp::Controller::Action::Path;
 use strict;
 use base 'TestApp::Controller::Action';
 
-sub one : Action Path("a path with spaces") {
+__PACKAGE__->config(
+    actions => {
+      'one' => { 'Path' => [ 'a path with spaces' ] },
+      'two' => { 'Path' => "åäö" },
+    },
+);
+
+sub one : Action Path("this_will_be_overriden") {
     my ( $self, $c ) = @_;
     $c->forward('TestApp::View::Dump::Request');
 }
 
-sub two : Action Path("åäö") {
+sub two : Action {
     my ( $self, $c ) = @_;
     $c->forward('TestApp::View::Dump::Request');
 }