Skip to content

Commit

Permalink
action_postfix method
Browse files Browse the repository at this point in the history
  • Loading branch information
Zbigniew Lukasiak committed Mar 8, 2012
1 parent ae8ec9e commit 360181e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/DvdDatabase/lib/DvdDatabase/Controller/Dvd.pm
Expand Up @@ -45,7 +45,7 @@ sub record_action {
path => [ $action ],
app => $self->app,
env => $self->env,
self_url => $self->self_url . "record/$id/",
self_url => $self->self_url . "record/$id",
record => $record,
);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/DvdDatabase/lib/DvdDatabase/Controller/Dvd1.pm
Expand Up @@ -93,7 +93,7 @@ sub edit_action {
);
if( $req->method eq 'POST' && $form->process() ){
my $res = $req->new_response();
$res->redirect( $self->self_url . '/' . $record->id . '/view' );
$res->redirect( $self->self_url . 'record/' . $record->id . '/view' );
return $res;
}
$form->field( 'submit' )->value( 'Update' );
Expand Down
2 changes: 1 addition & 1 deletion examples/DvdDatabase/lib/DvdDatabase/Controller/Dvd2.pm
Expand Up @@ -82,7 +82,7 @@ sub edit_action {
);
if( $req->method eq 'POST' && $form->process() ){
my $res = $req->new_response();
$res->redirect( $self->self_url . '/' . $record->id . '/view' );
$res->redirect( $self->self_url . 'record/' . $record->id . '/view' );
return $res;
}
$form->field( 'submit' )->value( 'Update' );
Expand Down
39 changes: 22 additions & 17 deletions examples/DvdDatabase/lib/DvdDatabase/Controller/DvdSimpleUrl.pm
Expand Up @@ -6,12 +6,6 @@ extends 'WebNano::Controller';

use DvdDatabase::Controller::Dvd::Form;

has url_map => (
is => 'ro',
isa => 'HashRef',
default => sub { { view => 'view', 'delete' => 'delete', edit => 'edit' } }
);

has record => (
is => 'ro',
lazy => 1,
Expand All @@ -26,20 +20,32 @@ sub _build_record {
return;
}

sub record_path {
my $self = shift;
return defined( $self->path->[0] ) and $self->path->[0] =~ /^\d+$/;
}

sub action_postfix {
my $self = shift;
if( $self->record_path ){
return '_record';
}
else{
return '_action';
}
}

sub action_name {
my $self = shift;
my @path = @{ $self->path };
warn "@path";
if( defined( $path[0] ) and $path[0] =~ /^\d+$/ ){
if( $self->record_path ){
if( defined $self->record ){
warn $self->record;
return $path[1];
return $self->path->[1];
}
else{
return 'no_record';
return 'no';
}
}
return $path[0];
return $self->path->[0] || 'index';
}

sub index_action {
Expand All @@ -66,13 +72,12 @@ sub create_action {
return $self->render( template => 'edit.tt', form => $form->render );
}

sub view {
sub view_record {
my ( $self, ) = @_;

return $self->render( record => $self->record );
}

sub delete {
sub delete_record {
my ( $self, ) = @_;
if( $self->req->method eq 'GET' ){
return $self->render( record => $self->record );
Expand All @@ -85,7 +90,7 @@ sub delete {
}
}

sub edit {
sub edit_record {
my ( $self, ) = @_;
my $req = $self->req;
my $form = DvdDatabase::Controller::Dvd::Form->new(
Expand Down
8 changes: 4 additions & 4 deletions lib/WebNano/Controller.pm
Expand Up @@ -25,20 +25,20 @@ sub render {
return $self->app->renderer->render( c => $self, @_ );
}

sub action_name { shift->path->[0] }
sub action_name { shift->path->[0] // 'index' }

sub action_args {
my $self = shift;
my @path = @{ $self->path };
return @path[ 1 .. $#path ];
}

sub action_postfix { '_action' };

sub local_dispatch {
my ( $self ) = @_;
my $action;
my $name = uri_unescape( $self->action_name );
$name = 'index' if !defined( $name ) || !length( $name );
warn "dispatching for $name";
if( my $map = $self->url_map ){
if( ref $map eq 'HASH' ){
$action = $self->can( $map->{$name} ) if $map->{$name};
Expand All @@ -48,7 +48,7 @@ sub local_dispatch {
}
}
if( !$action ){
$action = $self->can( $name . '_action' ) if defined $name;
$action = $self->can( $name . $self->action_postfix ) if defined $name;
}
my $out;
if( $action ){
Expand Down
8 changes: 3 additions & 5 deletions lib/WebNano/Renderer/TTiny.pm
Expand Up @@ -22,23 +22,21 @@ sub _to_list {
sub render {
my( $self, %vars ) = @_;
my $c = $vars{c};

my @search_path;
my $action;
if( $c ){
my $path = ref $c;
$path =~ s/.*::Controller(::)?//;
$path =~ s{::}{/};
@search_path = ( $path, @{ $c->template_search_path });
$action = $c->action_name;
}
if( !@search_path ){
@search_path = ( '' );
}
my $template = $vars{template};
if( !$template ){
my @caller = caller(2);
$template = $caller[3];
$template =~ s/_action$//;
$template =~ s/^.*:://;
$template = $action;
$template .= '.' . $self->TEMPLATE_EXTENSION if $self->TEMPLATE_EXTENSION;
}
my $full_template;
Expand Down

0 comments on commit 360181e

Please sign in to comment.