Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Inheriting templates in CRUD; Template Toolkit for CRUD

  • Loading branch information...
commit 8b4d633ab4c91926fc8b4e5e56b7d90587387d42 1 parent be2f2c6
Zbigniew Lukasiak authored
View
19 extensions/WebNano-Controller-CRUD/lib/WebNano/Controller/CRUD.pm
@@ -2,10 +2,10 @@ package WebNano::Controller::CRUD;
use Moose;
use MooseX::NonMoose;
use Class::MOP;
+use File::Spec::Functions 'catdir';
extends 'WebNano::Controller';
-has record_controller_class => ( is => 'ro', isa => 'Str', required => 1, default => 'WebNano::Controller::CRUD::Record' );
has form_class => ( is => 'ro', isa => 'Str', required => 1 );
has rs_name => ( is => 'ro', isa => 'Str', required => 1 );
@@ -15,6 +15,23 @@ has record_actions => (
default => sub { { view => 1, 'delete' => 1, edit => 1 } }
);
+my $FULLPATH;
+BEGIN { use Cwd (); $FULLPATH = Cwd::abs_path(__FILE__) }
+
+sub template_search_path {
+ my $self = shift;
+ my $mydir = $FULLPATH;
+ $mydir =~ s/.pm$//;
+ return [ catdir( $mydir, 'templates' ) ];
+}
+
+sub columns {
+ my $self = shift;
+ my $source = $self->application->schema->source( $self->rs_name );
+ return [ $source->columns ];
+}
+
+
sub parse_path {
my( $self, $path ) = @_;
my $parsed;
View
46 extensions/WebNano-Controller-CRUD/t/crud_base_controller.t
@@ -6,27 +6,41 @@ use lib 'extensions/lib';
use MyApp;
use File::Copy;
-copy('t/data/dvdzbr.db','t/tmp/dvdzbr.db') or die "Copy failed: $!";
+for my $controller( qw/DvdWithBaseCRUD OwnTemplates/ ){
+
+ copy('t/data/dvdzbr.db','t/tmp/dvdzbr.db') or die "Copy failed: $!";
+
+ test_psgi(
+ app => MyApp->new()->psgi_callback,
+ client => sub {
+ my $cb = shift;
+ $res = $cb->(GET "/$controller/");
+ like( $res->content, qr/Jurassic Park II/ );
+ $res = $cb->(POST "/$controller/5/edit", [ name => 'Not Jurassic Park' ] );
+ ok( $res->is_redirect, 'Redirect after POST' );
+ $res = $cb->(GET $res->header('Location'));
+ like( $res->content, qr/Not Jurassic Park/ );
+ $res = $cb->(POST "/$controller/create", [ name => 'A new dvd', owner => 1 ] );
+ ok( $res->is_redirect, 'Redirect after POST' );
+ $res = $cb->(GET $res->header('Location'));
+ like( $res->content, qr/A new dvd/ );
+ $res = $cb->(GET "/$controller/view/5");
+ is( $res->code, 404 , '404 for view with no record' );
+ $res = $cb->(GET "/$controller/555/view");
+ is( $res->code, 404 , '404 for view with no record' );
+ }
+ );
+}
test_psgi(
app => MyApp->new()->psgi_callback,
client => sub {
my $cb = shift;
- $res = $cb->(GET "/DvdWithBaseCRUD/");
- like( $res->content, qr/Jurassic Park II/ );
- $res = $cb->(POST '/DvdWithBaseCRUD/5/edit', [ name => 'Not Jurassic Park' ] );
- ok( $res->is_redirect, 'Redirect after POST' );
- $res = $cb->(GET $res->header('Location'));
- like( $res->content, qr/Not Jurassic Park/ );
- $res = $cb->(POST '/DvdWithBaseCRUD/create', [ name => 'A new dvd', owner => 1 ] );
- ok( $res->is_redirect, 'Redirect after POST' );
- $res = $cb->(GET $res->header('Location'));
- like( $res->content, qr/A new dvd/ );
- $res = $cb->(GET "/DvdWithBaseCRUD/view/5");
- is( $res->code, 404 , '404 for view with no record' );
- $res = $cb->(GET "/DvdWithBaseCRUD/555/view");
- is( $res->code, 404 , '404 for view with no record' );
- }
+ $res = $cb->(GET "/OwnTemplates/");
+ like( $res->content, qr{This is t/data/templates/OwnTemplates/list.tt} );
+ $res = $cb->(GET "/OwnTemplates/5/view");
+ like( $res->content, qr{This is t/data/templates/OwnTemplates/record.tt} );
+ }
);
done_testing();
View
6 extensions/WebNano-Controller-CRUD/t/data/templates/DvdWithBaseCRUD/edit.tt
@@ -1,6 +0,0 @@
-<html>
-<body>
-[% form %]
-</body>
-</html>
-
View
0  ...r-CRUD/t/data/templates/DvdWithBaseCRUD/delete.tt → ...ller-CRUD/t/data/templates/OwnTemplates/delete.tt
File renamed without changes
View
7 extensions/WebNano-Controller-CRUD/t/data/templates/OwnTemplates/edit.tt
@@ -0,0 +1,7 @@
+<html>
+<body>
+[% form %]
+This is t/data/templates/OwnTemplates/edit.tt
+</body>
+</html>
+
View
2  ...ler-CRUD/t/data/templates/DvdWithBaseCRUD/list.tt → ...roller-CRUD/t/data/templates/OwnTemplates/list.tt
@@ -21,6 +21,8 @@
<td><a href="[% c.self_url %][% record.id %]/delete">Delete</a></td>
</tr>
[% END %]
+
+This is t/data/templates/OwnTemplates/list.tt
</body>
</html>
View
2  ...r-CRUD/t/data/templates/DvdWithBaseCRUD/record.tt → ...ller-CRUD/t/data/templates/OwnTemplates/record.tt
@@ -64,6 +64,8 @@
[% record.hour %]
</td>
</tr>
+</table>
+This is t/data/templates/OwnTemplates/record.tt
</body>
</html>
View
4 extensions/WebNano-Controller-CRUD/t/lib/MyApp.pm
@@ -4,7 +4,7 @@ use MooseX::NonMoose;
extends 'WebNano';
use Config::Any;
use MyApp::DBSchema;
-use WebNano::Renderer::TTiny;
+use WebNano::Renderer::TT;
has config => ( is => 'ro', isa => 'HashRef', lazy_build => 1 );
@@ -25,7 +25,7 @@ has renderer => ( is => 'ro', lazy_build => 1 );
sub _build_renderer {
my $self = shift;
my $config = $self->config->{renderer};
- return WebNano::Renderer::TTiny->new( %$config );
+ return WebNano::Renderer::TT->new( %$config );
}
sub get_config {
View
14 extensions/WebNano-Controller-CRUD/t/lib/MyApp/Controller/OwnTemplates.pm
@@ -0,0 +1,14 @@
+package MyApp::Controller::OwnTemplates;
+use Moose;
+
+extends 'WebNano::Controller::CRUD';
+
+has '+form_class' => ( default => 'MyApp::Controller::Form' );
+has '+rs_name' => ( default => 'Dvd' );
+
+sub index_action {
+ my $self = shift;
+ return $self->list_action;
+}
+
+1;
View
13 extensions/WebNano-Renderer-TT/lib/WebNano/Renderer/TT.pm
@@ -50,7 +50,12 @@ sub render {
my @path = @{ $self->global_path };
for my $root( _to_list( $self->root ) ){
for my $sub_path( @input_path ){
- push @path, File::Spec->catdir( $root, $sub_path );
+ if( File::Spec->file_name_is_absolute( $sub_path ) ){
+ push @path, $sub_path;
+ }
+ else{
+ push @path, File::Spec->catdir( $root, $sub_path );
+ }
}
}
$self->INCLUDE_PATH( \@path );
@@ -64,8 +69,10 @@ sub render {
}
my $tt = $self->_tt;
my $output;
- $tt->process( $template, \%params, \$output )
- || die $tt->error();
+ if( ! $tt->process( $template, \%params, \$output ) ){
+ warn "Current INCLUDE_PATH: @path\n";
+ die $tt->error();
+ }
return $output;
}
Please sign in to comment.
Something went wrong with that request. Please try again.