[Catalyst-commits] r7937 - Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Sun Jun 22 11:30:20 BST 2008


Author: lukes
Date: 2008-06-22 11:30:20 +0100 (Sun, 22 Jun 2008)
New Revision: 7937

Modified:
   Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm
Log:
update working for direct belongs_to rels

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2008-06-20 18:14:11 UTC (rev 7936)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2008-06-22 10:30:20 UTC (rev 7937)
@@ -5,11 +5,13 @@
 use base qw/Catalyst::Controller CGI::Expand/;
 
 __PACKAGE__->mk_accessors(qw(
-  class create_requires update_requires update_allows class_rs create_allows list_returns
+  class create_requires update_requires update_allows create_allows list_returns rs_stash_key object_stash_key
 ));
 
 __PACKAGE__->config(
   class => undef,
+  rs_stash_key => 'class_rs',
+  object_stash_key => 'object',			   
   create_requires => [],
   create_allows => [],
   update_requires => [],
@@ -41,7 +43,7 @@
 
 =head2 setup
 
-Just sets $c->stash->{class_rs} to the resultset specified by the 'class' config.
+Just sets $c->stash->{$self->rs_stash_key} to the resultset specified by the 'class' config.
 This rs is then used by all other methods.
 
 =cut  
@@ -49,7 +51,7 @@
 sub setup :Chained('specify.in.subclass.config') :CaptureArgs(0) :PathPart('specify.in.subclass.config') {
 	my ($self, $c) = @_;
 
-	$c->stash->{class_rs} = $c->model($self->class);
+	$c->stash->{$self->rs_stash_key} = $c->model($self->class);
 }
 
 =head2 list
@@ -57,11 +59,10 @@
 =cut
 
 sub list :Private {
-	my ($self, $c) = @_;
+  my ($self, $c) = @_;
 
-  my $req_params = $self->expand_hash($c->req->params);
-
-  my $source = $c->stash->{class_rs}->result_source;
+  my $req_params = (grep { ref $_ } values %{$c->req->params}) ? $c->req->params : $self->expand_hash($c->req->params);
+  my $source = $c->stash->{$self->rs_stash_key}->result_source;
   my @columns = (scalar(@{$self->list_returns})) ? @{$self->list_returns} : $source->columns;
 
   my %search_params;
@@ -69,9 +70,23 @@
       %search_params = map { $_ => $req_params->{search}->{$_} } grep { exists $req_params->{search}->{$_} } $source->columns;
   }
 
-  $c->stash->{class_rs} = $c->stash->{class_rs}->search(\%search_params, { select => \@columns });
+#  use Data::Dumper; warn Dumper(\%search_params);
+  $c->stash->{$self->rs_stash_key} = $c->stash->{$self->rs_stash_key}->search(\%search_params, { select => \@columns });
 
-  my @ret = map { { $_->get_columns } } $c->stash->{class_rs}->all;
+  $c->forward('format_list');
+}
+
+
+=head2 list
+
+override this to customise the format of list
+
+=cut
+
+sub format_list :Private {
+  my ($self, $c) = @_;
+
+  my @ret = map { { $_->get_columns } } $c->stash->{$self->rs_stash_key}->all;
   $c->stash->{response}->{list} = \@ret;
 }
 
@@ -88,52 +103,54 @@
 	unless (ref($self->create_requires) eq 'ARRAY') {
 		die "create_requires must be an arrayref in config";
 	}
-	unless ($c->stash->{class_rs}) {
+	unless ($c->stash->{$self->rs_stash_key}) {
 		die "class resultset not set";
 	}
 
-	my $empty_object = $c->stash->{class_rs}->new_result({});
+	my $empty_object = $c->stash->{$self->rs_stash_key}->new_result({});
 	$self->validate_and_save_object($c, $empty_object);
 }
 
 =head2 update
 
 Matches cols specified in update_allows to $c->req->params and $c->stash->{params} 
-to create a hash which is used to update $c->stash->{object}
+to create a hash which is used to update $c->stash->{$self->object_stash_key}
 
 =cut
 
 sub update :Private {
 	my ($self, $c) = @_;
 
-	my $req_params = $self->expand_hash($c->req->params);
+	# expand params unless they have already been expanded
+	my $req_params = (grep { ref $_ } values %{$c->req->params}) ? $c->req->params : $self->expand_hash($c->req->params);
+
 	$c->req->params($req_params);
-	return unless ($c->stash->{object});
+	return unless ($c->stash->{$self->object_stash_key});
 
 	unless (ref($self->update_allows) eq 'ARRAY') {
 		die "update_allows must be an arrayref in config";
 	}
-	unless ($c->stash->{class_rs}) {
+	unless ($c->stash->{$self->rs_stash_key}) {
 		die "class resultset not set";
 	}
 
 #	use Data::Dumper; $c->log->debug(Dumper(\%create_args));
-	my $object = $c->stash->{object};
+	my $object = $c->stash->{$self->object_stash_key};
 	$self->validate_and_save_object($c, $object);
 }
 
 =head2 delete
 
-	Deletes $c->stash->{object}
+	Deletes $c->stash->{$self->object_stash_key}
 
 =cut
 
 sub delete :Private {
 	my ($self, $c) = @_;
 
-	return unless ($c->stash->{object});
+	return unless ($c->stash->{$self->object_stash_key});
 
-	return $c->stash->{object}->delete;
+	return $c->stash->{$self->object_stash_key}->delete;
 }
 
 =head2 validate_and_save_object
@@ -288,7 +305,7 @@
 # 	my $related_rs;
 # 	# pretty grim but i want it to work for many_to_many as well (so not $source->has_relationship)
 # 	eval {
-# 		$related_rs = $c->stash->{object}->$rel;
+# 		$related_rs = $c->stash->{$self->object_stash_key}->$rel;
 # 	};
 # 	if ($@) {
 # 		$c->detach( 'error', [{ message => "Invalid relationship $rel" }]);		
@@ -306,7 +323,7 @@
 # 		$c->detach( 'error', [{ message => "Invalid related row" }]);
 # 	}
 
-# 	$c->stash->{object}->$accessor($related_row);
+# 	$c->stash->{$self->object_stash_key}->$accessor($related_row);
 # }
 
 sub push_error {

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm	2008-06-20 18:14:11 UTC (rev 7936)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm	2008-06-22 10:30:20 UTC (rev 7937)
@@ -65,11 +65,12 @@
 sub object :Chained('setup') :Args(1) :PathPart('id') :ActionClass('REST') {
 	my ($self, $c, $id) = @_;
 
-	my $object = $c->stash->{class_rs}->find( $id );
+	my $object = $c->stash->{$self->rs_stash_key}->find( $id );
 	unless ($object) {
 		$self->push_error($c, { message => "Invalid id" });
 	}
-	$c->stash->{object} = $object;
+
+	$c->stash->{$self->object_stash_key} = $object if ($self->object_stash_key);
 }
 
 sub object_POST {

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm	2008-06-20 18:14:11 UTC (rev 7936)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm	2008-06-22 10:30:20 UTC (rev 7937)
@@ -6,11 +6,11 @@
 use JSON::Syck;
 
 __PACKAGE__->config(
-						'default'   => 'text/x-json',
+						'default'   => 'application/json',
 						'stash_key' => 'response',
 						'map'       => {
 							'application/x-www-form-urlencoded'        => 'JSON',
-							'text/x-json'        => 'JSON',
+							'application/json'        => 'JSON',
 						});
 =head1 NAME
 
@@ -59,11 +59,12 @@
 sub object :Chained('setup') :CaptureArgs(1) :PathPart('id') {
 	my ($self, $c, $id) = @_;
 
-	my $object = $c->stash->{class_rs}->find( $id );
+	my $object = $c->stash->{$self->rs_stash_key}->find( $id );
 	unless ($object) {
 		$self->push_error($c, { message => "Invalid id" });
 	}
-	$c->stash->{object} = $object;
+
+	$c->stash->{$self->object_stash_key} = $object;
 }
 
 sub index : Chained('setup') PathPart('') Args(0) {
@@ -101,7 +102,7 @@
 =head2 update
 
 Matches cols specified in update_allows to $c->req->params and $c->stash->{params} 
-to create a hash which is used to update $c->stash->{object}
+to create a hash which is used to update $c->stash->{$self->object_stash_key}
 
 =cut
 
@@ -113,7 +114,7 @@
 
 =head2 delete
 
-	Deletes $c->stash->{object}
+	Deletes $c->stash->{$self->object_stash_key}
 
 =cut
 
@@ -157,7 +158,7 @@
 # 	my $related_rs;
 # 	# pretty grim but i want it to work for many_to_many as well (so not $source->has_relationship)
 # 	eval {
-# 		$related_rs = $c->stash->{object}->$rel;
+# 		$related_rs = $c->stash->{$self->object_stash_key}->$rel;
 # 	};
 # 	if ($@) {
 # 		$c->detach( 'error', [{ message => "Invalid relationship $rel" }]);		
@@ -175,7 +176,7 @@
 # 		$c->detach( 'error', [{ message => "Invalid related row" }]);
 # 	}
 	
-# 	$c->stash->{object}->$accessor($related_row);
+# 	$c->stash->{$self->object_stash_key}->$accessor($related_row);
 # }
 
 




More information about the Catalyst-commits mailing list