[Catalyst-commits] r9434 -
trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller
zby at dev.catalyst.perl.org
zby at dev.catalyst.perl.org
Thu Mar 5 16:33:26 GMT 2009
Author: zby
Date: 2009-03-05 16:33:25 +0000 (Thu, 05 Mar 2009)
New Revision: 9434
Modified:
trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD.pm
Log:
start of REST
Modified: trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD.pm
===================================================================
--- trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD.pm 2009-03-05 02:54:01 UTC (rev 9433)
+++ trunk/Catalyst-Example-InstantCRUD/lib/Catalyst/Example/Controller/InstantCRUD.pm 2009-03-05 16:33:25 UTC (rev 9434)
@@ -2,8 +2,10 @@
use warnings;
use strict;
-use base 'Catalyst::Controller';
+#use base 'Catalyst::Controller';
+use base 'Catalyst::Controller::REST';
+
use Carp;
use Data::Dumper;
use Path::Class;
@@ -30,6 +32,13 @@
return $item;
}
+sub model_pks {
+ my ( $self, $c ) = @_;
+ my $rs = $self->model_resultset($c);
+ my @pks = $rs->result_source->primary_columns;
+ return @pks;
+}
+
sub model_resultset {
my ( $self, $c ) = @_;
my $model_name = $c->config->{InstantCRUD}{model_name};
@@ -82,6 +91,65 @@
$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 );
+ warn "model pks: @model_pks";
+ my @pks = @args[ 0 .. scalar @model_pks - 1 ];
+ warn "pks: @pks";
+ my $view_type = $args[ scalar @model_pks ] || 'view';
+ warn "view_type: $view_type";
+ 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 );
+ warn "model pks: @model_pks";
+ my @pks = @args[ 0 .. scalar @model_pks - 1 ];
+ warn "pks: @pks";
+ my $item = $self->model_item( $c, @pks );
+ $c->stash->{item} = $item;
+ 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 ){
+ $c->res->redirect( $c->uri_for( 'view', $item->id ) );
+ $c->stash( item => $item );
+ }
+ 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;
More information about the Catalyst-commits
mailing list