From: Matt S Trout Date: Fri, 2 Jun 2006 23:30:56 +0000 (+0000) Subject: added actions-paths-from-config X-Git-Tag: 5.7099_04~550 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=c02f74904a39d6bebc3c94b5c10e723712d97b1d;hp=1facb4917de4cbcb24b96249ed5b72106cdc74da added actions-paths-from-config --- diff --git a/Changes b/Changes index ff419a4..191c627 100644 --- 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 diff --git a/lib/Catalyst/Base.pm b/lib/Catalyst/Base.pm index 16eae62..a90656c 100644 --- a/lib/Catalyst/Base.pm +++ b/lib/Catalyst/Base.pm @@ -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) ) { diff --git a/lib/Catalyst/Manual/Intro.pod b/lib/Catalyst/Manual/Intro.pod index 0026402..8e0211c 100644 --- a/lib/Catalyst/Manual/Intro.pod +++ b/lib/Catalyst/Manual/Intro.pod @@ -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 diff --git a/t/lib/TestApp/Controller/Action/Path.pm b/t/lib/TestApp/Controller/Action/Path.pm index 1c36537..3a93525 100644 --- a/t/lib/TestApp/Controller/Action/Path.pm +++ b/t/lib/TestApp/Controller/Action/Path.pm @@ -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'); }