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

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Wed Mar 18 12:02:27 GMT 2009


Author: lukes
Date: 2009-03-18 12:02:27 +0000 (Wed, 18 Mar 2009)
New Revision: 9514

Added:
   Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Controller/API/RPC/TrackSetupDBICArgs.pm
   Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/setup_dbic_args.t
Modified:
   Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm
   Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
Log:
added setup_dbic_args_method hook

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 11:10:19 UTC (rev 9513)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2009-03-18 12:02:27 UTC (rev 9514)
@@ -9,7 +9,7 @@
 use JSON::Any;
 
 __PACKAGE__->mk_accessors(qw(
-  class create_requires update_requires update_allows $self->rs_stash_key create_allows list_count list_returns list_grouped_by list_search_exposes list_ordered_by rs_stash_key object_stash_key setup_list_method
+  class create_requires update_requires update_allows $self->rs_stash_key create_allows list_count list_returns list_grouped_by list_search_exposes list_ordered_by rs_stash_key object_stash_key setup_list_method setup_dbic_args_method
 ));
 
 __PACKAGE__->config(
@@ -80,6 +80,14 @@
 		$args->{select} = [map { ($_ =~ m/\./) ? $_ : "me.$_" } (ref $args->{select}) ? @{$args->{select}} : $args->{select}];
 	}
   $args->{join} = $join;
+  if ( my $a = $self->setup_dbic_args_method ) {
+    my $format_action = $self->action_for($a);
+    if ( defined $format_action ) {
+      ($params, $args) = @{$c->forward("/$format_action", [ $params, $args ])};
+    } else {
+      $c->log->error("setup_dbic_args_method was configured, but action $a not found");
+    }
+  }
 
   return [$params, $args];
 }

Modified: Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm	2009-03-18 11:10:19 UTC (rev 9513)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm	2009-03-18 12:02:27 UTC (rev 9514)
@@ -119,6 +119,26 @@
 
 List level methods such as list and create stash the class resultset in the stash. Specify the stash key you would like to use here. Defaults to 'class_rs'.
 
+=head2 setup_dbic_args_method
+
+This hook will allow you to alter the parameters before they are passed to $rs->search. 
+Here you can add additional attributes or alter the generated query. 
+
+Note that the method is passed ($self, $c, $params, $attrs) and must return [$params, $attrs]. Below is an example of basic usage:
+
+  __PACKAGE__->config(
+      ...,
+      setup_dbic_args_method => 'setup_dbic_args'
+  );
+
+  sub setup_dbic_args : Private {
+	  my ($self, $c, $params, $args) = @_;
+
+    # we only ever want to show items with position greater than 1
+	  $params->{position} = { '!=' => '1' };
+	  return [$params, $args];
+  }
+
 =head2 setup_list_method
 
 If you need to process the incoming parameters (for validation, access control,

Added: Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Controller/API/RPC/TrackSetupDBICArgs.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Controller/API/RPC/TrackSetupDBICArgs.pm	                        (rev 0)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/t/lib/RestTest/Controller/API/RPC/TrackSetupDBICArgs.pm	2009-03-18 12:02:27 UTC (rev 9514)
@@ -0,0 +1,23 @@
+package RestTest::Controller::API::RPC::TrackSetupDBICArgs;
+
+use strict;
+use warnings;
+use base qw/Catalyst::Controller::DBIC::API::RPC/;
+use JSON::Syck;
+
+__PACKAGE__->config
+    ( action => { setup => { PathPart => 'track_setup_dbic_args', Chained => '/api/rpc/rpc_base' } },
+      class => 'RestTestDB::Track',
+      list_returns => [qw/position title/],
+      list_ordered_by => [qw/position/],
+			setup_dbic_args_method => 'setup_dbic_args'
+      );
+
+sub setup_dbic_args : Private {
+	my ($self, $c, $params, $args) = @_;
+
+	$params->{position} = { '!=' => '1' };
+	return [$params, $args];
+}
+
+1;

Added: Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/setup_dbic_args.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/setup_dbic_args.t	                        (rev 0)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/setup_dbic_args.t	2009-03-18 12:02:27 UTC (rev 9514)
@@ -0,0 +1,35 @@
+use 5.6.0;
+
+use strict;
+use warnings;
+
+use lib 't/lib';
+
+my $base = 'http://localhost';
+
+use RestTest;
+use DBICTest;
+use URI;
+use Test::More qw(no_plan);
+use Test::WWW::Mechanize::Catalyst 'RestTest';
+use HTTP::Request::Common;
+use JSON::Syck;
+
+my $mech = Test::WWW::Mechanize::Catalyst->new;
+ok(my $schema = DBICTest->init_schema(), 'got schema');
+
+my $track_list_url = "$base/api/rpc/track_setup_dbic_args/list";
+my $base_rs = $schema->resultset('Track')->search({}, { select => [qw/me.title me.position/], order_by => 'position' });
+
+# test open request
+{
+  my $req = GET( $track_list_url, {
+		
+  }, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
+
+  my @expected_response = map { { $_->get_columns } } $base_rs->search({ position => { '!=' => '1' } })->all;
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct message returned' );
+}




More information about the Catalyst-commits mailing list