[Catalyst-commits] r8153 - in
Catalyst-Controller-DBIC-API/1.000/trunk: .
lib/Catalyst/Controller/DBIC lib/Catalyst/Controller/DBIC/API t/rest
jshirley at dev.catalyst.perl.org
jshirley at dev.catalyst.perl.org
Wed Jul 23 18:42:08 BST 2008
Author: jshirley
Date: 2008-07-23 18:42:08 +0100 (Wed, 23 Jul 2008)
New Revision: 8153
Modified:
Catalyst-Controller-DBIC-API/1.000/trunk/Changes
Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API.pm
Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t
Log:
Adding setup_list_method hook to filter incoming search parameters, added changelog entry as well
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/Changes
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/Changes 2008-07-23 11:39:01 UTC (rev 8152)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/Changes 2008-07-23 17:42:08 UTC (rev 8153)
@@ -1,5 +1,8 @@
Revision history for Catalyst-Controller-DBIC-API
+1.000003
+ Added setup_list_method configuration flag (jshirley)
+
1.000002
- Fixed lack of deserialization under RPC
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-23 11:39:01 UTC (rev 8152)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2008-07-23 17:42:08 UTC (rev 8153)
@@ -6,7 +6,7 @@
use base qw/Catalyst::Controller CGI::Expand/;
__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_ordered_by rs_stash_key object_stash_key
+ class create_requires update_requires update_allows $self->rs_stash_key create_allows list_count list_returns list_grouped_by list_ordered_by rs_stash_key object_stash_key setup_list_method
));
__PACKAGE__->config(
@@ -44,12 +44,20 @@
my ($self, $c) = @_;
my $req_params = (grep { ref $_ } values %{$c->req->params}) ? $c->req->params : $self->expand_hash($c->req->params);
-# use Data::Dumper; warn Dumper($req_params);
+
+ if ( my $a = $self->setup_list_method ) {
+ my $setup_action = $self->action_for($a);
+ if ( defined $setup_action ) {
+ $c->forward("/$setup_action", [ $req_params ]);
+ } else {
+ $c->log->error("setup_list_method was configured, but action $a not found");
+ }
+ }
+
my $source = $c->stash->{$self->rs_stash_key}->result_source;
my ($params, $join);
($params, $join) = $self->_format_search({ params => $req_params->{search}, source => $source }) if ($req_params->{search});
-
my $args = {};
$args->{group_by} = $req_params->{list_grouped_by} || ((scalar(@{$self->list_grouped_by})) ? $self->list_grouped_by : undef);
$args->{order_by} = $req_params->{list_ordered_by} || ((scalar(@{$self->list_ordered_by})) ? $self->list_ordered_by : undef);
@@ -133,9 +141,15 @@
my $params;
unless ($params = $self->validate($c, $object)) {
+ $c->log->debug("No value from validate, cowardly bailing out")
+ if $c->debug;
return;
}
+ if ( $c->debug ) {
+ $c->log->debug("Saving object: $object");
+ $c->log->_dump( $params );
+ }
return $self->save_object($c, $object, $params);
}
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API.pm 2008-07-23 11:39:01 UTC (rev 8152)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API.pm 2008-07-23 17:42:08 UTC (rev 8153)
@@ -90,6 +90,24 @@
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_list_method
+
+If you need to process the incoming parameters (for validation, access control,
+etc) you can configure an action to forward to. This is called before the
+search is handed off to DBIC, so you can process the incoming request
+parameters, or add your own filters. Below is an example of basic usage:
+
+ __PACKAGE__->config(
+ ...,
+ setup_list_method => 'filter_search_params'
+ );
+
+ sub filter_search_params : Private {
+ my ( $self, $c, $query ) = @_;
+ $query->{search}->{'user_id'} = $c->user->id;
+ }
+
+
=head1 METHODS
Note: see the individual interface classes - L<Catalyst::Controller::DBIC::API::RPC> and L<Catalyst::Controller::DBIC::API::REST> - for details of the endpoints to these abstract methods.
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t 2008-07-23 11:39:01 UTC (rev 8152)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t 2008-07-23 17:42:08 UTC (rev 8153)
@@ -10,7 +10,7 @@
use RestTest;
use DBICTest;
use URI;
-use Test::More tests => 13;
+use Test::More tests => 15;
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_list_url = "$base/api/rest/artist";
+my $filtered_artist_list_url = "$base/api/rest/bound_artist";
my $producer_list_url = "$base/api/rest/producer";
# test open request
@@ -93,3 +94,14 @@
# use Data::Dumper; warn Dumper($response, \@expected_response);
is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for class with list_returns specified' );
}
+
+{
+ my $uri = URI->new( $filtered_artist_list_url );
+ $uri->query_form({ 'search.artistid' => '2' });
+ my $req = GET( $uri, 'Accept' => 'text/x-json' );
+ $mech->request($req);
+ cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+ my $response = JSON::Syck::Load( $mech->content);
+ my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'artistid' => '1' })->all;
+ is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for class with setup_list_method specified' );
+}
More information about the Catalyst-commits
mailing list