improved pod for list_munge_parameters
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API.pm
index ea086b7..229237d 100644 (file)
@@ -2,7 +2,7 @@ package Catalyst::Controller::DBIC::API;
 
 #ABSTRACT: Provides a DBIx::Class web service automagically
 use Moose;
-BEGIN { extends 'Catalyst::Controller'; }
+BEGIN { extends 'Catalyst::Controller::ActionRole'; }
 
 use CGI::Expand ();
 use DBIx::Class::ResultClass::HashRefInflator;
@@ -374,6 +374,7 @@ sub list
 =method_protected list_munge_parameters
 
 list_munge_parameters is a noop by default. All arguments will be passed through without any manipulation. In order to successfully manipulate the parameters before the search is performed, simply access $c->req->search_parameters|search_attributes (ArrayRef and HashRef respectively), which correspond directly to ->search($parameters, $attributes). Parameter keys will be in already-aliased form.
+To store the munged parameters call $c->req->_set_search_parameters($newparams) and $c->req->_set_search_attributes($newattrs).
 
 =cut
 
@@ -556,7 +557,7 @@ sub validate_objects
     {
         my $err = $_;
         $c->log->error($err);
-        $err =~ s/\s+at\s+\/.+\n$//g;
+        $err =~ s/\s+at\s+.+\n$//g;
         $self->push_error($c, { message => $err });
         $c->detach();
     }
@@ -611,7 +612,7 @@ sub validate_object
 
             foreach my $related_col (@{$allowed_related_cols})
             {
-                if (my $related_col_value = $related_params->{$related_col}) {
+                if (defined(my $related_col_value = $related_params->{$related_col})) {
                     $values{$key}{$related_col} = $related_col_value;
                 }
             }
@@ -748,7 +749,13 @@ sub update_object_relation
 {
     my ($self, $c, $object, $related_params, $relation) = @_;
     my $row = $object->find_related($relation, {} , {});
-    $row->update($related_params);
+
+    if ($row) {
+        $row->update($related_params);
+    }
+    else {
+        $object->create_related($relation, $related_params);
+    }
 }
 
 =method_protected insert_object_from_params
@@ -760,8 +767,22 @@ insert_object_from_params sets the columns for the object, then calls ->insert
 sub insert_object_from_params
 {
     my ($self, $c, $object, $params) = @_;
-    $object->set_columns($params);
+
+    my %rels;
+    while (my ($k, $v) = each %{ $params }) {
+        if (ref $v && !($v == JSON::Any::true || $v == JSON::Any::false)) {
+            $rels{$k} = $v;
+        }
+        else {
+            $object->set_column($k => $v);
+        }
+    }
+
     $object->insert;
+
+    while (my ($k, $v) = each %rels) {
+        $object->create_related($k, $v);
+    }
 }
 
 =method_protected delete_objects