Permalink
Browse files

example cruds improved

  • Loading branch information...
1 parent 190387f commit 67e4554b3b33a7d3f4bb65bb89892840a1ca84ae Zbigniew Lukasiak committed Sep 4, 2010
Showing with 579 additions and 28 deletions.
  1. +2 −2 examples/DvdDatabase/{t/data → }/app.conf
  2. +4 −2 examples/DvdDatabase/lib/DvdDatabase.pm
  3. +102 −0 examples/DvdDatabase/lib/DvdDatabase/Controller/Dvd1.pm
  4. +92 −0 examples/DvdDatabase/lib/DvdDatabase/Controller/Dvd2.pm
  5. +1 −1 examples/DvdDatabase/lib/DvdDatabase/Controller/DvdSimpleUrl.pm
  6. +1 −20 examples/DvdDatabase/t/{crudapp.t → app.t}
  7. +34 −0 examples/DvdDatabase/t/separate_url.t
  8. +34 −0 examples/DvdDatabase/t/simple_url.t
  9. BIN examples/DvdDatabase/t/tmp/dvdzbr.db
  10. 0 examples/DvdDatabase/{t/data → }/templates/Dvd/Record/delete.tt
  11. 0 examples/DvdDatabase/{t/data → }/templates/Dvd/Record/edit.tt
  12. 0 examples/DvdDatabase/{t/data → }/templates/Dvd/Record/record.tt
  13. 0 examples/DvdDatabase/{t/data → }/templates/Dvd/edit.tt
  14. 0 examples/DvdDatabase/{t/data → }/templates/Dvd/list.tt
  15. +10 −0 examples/DvdDatabase/templates/Dvd1/delete.tt
  16. +6 −0 examples/DvdDatabase/templates/Dvd1/edit.tt
  17. 0 examples/DvdDatabase/{t/data/templates/DvdSimpleUrl → templates/Dvd1}/list.tt
  18. +69 −0 examples/DvdDatabase/templates/Dvd1/record.tt
  19. +10 −0 examples/DvdDatabase/templates/Dvd2/delete.tt
  20. +6 −0 examples/DvdDatabase/templates/Dvd2/edit.tt
  21. +26 −0 examples/DvdDatabase/templates/Dvd2/list.tt
  22. +69 −0 examples/DvdDatabase/templates/Dvd2/view.tt
  23. +10 −0 examples/DvdDatabase/templates/DvdSimpleUrl/delete.tt
  24. +6 −0 examples/DvdDatabase/templates/DvdSimpleUrl/edit.tt
  25. +26 −0 examples/DvdDatabase/templates/DvdSimpleUrl/list.tt
  26. +69 −0 examples/DvdDatabase/templates/DvdSimpleUrl/view.tt
  27. +0 −1 extensions/WebNano-Controller-CRUD/lib/WebNano/Controller/CRUD.pm
  28. BIN extensions/WebNano-Controller-CRUD/t/tmp/dvdzbr.db
  29. +2 −2 lib/WebNano/Controller.pm
@@ -1,9 +1,9 @@
<renderer>
-root = t/data/templates
+root = templates
TEMPLATE_EXTENSION = tt
</renderer>
<schema>
-dbi_dsn = dbi:SQLite:dbname=t/tmp/dvdzbr.db
+dbi_dsn = dbi:SQLite:dbname=dvdzbr.db
<dbi_params>
sqlite_unicode = 1
</dbi_params>
@@ -3,14 +3,16 @@ use Moose;
use MooseX::NonMoose;
extends 'WebNano';
use Config::Any;
-use DvdDatabase::DBSchema;
+use Plack::Request;
use WebNano::Renderer::TTiny;
+use DvdDatabase::DBSchema;
+
has config => ( is => 'ro', isa => 'HashRef', lazy_build => 1 );
sub _build_config {
my( $self ) = @_;
- return $self->get_config( 't/data/app' );
+ return $self->get_config( 'app' );
}
has schema => ( is => 'ro', isa => 'DBIx::Class::Schema', lazy_build => 1 );
@@ -0,0 +1,102 @@
+package DvdDatabase::Controller::Dvd1;
+use Moose;
+use MooseX::NonMoose;
+
+extends 'WebNano::Controller';
+
+use DvdDatabase::Controller::Dvd::Form;
+
+has record_methods => (
+ is => 'ro',
+ isa => 'HashRef',
+ default => sub { { view_action => 1, 'delete_action' => 1, edit_action => 1 } }
+);
+
+sub record_action {
+ my( $self, $id, $method, @args ) = @_;
+ my $rs = $self->application->schema->resultset( 'Dvd' );
+ my $record = $rs->find( $id );
+ return $self->local_dispatch( $method, $record, @args );
+}
+
+sub index_action {
+ my( $self ) = @_;
+ my $rs = $self->application->schema->resultset( 'Dvd' );
+ return $self->render( template => 'list.tt', items => [ $rs->search ] );
+}
+
+sub create_action {
+ my ( $self ) = @_;
+ my $req = $self->request;
+
+ my $form = DvdDatabase::Controller::Dvd::Form->new(
+ params => $req->parameters->as_hashref,
+ schema => $self->application->schema,
+ );
+ if( $req->method eq 'POST' && $form->process() ){
+ my $record = $form->item;
+ my $res = $req->new_response();
+ $res->redirect( $self->self_url . 'record/' . $record->id . '/view' );
+ return $res;
+ }
+ $form->field( 'submit' )->value( 'Create' );
+ return $self->render( template => 'edit.tt', form => $form->render );
+}
+
+sub view_action {
+ my ( $self, $record ) = @_;
+ if( !$record || !blessed($record) ) {
+ my $res = $self->request->new_response(404);
+ $res->content_type('text/plain');
+ $res->body( 'No record found' );
+ return $res;
+ }
+
+ return $self->render( template => 'record.tt', record => $record );
+}
+
+sub delete_action {
+ my ( $self, $record ) = @_;
+ if( !$record || !blessed($record) ) {
+ my $res = $self->request->new_response(404);
+ $res->content_type('text/plain');
+ $res->body( 'No record found' );
+ return $res;
+ }
+
+ if( $self->request->method eq 'GET' ){
+ return $self->render( record => $record );
+ }
+ else{
+ $record->delete;
+ my $res = $self->request->new_response();
+ $res->redirect( $self->self_url );
+ return $res;
+ }
+}
+
+sub edit_action {
+ my ( $self, $record ) = @_;
+ if( !$record || !blessed($record) ) {
+ my $res = $self->request->new_response(404);
+ $res->content_type('text/plain');
+ $res->body( 'No record found' );
+ return $res;
+ }
+
+ my $req = $self->request;
+ my $form = DvdDatabase::Controller::Dvd::Form->new(
+ item => $record,
+ params => $req->parameters->as_hashref,
+ );
+ if( $req->method eq 'POST' && $form->process() ){
+ my $res = $req->new_response();
+ $res->redirect( $self->self_url . '/' . $record->id . '/view' );
+ return $res;
+ }
+ $form->field( 'submit' )->value( 'Update' );
+ return $self->render( form => $form->render );
+}
+
+1;
+
@@ -0,0 +1,92 @@
+package DvdDatabase::Controller::Dvd2;
+use Moose;
+use MooseX::NonMoose;
+
+extends 'WebNano::Controller';
+
+use DvdDatabase::Controller::Dvd::Form;
+
+around 'local_dispatch' => sub {
+ my( $orig, $self, $path, @args ) = @_;
+ if( $path =~ s{^record/(\d+)/}{} ){
+ my $id = $1;
+ my $rs = $self->application->schema->resultset( 'Dvd' );
+ my $record = $rs->find( $id );
+ if( ! $record ) {
+ my $res = $self->request->new_response(404);
+ $res->content_type('text/plain');
+ $res->body( 'No record with id: ' . $id );
+ return $res;
+ }
+ unshift @args, $record;
+ }
+ if( $path =~ m{^(view|edit|delete)/} ){
+ my $res = $self->request->new_response(404);
+ $res->content_type('text/plain');
+ $res->body( 'No page found' );
+ return $res;
+ }
+ return $self->$orig( $path, @args );
+};
+
+sub index_action {
+ my( $self ) = @_;
+ my $rs = $self->application->schema->resultset( 'Dvd' );
+ return $self->render( template => 'list.tt', items => [ $rs->search ] );
+}
+
+sub create_action {
+ my ( $self ) = @_;
+ my $req = $self->request;
+
+ my $form = DvdDatabase::Controller::Dvd::Form->new(
+ params => $req->parameters->as_hashref,
+ schema => $self->application->schema,
+ );
+ if( $req->method eq 'POST' && $form->process() ){
+ my $record = $form->item;
+ my $res = $req->new_response();
+ $res->redirect( $self->self_url . 'record/' . $record->id . '/view' );
+ return $res;
+ }
+ $form->field( 'submit' )->value( 'Create' );
+ return $self->render( template => 'edit.tt', form => $form->render );
+}
+
+sub view_action {
+ my ( $self, $record ) = @_;
+
+ return $self->render( record => $record );
+}
+
+sub delete_action {
+ my ( $self, $record ) = @_;
+ if( $self->request->method eq 'GET' ){
+ return $self->render( record => $record );
+ }
+ else{
+ $record->delete;
+ my $res = $self->request->new_response();
+ $res->redirect( $self->self_url );
+ return $res;
+ }
+}
+
+sub edit_action {
+ my ( $self, $record ) = @_;
+ my $req = $self->request;
+ my $form = DvdDatabase::Controller::Dvd::Form->new(
+ item => $record,
+ params => $req->parameters->as_hashref,
+ );
+ if( $req->method eq 'POST' && $form->process() ){
+ my $res = $req->new_response();
+ $res->redirect( $self->self_url . '/' . $record->id . '/view' );
+ return $res;
+ }
+ $form->field( 'submit' )->value( 'Update' );
+ return $self->render( form => $form->render );
+}
+
+1;
+
@@ -57,7 +57,7 @@ sub create_action {
sub view {
my ( $self, $record ) = @_;
- return $self->render( template => 'record.tt', record => $record );
+ return $self->render( record => $record );
}
sub delete {
@@ -6,27 +6,8 @@ use File::Copy;
use Plack::Middleware::Session;
use Test::WWW::Mechanize::PSGI;
-copy('t/data/dvdzbr.db','t/tmp/dvdzbr.db') or die "Copy failed: $!";
-
+copy('t/data/dvdzbr.db','dvdzbr.db') or die "Copy failed: $!";
my $app = Plack::Middleware::Session->wrap( DvdDatabase->new()->psgi_callback );
-test_psgi(
- app => $app,
- client => sub {
- my $cb = shift;
- my $res;
- $res = $cb->(GET "/Dvd");
- like( $res->content, qr/Jurassic Park II/ );
- $res = $cb->(POST '/Dvd/record/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 '/DvdSimpleUrl/5/edit', [ name => 'Not even trace of Jurrasic Park' ] );
- ok( $res->is_redirect, 'Redirect after POST' );
- $res = $cb->(GET $res->header('Location'));
- like( $res->content, qr/Not even trace of Jurrasic Park/ );
- }
-);
-
my $mech = Test::WWW::Mechanize::PSGI->new( app => $app );
$mech->get_ok( '/session_check' );
$mech->content_contains( "Hello, you've been here for 1th time!" );
@@ -0,0 +1,34 @@
+use Test::More;
+use Plack::Test;
+use HTTP::Request::Common;
+use File::Copy;
+use Test::WWW::Mechanize::PSGI;
+
+use DvdDatabase;
+
+for my $controller( qw/Dvd Dvd1 Dvd2/ ){
+ copy('t/data/dvdzbr.db','dvdzbr.db') or die "Copy failed: $!";
+
+ my $app = DvdDatabase->new()->psgi_callback;
+ test_psgi(
+ app => $app,
+ client => sub {
+ my $cb = shift;
+ my $res;
+ $res = $cb->(GET "/$controller");
+ like( $res->content, qr/Jurassic Park II/ );
+ $res = $cb->(POST "/$controller/record/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->(GET "/$controller/record/5/view");
+ like( $res->content, qr/Not Jurassic Park/ );
+ $res = $cb->(GET "/$controller/view/5");
+ is( $res->code, 404 , '404 for view with no record' );
+ $res = $cb->(GET "/$controller/record/555/view");
+ is( $res->code, 404 , '404 for view with no record' );
+ }
+ );
+}
+
+done_testing();
@@ -0,0 +1,34 @@
+use Test::More;
+use Plack::Test;
+use HTTP::Request::Common;
+use DvdDatabase;
+use File::Copy;
+use Plack::Middleware::Session;
+use Test::WWW::Mechanize::PSGI;
+
+for my $controller( qw/DvdSimpleUrl/ ){
+ copy('t/data/dvdzbr.db','dvdzbr.db') or die "Copy failed: $!";
+
+ my $app = Plack::Middleware::Session->wrap( DvdDatabase->new()->psgi_callback );
+ test_psgi(
+ app => $app,
+ client => sub {
+ my $cb = shift;
+ my $res;
+ $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->(GET "/$controller/5/view");
+ like( $res->content, qr/Not Jurassic Park/, 'view' );
+ $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' );
+ }
+ );
+}
+
+done_testing();
Binary file not shown.
@@ -0,0 +1,10 @@
+<html>
+<body>
+<form method="post">
+<fieldset class="widget_fieldset">
+<input class="submit" id="widget_ok" name="ok" type="submit" value="Delete ?" />
+</fieldset>
+</form>
+</body>
+</html>
+
@@ -0,0 +1,6 @@
+<html>
+<body>
+[% form %]
+</body>
+</html>
+
Oops, something went wrong.

0 comments on commit 67e4554

Please sign in to comment.