[Catalyst-commits] r9471 - in trunk/Catalyst-Example-InstantCRUD:
lib/Catalyst/Example/Controller
lib/Catalyst/Example/Controller/InstantCRUD
lib/Catalyst/Helper lib/Catalyst/Helper/Controller
lib/Catalyst/Helper/View script t
zby at dev.catalyst.perl.org
zby at dev.catalyst.perl.org
Mon Mar 9 06:45:26 GMT 2009
Author: zby
Date: 2009-03-09 06:45:26 +0000 (Mon, 09 Mar 2009)
New Revision: 9471
Added:
trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD/
trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD/REST.pm
trunk/Catalyst-Example-InstantCRUD/t/30.create_rest.t
Modified:
trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD.pm
trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/Controller/InstantCRUD.pm
trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/InstantCRUD.pm
trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/View/InstantCRUD.pm
trunk/Catalyst-Example-InstantCRUD/script/instantcrud.pl
Log:
rest switch
Added: trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD/REST.pm
===================================================================
--- trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD/REST.pm (rev 0)
+++ trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD/REST.pm 2009-03-09 06:45:26 UTC (rev 9471)
@@ -0,0 +1,229 @@
+use strict;
+use warnings;
+
+package Catalyst::Example::Controller::InstantCRUD::REST;
+
+use base 'Catalyst::Example::Controller::InstantCRUD';
+
+use Carp;
+use Data::Dumper;
+use Rose::HTMLx::Form::DBIC;
+
+use version; our $VERSION = qv('0.0.1');
+
+sub create_form : Local {
+ my ( $self, $c, @pks ) = @_;
+ $self->edit( $c, @pks );
+ $c->stash( template => 'edit.tt' );
+}
+
+sub by_id : Local : ActionClass('REST') { }
+
+sub by_id_GET : Local {
+ my ( $self, $c, @args ) = @_;
+ my @model_pks = $self->model_pks( $c );
+ my @pks = @args[ 0 .. scalar @model_pks - 1 ];
+ my $view_type = $args[ scalar @model_pks ];
+ $view_type = 'view' if $view_type ne 'edit';
+ my $item = $self->model_item( $c, @pks );
+ $c->stash->{item} = $item;
+ if( $view_type eq 'edit' ){
+ my $form_name = ref( $self ) . '::' . $self->source_name . 'Form';
+ my $form = $form_name->new();
+ $form->add_fields( 'x-tunneled-method' => { type => 'hidden', value => 'PUT' } );
+ my $rs = $self->model_resultset($c);
+ my $processor = Rose::HTMLx::Form::DBIC->new( form => $form, rs => $rs );
+ my $params = $c->req->params;
+ $processor->init_params( $params );
+ $processor->init_from_dbic(@pks) if scalar @pks;
+ $c->stash( form => $form );
+ }
+ $c->stash( template => $view_type . '.tt' );
+}
+
+sub by_id_PUT : Local {
+ my ( $self, $c, @args ) = @_;
+ my @model_pks = $self->model_pks( $c );
+ my @pks = @args[ 0 .. scalar @model_pks - 1 ];
+ my $form_name = ref( $self ) . '::' . $self->source_name . 'Form';
+ my $form = $form_name->new();
+ $form->add_fields( 'x-tunneled-method' => { type => 'hidden', value => 'PUT' } );
+ my $rs = $self->model_resultset($c);
+ my $processor = Rose::HTMLx::Form::DBIC->new( form => $form, rs => $rs );
+ my $params = $c->req->params;
+ $processor->init_params( $params );
+ my $item = $processor->dbic_from_form(@pks);
+ if( $item ){
+ my @new_pks = map { $item->$_ } @model_pks;
+ $c->res->redirect( $c->uri_for( 'by_id', @new_pks ) );
+ }
+ else{
+ $c->stash( form => $form );
+ $c->stash( template => 'edit.tt' );
+ }
+}
+
+
+1;
+
+__END__
+
+=head1 NAME
+
+Catalyst::Example::Controller::InstantCRUD::REST - Catalyst CRUD example RESTful Controller
+
+
+=head1 VERSION
+
+This document describes Catalyst::Example::Controller::InstantCRUD::REST version 0.0.1
+
+
+=head1 SYNOPSIS
+
+ use base Catalyst::Example::Controller::InstantCRUD::REST;
+
+=for author to fill in:
+ Brief code example(s) here showing commonest usage(s).
+ This section will be as far as many users bother reading
+ so make it as educational and exeplary as possible.
+
+
+=head1 DESCRIPTION
+
+=for author to fill in:
+ Write a full description of the module and its features here.
+ Use subsections (=head2, =head3) as appropriate.
+
+
+=head1 INTERFACE
+
+=head2 METHODS
+
+=over 4
+
+=item by_id
+The main dispatch point.
+
+=item by_id_PUT
+Updates an object (or creates one).
+
+=item by_id_GET
+Shows object representation
+
+=item create_form
+Form for object creation.
+
+=back
+
+=head1 DIAGNOSTICS
+
+=for author to fill in:
+ List every single error and warning message that the module can
+ generate (even the ones that will "never happen"), with a full
+ explanation of each problem, one or more likely causes, and any
+ suggested remedies.
+
+=over
+
+=item C<< Error message here, perhaps with %s placeholders >>
+
+[Description of error here]
+
+=item C<< Another error message here >>
+
+[Description of error here]
+
+[Et cetera, et cetera]
+
+=back
+
+
+=head1 CONFIGURATION AND ENVIRONMENT
+
+=for author to fill in:
+ A full explanation of any configuration system(s) used by the
+ module, including the names and locations of any configuration
+ files, and the meaning of any environment variables or properties
+ that can be set. These descriptions must also include details of any
+ configuration language used.
+
+Catalyst::Example::Controller::InstantCRUD requires no configuration files or environment variables.
+
+
+=head1 DEPENDENCIES
+
+=for author to fill in:
+ A list of all the other modules that this module relies upon,
+ including any restrictions on versions, and an indication whether
+ the module is part of the standard Perl distribution, part of the
+ module's distribution, or must be installed separately. ]
+
+None.
+
+
+=head1 INCOMPATIBILITIES
+
+=for author to fill in:
+ A list of any modules that this module cannot be used in conjunction
+ with. This may be due to name conflicts in the interface, or
+ competition for system or program resources, or due to internal
+ limitations of Perl (for example, many modules that use source code
+ filters are mutually incompatible).
+
+None reported.
+
+
+=head1 BUGS AND LIMITATIONS
+
+=for author to fill in:
+ A list of known problems with the module, together with some
+ indication Whether they are likely to be fixed in an upcoming
+ release. Also a list of restrictions on the features the module
+ does provide: data types that cannot be handled, performance issues
+ and the circumstances in which they may arise, practical
+ limitations on the size of data sets, special cases that are not
+ (yet) handled, etc.
+
+No bugs have been reported.
+
+Please report any bugs or feature requests to
+C<bug-catalyst-example-controller-instantcrud at rt.cpan.org>, or through the web interface at
+L<http://rt.cpan.org>.
+
+
+=head1 AUTHOR
+
+<Zbigniew Lukasiak> C<< <<zz bb yy @ gmail.com>> >>
+<Jonas Alves> C<< <<jonas.alves at gmail.com>> >>
+<Lars Balker Rasmussen>
+
+=head1 LICENCE AND COPYRIGHT
+
+Copyright (c) 2005, <Zbigniew Lukasiak> C<< <<zz bb yy @ gmail.com>> >>. All rights reserved.
+
+This module is free software; you can redistribute it and/or
+modify it under the same terms as Perl itself. See L<perlartistic>.
+
+
+=head1 DISCLAIMER OF WARRANTY
+
+BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
+EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
+ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
+YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
+NECESSARY SERVICING, REPAIR, OR CORRECTION.
+
+IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
+LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
+OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
+THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
Modified: trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD.pm
===================================================================
--- trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD.pm 2009-03-09 00:17:49 UTC (rev 9470)
+++ trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD.pm 2009-03-09 06:45:26 UTC (rev 9471)
@@ -1,11 +1,10 @@
-package Catalyst::Example::Controller::InstantCRUD;
-
-use warnings;
use strict;
-#use base 'Catalyst::Controller';
+use warnings;
-use base 'Catalyst::Controller::REST';
+package Catalyst::Example::Controller::InstantCRUD;
+use base 'Catalyst::Controller';
+
use Carp;
use Data::Dumper;
use Path::Class;
@@ -91,59 +90,6 @@
$c->stash( form => $form );
}
-sub create_form : Local {
- my ( $self, $c, @pks ) = @_;
- $self->edit( $c, @pks );
- $c->stash( template => 'edit.tt' );
-}
-
-sub by_id : Local : ActionClass('REST') { }
-
-sub by_id_GET : Local {
- my ( $self, $c, @args ) = @_;
- my @model_pks = $self->model_pks( $c );
- my @pks = @args[ 0 .. scalar @model_pks - 1 ];
- my $view_type = $args[ scalar @model_pks ];
- $view_type = 'view' if $view_type ne 'edit';
- my $item = $self->model_item( $c, @pks );
- $c->stash->{item} = $item;
- if( $view_type eq 'edit' ){
- my $form_name = ref( $self ) . '::' . $self->source_name . 'Form';
- my $form = $form_name->new();
- $form->add_fields( 'x-tunneled-method' => { type => 'hidden', value => 'PUT' } );
- my $rs = $self->model_resultset($c);
- my $processor = Rose::HTMLx::Form::DBIC->new( form => $form, rs => $rs );
- my $params = $c->req->params;
- $processor->init_params( $params );
- $processor->init_from_dbic(@pks) if scalar @pks;
- $c->stash( form => $form );
- }
- $c->stash( template => $view_type . '.tt' );
-}
-
-sub by_id_PUT : Local {
- my ( $self, $c, @args ) = @_;
- my @model_pks = $self->model_pks( $c );
- my @pks = @args[ 0 .. scalar @model_pks - 1 ];
- my $form_name = ref( $self ) . '::' . $self->source_name . 'Form';
- my $form = $form_name->new();
- $form->add_fields( 'x-tunneled-method' => { type => 'hidden', value => 'PUT' } );
- my $rs = $self->model_resultset($c);
- my $processor = Rose::HTMLx::Form::DBIC->new( form => $form, rs => $rs );
- my $params = $c->req->params;
- $processor->init_params( $params );
- my $item = $processor->dbic_from_form(@pks);
- if( $item ){
- my @new_pks = map { $item->$_ } @model_pks;
- $c->res->redirect( $c->uri_for( 'by_id', @new_pks ) );
- }
- else{
- $c->stash( form => $form );
- $c->stash( template => 'edit.tt' );
- }
-}
-
-
sub view : Local {
my ( $self, $c, @pks ) = @_;
die "You need to pass an id" unless @pks;
@@ -251,6 +197,9 @@
=item model_item
Returns an item from the model.
+=item model_pks
+Returns columns comprising the primary key.
+
=item source_name
Class method for finding name of corresponding database table.
Modified: trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/Controller/InstantCRUD.pm
===================================================================
--- trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/Controller/InstantCRUD.pm 2009-03-09 00:17:49 UTC (rev 9470)
+++ trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/Controller/InstantCRUD.pm 2009-03-09 06:45:26 UTC (rev 9471)
@@ -55,7 +55,8 @@
use warnings;
package [% class %];
-use base "Catalyst::Example::Controller::InstantCRUD";
+[% IF rest %]
+use base "Catalyst::Example::Controller::InstantCRUD::REST";
__PACKAGE__->config(
serialize => {
default => 'text/html',
@@ -64,6 +65,9 @@
}
}
);
+[% ELSE %]
+use base "Catalyst::Example::Controller::InstantCRUD";
+[% END %]
[% form_code %]
Modified: trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/InstantCRUD.pm
===================================================================
--- trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/InstantCRUD.pm 2009-03-09 00:17:49 UTC (rev 9470)
+++ trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/InstantCRUD.pm 2009-03-09 06:45:26 UTC (rev 9471)
@@ -36,13 +36,13 @@
=begin pod_to_ignore
__appclass__
-package [% name %];
-
use strict;
use warnings;
+package [% name %];
+
use Catalyst::Runtime '5.70';
-use Catalyst::Request::REST::ForBrowsers;
+[% IF rest %]use Catalyst::Request::REST::ForBrowsers;[% END %]
use Catalyst qw/
-Debug
@@ -63,7 +63,7 @@
our $VERSION = '0.01';
__PACKAGE__->config( name => '[% name %]' );
-__PACKAGE__->request_class( 'Catalyst::Request::REST::ForBrowsers' );
+[% IF rest %]__PACKAGE__->request_class( 'Catalyst::Request::REST::ForBrowsers' );[% END %]
# Start the application
__PACKAGE__->setup;
Modified: trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/View/InstantCRUD.pm
===================================================================
--- trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/View/InstantCRUD.pm 2009-03-09 00:17:49 UTC (rev 9470)
+++ trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Helper/View/InstantCRUD.pm 2009-03-09 06:45:26 UTC (rev 9471)
@@ -185,15 +185,15 @@
</td>
<+ END +>
[% SET id = row.$pri %]
- <td><a href="[% c.uri_for( 'view', <+ FOR key = primary_keys +>row.<+ key +>, <+ END +> ) %]">View</a></td>
- <td><a href="[% c.uri_for( 'edit', <+ FOR key = primary_keys +>row.<+ key +>, <+ END +> ) %]">Edit</a></td>
- <td><a href="[% c.uri_for( 'destroy', <+ FOR key = primary_keys +>row.<+ key +>, <+ END +> ) %]">Destroy</a></td>
+ <td><a href="[% c.uri_for( <+ IF rest +>'by_id'<+ ELSE +>'view'<+ END +>, <+ FOR key = primary_keys +>row.<+ key +>, <+ END +> ) %]">View</a></td>
+ <td><a href="[% c.uri_for( <+ IF rest +>'by_id'<+ ELSE +>'edit'<+ END +>, <+ FOR key = primary_keys +>row.<+ key +>, <+ END +><+ IF rest +>,'edit'<+ END +> ) %]">Edit</a></td>
+ <td><a href="[% c.uri_for( <+ IF rest +>'by_id'<+ ELSE +>'destroy'<+ END +>, <+ FOR key = primary_keys +>row.<+ key +>, <+ END +><+ IF rest +>,'destroy'<+ END +> ) %]">Destroy</a></td>
</tr>
[% END %]
</table>
[% PROCESS pager.tt %]
<br/>
-<a href="[% c.uri_for( 'edit' ) %]">Add</a>
+<a href="[% c.uri_for( 'create_form' ) %]">Add</a>
__view__
[% TAGS <+ +> %]
Modified: trunk/Catalyst-Example-InstantCRUD/script/instantcrud.pl
===================================================================
--- trunk/Catalyst-Example-InstantCRUD/script/instantcrud.pl 2009-03-09 00:17:49 UTC (rev 9470)
+++ trunk/Catalyst-Example-InstantCRUD/script/instantcrud.pl 2009-03-09 06:45:26 UTC (rev 9471)
@@ -25,18 +25,19 @@
my $scripts = 0;
my $short = 0;
my $auth = 1;
+my $rest = 0;
my $dsn;
my $duser;
my $dpassword;
my $model_name = 'DBICSchemamodel';
my $schema_name = 'DBSchema';
-
my %auth;
my %authz;
GetOptions(
'help|?' => \$help,
'advanced_help' => \$adv_help,
+ 'rest' => \$rest,
'nonew' => \$nonew,
'scripts' => \$scripts,
'short' => \$short,
@@ -72,6 +73,7 @@
'schema_name' => $schema_name,
'auth' => \%auth,
'authz' => \%authz,
+ 'rest' => $rest,
} );
if( ! $helper->mk_app( $appname ) ){
Added: trunk/Catalyst-Example-InstantCRUD/t/30.create_rest.t
===================================================================
--- trunk/Catalyst-Example-InstantCRUD/t/30.create_rest.t (rev 0)
+++ trunk/Catalyst-Example-InstantCRUD/t/30.create_rest.t 2009-03-09 06:45:26 UTC (rev 9471)
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use Test::More tests => 1;
+use Path::Class;
+use File::Path;
+use File::Copy;
+
+my $app = 'DVDzbr_rest';
+my $lcapp = lc $app;
+
+rmtree( ["t/tmp/$app", "t/tmp/$lcapp.db"] );
+
+`cd t/tmp; $^X -I../../lib ../../script/instantcrud.pl $app -rest`;
+
+ok( -f "t/tmp/$app/lib/$app/DBSchema.pm", 'DBSchema creation');
+
+
More information about the Catalyst-commits
mailing list