[Catalyst-commits] r9512 - in Catalyst-Controller-DBIC-API/1.001/trunk: lib/Catalyst/Controller/DBIC/API t/lib/RestTest/Controller/API/RPC t/lib/RestTest/Schema/ResultSet t/rpc

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Wed Mar 18 11:02:24 GMT 2009


Author: lukes
Date: 2009-03-18 11:02:24 +0000 (Wed, 18 Mar 2009)
New Revision: 9512

Modified:
   Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
   Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Controller/API/RPC/TrackExposed.pm
   Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Schema/ResultSet/Track.pm
   Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list_search_allows.t
Log:
list_search_allows working with related sources

Modified: Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2009-03-18 10:15:34 UTC (rev 9511)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2009-03-18 11:02:24 UTC (rev 9512)
@@ -94,8 +94,12 @@
   my %search_params;
 
 	# munge list_search_exposes into format that's easy to do with
-	my %valid = map { (ref $_) ? %{$_} : ($_ => 1) } @{$self->list_search_exposes};
-
+	my %valid = map { (ref $_) ? %{$_} : ($_ => 1) } @{$p->{_list_search_exposes} || $self->list_search_exposes};
+	if ($valid{'*'}) {
+		# if the wildcard is passed they can access any column or relationship
+		$valid{$_} = 1 for $source->columns;
+		$valid{$_} = ['*'] for $source->relationships;
+	}
 	# figure out the valid cols, defaulting to all cols if not specified
 	my @valid_cols = @{$self->list_search_exposes} ? (grep { $valid{$_} eq 1 } keys %valid) : $source->columns;
 
@@ -111,6 +115,7 @@
 		# XXX this is broken when attempting complex search 
 		# XXX clauses on a col like { col => { LIKE => '%dfdsfs%' } }
 		# XXX when rel and col have the same name
+		next if $valid{'*'};
 		if (ref $params->{$key} && $_rel_map{$key}) {
 			$self->push_error($c, { message => "${key} is not a valid relation" }) unless (exists $_rel_map{$key});
 		} else {			
@@ -128,14 +133,13 @@
 
   # build up related conditions
 	foreach my $rel (@valid_rels) {    
-    next if ($search_params{join('.', $base, $rel)}); # if it's a condition on the base source, then it's can't also be a rel
-		if (exists $params->{$rel}) {
-			my $rel_params;
-			($rel_params, $join->{$rel}) = $self->_format_search($c, { params => $params->{$rel}, source => $source->related_source($rel), base => $rel });
-			%search_params = ( %search_params, %{$rel_params} );
-		}
+    next if ($search_params{join('.', $base, $rel)}); # if it's a condition on the base source, then it's can't also be a rel		
+		next unless (exists $params->{$rel});
+		next unless (ref $params->{$rel});
+		my $rel_params;
+		($rel_params, $join->{$rel}) = $self->_format_search($c, { params => $params->{$rel}, source => $source->related_source($rel), base => $rel, _list_search_exposes => $valid{$rel} });
+		%search_params = ( %search_params, %{$rel_params} );
 	}
-
 	return (\%search_params, $join);
 }
 

Modified: Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Controller/API/RPC/TrackExposed.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Controller/API/RPC/TrackExposed.pm	2009-03-18 10:15:34 UTC (rev 9511)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Controller/API/RPC/TrackExposed.pm	2009-03-18 11:02:24 UTC (rev 9512)
@@ -10,7 +10,7 @@
       class => 'RestTestDB::Track',
       list_returns => [qw/position title/],
       list_ordered_by => [qw/position/],
-			list_search_exposes => [qw/position/, { cd => ['*'] }],
+			list_search_exposes => [qw/position/, { cd => [qw/title year pretend/, { artist => ['*'] }] }],
       );
 
 1;

Modified: Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Schema/ResultSet/Track.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Schema/ResultSet/Track.pm	2009-03-18 10:15:34 UTC (rev 9511)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Schema/ResultSet/Track.pm	2009-03-18 11:02:24 UTC (rev 9512)
@@ -7,7 +7,7 @@
 	my $self = shift;
 	my ($clause, $params) = @_;
 
-	
+	use Data::Dumper; warn Dumper($params);
   my $rs = $self->SUPER::search(@_);	
 }
 

Modified: Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list_search_allows.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list_search_allows.t	2009-03-18 10:15:34 UTC (rev 9511)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list_search_allows.t	2009-03-18 11:02:24 UTC (rev 9512)
@@ -77,14 +77,51 @@
 
 {
   my $uri = URI->new( $track_list_url );
+  $uri->query_form({ 'search.cd.artist' => '1' });
+  my $req = GET( $uri, {
+		
+  }, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 400, 'search on various cd fields not okay' );
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply({ success => 'false',messages => ["artist is not a valid column"]}, $response, 'correct message returned' );
+}
+
+{
+  my $uri = URI->new( $track_list_url );
   $uri->query_form({ 'search.cd.title' => 'Spoonful of bees', 'search.cd.year' => '1999' });
   my $req = GET( $uri, {
 		
   }, 'Accept' => 'text/x-json' );
   $mech->request($req);
   cmp_ok( $mech->status, '==', 200, 'search on various cd fields okay' );
-  warn $mech->content;
-  my $expected_response = map { { $_->get_columns } } $base_rs->search({ 'cd.year' => '1999', 'cd.title' => 'Spoonful of bees' }, { join => 'cd' })->all;
+  my @expected_response = map { { $_->get_columns } } $base_rs->search({ 'cd.year' => '1999', 'cd.title' => 'Spoonful of bees' }, { join => 'cd' })->all;
   my $response = JSON::Syck::Load( $mech->content);
-  is_deeply({ success => 'false',messages => ["title is not a valid column"]}, $response, 'correct message returned' );
+  is_deeply({ success => 'true',list => \@expected_response }, $response, 'correct message returned' );
 }
+
+{
+  my $uri = URI->new( $track_list_url );
+  $uri->query_form({ 'search.cd.title' => 'Spoonful of bees', 'search.cd.pretend' => '1999' });
+  my $req = GET( $uri, {
+		
+  }, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'search on various cd fields okay' );
+  my @expected_response = map { { $_->get_columns } } $base_rs->search({ 'cd.year' => '1999', 'cd.title' => 'Spoonful of bees' }, { join => 'cd' })->all;
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply({ success => 'true',list => \@expected_response }, $response, 'correct message returned' );
+}
+
+{
+  my $uri = URI->new( $track_list_url );
+  $uri->query_form({ 'search.cd.artist.name' => 'Random Boy Band' });
+  my $req = GET( $uri, {
+		
+  }, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'search on artist field okay due to wildcard' );
+  my @expected_response = map { { $_->get_columns } } $base_rs->search({ 'artist.name' => 'Random Boy Band' }, { join => { cd => 'artist' } })->all;
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply({ success => 'true',list => \@expected_response }, $response, 'correct message returned' );
+}




More information about the Catalyst-commits mailing list