[Catalyst-commits] r8147 - in Catalyst-Controller-DBIC-API/1.000/trunk: lib/Catalyst/Controller/DBIC/API t/lib/RestTest/Controller/API/RPC t/rpc t/var

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Tue Jul 22 13:24:19 BST 2008


Author: lukes
Date: 2008-07-22 13:24:19 +0100 (Tue, 22 Jul 2008)
New Revision: 8147

Added:
   Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Any.pm
Modified:
   Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/create.t
   Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/update.t
   Catalyst-Controller-DBIC-API/1.000/trunk/t/var/DBIxClass.db
Log:
added support for using config values from the stash

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-07-21 22:35:16 UTC (rev 8146)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2008-07-22 12:24:19 UTC (rev 8147)
@@ -89,8 +89,8 @@
 sub create :Private {
 	my ($self, $c) = @_;
 
-	unless (ref($self->create_requires) eq 'ARRAY') {
-		die "create_requires must be an arrayref in config";
+	unless (ref($c->stash->{create_requires} || $self->create_requires) eq 'ARRAY') {
+		die "create_requires must be an arrayref in config or stash";
 	}
 	unless ($c->stash->{$self->rs_stash_key}) {
 		die "class resultset not set";
@@ -109,8 +109,8 @@
 	$c->req->params($req_params);
 	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 (ref($c->stash->{update_allows} || $self->update_allows) eq 'ARRAY') {
+		die "update_allows must be an arrayref in config or stash";
 	}
 	unless ($c->stash->{$self->rs_stash_key}) {
 		die "class resultset not set";
@@ -144,8 +144,8 @@
 	my $params = $c->req->params;
 
 	my %values;
-	my %requires_map = map { $_ => 1 } @{($object->in_storage) ? [] : $self->create_requires};
-	my %allows_map = map { (ref $_) ? %{$_} : ($_ => 1) } (keys %requires_map, @{($object->in_storage) ? $self->update_allows : $self->create_allows});
+	my %requires_map = map { $_ => 1 } @{($object->in_storage) ? [] : $c->stash->{create_requires} || $self->create_requires};
+	my %allows_map = map { (ref $_) ? %{$_} : ($_ => 1) } (keys %requires_map, @{($object->in_storage) ? ($c->stash->{update_allows} || $self->update_allows) : $self->create_allows});
 	
 #	use Data::Dumper; warn Dumper($params, \%requires_map, \%allows_map);
 	foreach my $key (keys %allows_map) {

Added: Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Any.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Any.pm	                        (rev 0)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Any.pm	2008-07-22 12:24:19 UTC (rev 8147)
@@ -0,0 +1,28 @@
+package RestTest::Controller::API::RPC::Any;
+
+use strict;
+use warnings;
+use base qw/Catalyst::Controller::DBIC::API::RPC/;
+use JSON::Syck;
+
+sub setup :Chained('/api/rpc/rpc_base') :CaptureArgs(1) :PathPart('any') {
+  my ($self, $c, $object_type) = @_;
+
+  my $config = {};
+  if ($object_type eq 'artist') {
+    $config->{class} = 'Artist';
+    $config->{create_requires} = [qw/name/];
+    $config->{update_allows} = [qw/name/];
+  } elsif ($object_type eq 'track') {
+    $config->{class} = 'Track';
+    $config->{update_allows} = [qw/title position/];
+  } else {
+    $self->push_error($c, { message => "invalid object_type" });
+    return;
+  }
+
+  $c->stash->{$self->rs_stash_key} = $c->model('RestTestDB::' . $config->{class});
+  $c->stash->{$_} = $config->{$_} for keys %{$config};
+}
+
+1;

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/create.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/create.t	2008-07-21 22:35:16 UTC (rev 8146)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/create.t	2008-07-22 12:24:19 UTC (rev 8147)
@@ -10,7 +10,7 @@
 
 use RestTest;
 use DBICTest;
-use Test::More tests => 8;
+use Test::More tests => 11;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
 use JSON::Syck;
@@ -19,6 +19,7 @@
 ok(my $schema = DBICTest->init_schema(), 'got schema');
 
 my $artist_create_url = "$base/api/rpc/artist/create";
+my $any_artist_create_url = "$base/api/rpc/any/artist/create";
 my $producer_create_url = "$base/api/rpc/producer/create";
 
 # test validation when no params sent
@@ -57,3 +58,18 @@
   my $response = JSON::Syck::Load( $mech->content);
   is_deeply( $response->{new_producer}, { $new_obj->get_columns }, 'json for new producer returned' );
 }
+
+# test stash config handling
+{
+  my $req = POST( $any_artist_create_url, {
+	  name => 'queen monkey'
+  }, 'Accept' => 'text/json' );
+  $mech->request($req, $content_type);
+  cmp_ok( $mech->status, '==', 200, 'stashed config okay' );
+
+  my $new_obj = $schema->resultset('Artist')->find({ name => 'queen monkey' });
+  ok($new_obj, 'record created with specified name');
+
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply( $response, { success => 'true' }, 'json for new artist returned' );
+}

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/update.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/update.t	2008-07-21 22:35:16 UTC (rev 8146)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/update.t	2008-07-22 12:24:19 UTC (rev 8147)
@@ -10,7 +10,7 @@
 
 use RestTest;
 use DBICTest;
-use Test::More tests => 21;
+use Test::More tests => 23;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
 use JSON::Syck;
@@ -22,6 +22,7 @@
 my %original_cols = $track->get_columns;
 
 my $track_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
+my $any_track_update_url = "$base/api/rpc/any/track/id/" . $track->id . "/update";
 
 # test invalid track id caught
 {
@@ -106,3 +107,14 @@
   is($track->title, 'sheep sheep', 'Title changed');
   is($track->cd->year, '2009', 'Related field changed"');
 }
+
+{
+  my $req = POST( $any_track_update_url, {
+	  title => 'baa'
+  });
+  $mech->request($req, $content_type);
+  cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
+
+  $track->discard_changes;
+  is($track->title, 'baa', 'Title changed');
+}

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/var/DBIxClass.db
===================================================================
(Binary files differ)




More information about the Catalyst-commits mailing list