[Catalyst-commits] r8173 - in CatalystX-CRUD/CatalystX-CRUD/trunk:
. lib/CatalystX/CRUD lib/CatalystX/CRUD/Model
lib/CatalystX/CRUD/Test
karpet at dev.catalyst.perl.org
karpet at dev.catalyst.perl.org
Thu Jul 31 17:42:52 BST 2008
Author: karpet
Date: 2008-07-31 17:42:52 +0100 (Thu, 31 Jul 2008)
New Revision: 8173
Modified:
CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/Utils.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/REST.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Controller.pm
Log:
support multi-column PKs
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/Changes 2008-07-31 16:42:29 UTC (rev 8172)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/Changes 2008-07-31 16:42:52 UTC (rev 8173)
@@ -129,5 +129,6 @@
0.28 xxx
* API for ModelAdapter changed to pass controller instance in do_model()
- * add get_primary_key() method to base Controller. This allows for PKs composed of multiple columns.
+ * add get_primary_key() and make_primary_key_string() methods to base Controller.
+ This allows for PKs composed of multiple columns.
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm 2008-07-31 16:42:29 UTC (rev 8172)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm 2008-07-31 16:42:52 UTC (rev 8173)
@@ -177,6 +177,7 @@
sub get_primary_key {
my ( $self, $c, $id ) = @_;
+ return () unless defined $id;
my $pk = $self->primary_key;
my @ret;
if ( ref $pk ) {
@@ -191,6 +192,39 @@
return @ret;
}
+=head2 make_primary_key_string( I<object> )
+
+Using value of B<primary_string> constructs a URI-ready
+string based on values in I<object>. I<object> is often
+the value of:
+
+ $c->stash->{object}
+
+but could be any object that has accessor methods with
+the same names as the field(s) specified by B<primary_key>.
+
+=cut
+
+sub make_primary_key_string {
+ my ( $self, $obj ) = @_;
+ my $pk = $self->primary_key;
+ my $id;
+ if ( ref $pk ) {
+ my @vals;
+ for my $field (@$pk) {
+ my $v = scalar $obj->$field;
+ $v = '' unless defined $v;
+ $v =~ s/;/ sprintf( "%%%02X", ';' ) /eg;
+ push( @vals, $v );
+ }
+ $id = join( ';;', @vals );
+ }
+ else {
+ $id = $obj->$pk;
+ }
+ return $id;
+}
+
=head2 create
Attribute: Local
@@ -589,13 +623,14 @@
sub postcommit {
my ( $self, $c, $o ) = @_;
- my $pk = $self->primary_key;
+ my $id = $self->make_primary_key_string($o);
+
if ( $c->action->name eq 'rm' ) {
$c->response->redirect( $c->uri_for('') );
}
else {
- $c->response->redirect( $c->uri_for( '', $o->$pk, 'view' ) );
+ $c->response->redirect( $c->uri_for( '', $id, 'view' ) );
}
1;
@@ -614,14 +649,13 @@
sub view_on_single_result {
my ( $self, $c, $results ) = @_;
return 0 unless $self->config->{view_on_single_result};
- my $pk = $self->primary_key;
my $obj = $results->[0];
+ my $id = $self->make_primary_key_string($obj);
# the append . '' is to force stringify anything
# that might be an object with overloading. Otherwise
# uri_for() assumes it is an Action object.
- return $c->uri_for( $obj->$pk . '',
- $self->can_write($c) ? 'edit' : 'view' );
+ return $c->uri_for( $id . '', $self->can_write($c) ? 'edit' : 'view' );
}
=head2 make_query( I<context>, I<arg> )
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/Utils.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/Utils.pm 2008-07-31 16:42:29 UTC (rev 8172)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/Utils.pm 2008-07-31 16:42:52 UTC (rev 8173)
@@ -107,7 +107,8 @@
return $params->{'_order'} if defined $params->{'_order'};
return join( ' ', $params->{'_sort'}, $params->{'_dir'} )
if defined( $params->{'_sort'} ) && defined( $params->{'_dir'} );
- return $c->controller->primary_key . ' DESC';
+ my %pks = $c->controller->get_primary_key($c);
+ return join( ' ', map { $_ . ' DESC' } keys %pks );
}
sub make_sql_query {
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/REST.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/REST.pm 2008-07-31 16:42:29 UTC (rev 8172)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/REST.pm 2008-07-31 16:42:52 UTC (rev 8173)
@@ -217,13 +217,13 @@
sub postcommit {
my ( $self, $c, $o ) = @_;
- my $pk = $self->primary_key;
+ my $id = $self->make_primary_key_string($o);
if ( $c->action->name eq 'rm' ) {
$c->response->redirect( $c->uri_for('') );
}
else {
- $c->response->redirect( $c->uri_for( '', $o->$pk ) );
+ $c->response->redirect( $c->uri_for( '', $id ) );
}
1;
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Controller.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Controller.pm 2008-07-31 16:42:29 UTC (rev 8172)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Controller.pm 2008-07-31 16:42:52 UTC (rev 8173)
@@ -59,15 +59,17 @@
my $form_meth = $self->init_form;
# id always comes from url but not necessarily from form
- my $pk = $self->primary_key;
- my $id = $c->req->params->{$pk} || $c->stash->{object_id};
+ my $id = $c->stash->{object_id};
+ my %pk = $self->get_primary_key( $c, $id );
# initialize the form with the object's values
$form->$form_meth($obj);
# set param values from request
$form->params( $c->req->params );
- $form->param( $pk => $id );
+ for my $field ( keys %pk ) {
+ $form->param( $field => $pk{$field} );
+ }
# override form's values with those from params
# no_clear is important because we already initialized with object
@@ -84,16 +86,19 @@
# re-set object's values from the now-valid form
$form->$obj_meth($obj);
- # set id explicitly since there's some bug
- # with param() setting it in save()
- $obj->$pk($id);
+ # set PK(s) explicitly
+ for my $field ( keys %pk ) {
+ $obj->$field( $pk{$field} );
+ }
# let serial column work its magic
- $obj->$pk(undef)
- if ( !$obj->$pk || $obj->$pk eq '0' || $id eq '0' );
+ # if this is a first-time save (create)
+ if ( scalar( keys %pk ) == 1 or $id eq '0' ) {
+ my ( $field, $value ) = each %pk;
+ $obj->$field(undef)
+ if ( !$obj->$field || $obj->$field eq '0' || $value eq '0' );
+ }
- #carp "object $pk == $id ? " . $obj->$pk;
-
#carp $self->serialize_object( $c, $obj );
return $obj;
More information about the Catalyst-commits
mailing list