[Catalyst-commits] r10178 - in
Catalyst-Controller-DBIC-API/1.003/trunk:
lib/Catalyst/Controller/DBIC/API t/rest t/rpc
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Sat May 16 14:01:40 GMT 2009
Author: lukes
Date: 2009-05-16 14:01:40 +0000 (Sat, 16 May 2009)
New Revision: 10178
Modified:
Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm
Catalyst-Controller-DBIC-API/1.003/trunk/t/rest/create.t
Catalyst-Controller-DBIC-API/1.003/trunk/t/rpc/list_json_search.t
Log:
refactored to channel all request param parsing through common code
Modified: Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2009-05-16 13:15:08 UTC (rev 10177)
+++ Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2009-05-16 14:01:40 UTC (rev 10178)
@@ -46,21 +46,26 @@
sub deserialize :ActionClass('Deserialize') {
my ($self, $c) = @_;
- my $req_params = $self->expand_hash($c->req->params);
- foreach my $param (qw/search list_count list_ordered_by list_grouped_by list_prefetch/) {
- # these params can also be composed of JSON
- eval {
- my $deserialized = JSON::Any->from_json($req_params->{$param});
- $req_params->{$param} = $deserialized;
- };
+ my $req_params;
+ if ($c->req->data) {
+ $req_params = $c->req->data;
+ } else {
+ $req_params = $self->expand_hash($c->req->params);
+ foreach my $param (qw/search list_count list_ordered_by list_grouped_by list_prefetch/) {
+ # these params can also be composed of JSON
+ eval {
+ my $deserialized = JSON::Any->from_json($req_params->{$param});
+ $req_params->{$param} = $deserialized;
+ };
+ }
}
-# use Data::Dumper; warn Dumper($req_params);
$c->stash->{_dbic_api}->{req_params} = $req_params;
}
sub list :Private {
my ($self, $c) = @_;
+ return if $self->get_errors($c);
my $ret = $c->forward('generate_dbic_search_args');
return unless ($ret && ref $ret);
my ($params, $args) = @{$ret};
@@ -91,18 +96,6 @@
}
}
- # if expand_hash didn't do anything, try json
- unless (exists $req_params->{search} && ref $req_params->{search} eq 'HASH') {
- eval {
- $req_params->{search} = exists $c->req->params->{search} ? JSON::Any->from_json($c->req->params->{search}) : undef;
- };
- if ($@) {
- # json didn't work either. looks like it's screwed.
- $self->push_error($c, { message => "can not parse search arg" });
- return;
- }
- }
-
if ( my $a = $self->setup_list_method ) {
my $setup_action = $self->action_for($a);
if ( defined $setup_action ) {
@@ -114,7 +107,12 @@
my $source = $c->stash->{$self->rs_stash_key}->result_source;
my ($params, $join);
-
+
+ if ($req_params->{search} && !ref $req_params->{search}) {
+ $self->push_error($c, { message => "can not parse search arg" });
+ return;
+ }
+
($params, $join) = $self->_format_search($c, { params => $req_params->{search}, source => $source }) if ($req_params->{search});
$args->{group_by} = $req_params->{list_grouped_by} || ((scalar(@{$self->list_grouped_by})) ? $self->list_grouped_by : undef);
@@ -244,9 +242,8 @@
my ($self, $c) = @_;
# 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);
+ my $req_params = $c->stash->{_dbic_api}->{req_params};
- $c->req->params($req_params);
die "no object to update (looking at " . $self->object_stash_key . ")"
unless ( defined $c->stash->{$self->object_stash_key} );
@@ -257,7 +254,6 @@
die "class resultset not set";
}
- # use Data::Dumper; $c->log->debug(Dumper(\%create_args));
my $object = $c->stash->{$self->object_stash_key};
$self->validate_and_save_object($c, $object);
}
@@ -277,7 +273,7 @@
if $c->debug;
return;
}
-
+# use Data::Dumper; warn Dumper($params);
if ( $c->debug ) {
$c->log->debug("Saving object: $object");
$c->log->_dump( $params );
@@ -287,7 +283,7 @@
sub validate {
my ($self, $c, $object) = @_;
- my $params = $c->req->params;
+ my $params = $c->stash->{_dbic_api}->{req_params};
my %values;
my %requires_map = map { $_ => 1 } @{($object->in_storage) ? [] : $c->stash->{create_requires} || $self->create_requires};
@@ -302,9 +298,8 @@
$self->push_error($c, { message => "${key} is not a valid relation" });
next;
}
-
- my $related_params = $params->{$key};
+ my $related_params = $params->{$key};
# it's an error for $c->req->params->{$key} to be defined but not be an array
unless (ref $related_params) {
unless (!defined $related_params) {
Modified: Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm 2009-05-16 13:15:08 UTC (rev 10177)
+++ Catalyst-Controller-DBIC-API/1.003/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm 2009-05-16 14:01:40 UTC (rev 10178)
@@ -78,7 +78,6 @@
my ($self, $c) = @_;
$c->forward('deserialize');
- $c->req->params($c->req->data);
}
sub object :Chained('setup') :Args(1) :PathPart('') :ActionClass('REST') {
Modified: Catalyst-Controller-DBIC-API/1.003/trunk/t/rest/create.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.003/trunk/t/rest/create.t 2009-05-16 13:15:08 UTC (rev 10177)
+++ Catalyst-Controller-DBIC-API/1.003/trunk/t/rest/create.t 2009-05-16 14:01:40 UTC (rev 10178)
@@ -61,9 +61,8 @@
);
$req->content( $test_data );
$mech->request($req);
-
cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
- my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' });
+ my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' });
ok($new_obj, 'record created with specified name');
my $response = JSON::Syck::Load( $mech->content);
Modified: Catalyst-Controller-DBIC-API/1.003/trunk/t/rpc/list_json_search.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.003/trunk/t/rpc/list_json_search.t 2009-05-16 13:15:08 UTC (rev 10177)
+++ Catalyst-Controller-DBIC-API/1.003/trunk/t/rpc/list_json_search.t 2009-05-16 14:01:40 UTC (rev 10178)
@@ -32,7 +32,6 @@
is_deeply( { messages => ['can not parse search arg'], success => 'false' }, $response, 'correct data returned for gibberish in search' );
}
-
{
my $uri = URI->new( $artist_list_url );
$uri->query_form({ 'search' => '{"name":{"LIKE":"%waul%"}}' });
More information about the Catalyst-commits
mailing list