[Jifty-commit] jifty branch, master, updated. 1.10518-100-g5d1407b

Jifty commits jifty-commit at lists.jifty.org
Thu Aug 16 01:40:11 EDT 2012


The branch, master has been updated
       via  5d1407bd8e0b566a37947c39772ee01148ce0fdc (commit)
       via  f8e0284a57c1ae45532af936ac5f74dac39139ff (commit)
      from  3b3580f926751d5350ab877f29109cc766518d44 (commit)

Summary of changes:
 lib/Jifty/Plugin/REST/Dispatcher.pm    | 34 ++++++++++++++++++++++++++++++++++
 lib/Jifty/Request.pm                   |  2 +-
 t/TestApp-Plugin-REST/t/02-basic-use.t | 16 +++++++++++-----
 3 files changed, 46 insertions(+), 6 deletions(-)

- Log -----------------------------------------------------------------
commit f8e0284a57c1ae45532af936ac5f74dac39139ff
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 15 22:03:28 2012 -0700

    It turns out that clients that don't post a content type cause a warning :/

diff --git a/lib/Jifty/Request.pm b/lib/Jifty/Request.pm
index 0008214..a7dbd7d 100644
--- a/lib/Jifty/Request.pm
+++ b/lib/Jifty/Request.pm
@@ -260,7 +260,7 @@ sub promote {
                             template_arguments => {} );
 
     # Grab content type and posted data, if any
-    my $ct   = $req->content_type;
+    my $ct   = $req->content_type || 'application/octet-stream';
     my $data = $req->content;
 
     # Check it for something appropriate

commit 5d1407bd8e0b566a37947c39772ee01148ce0fdc
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Aug 15 22:36:20 2012 -0700

    Add a PUT method to update a single column of a model to the REST API

diff --git a/lib/Jifty/Plugin/REST/Dispatcher.pm b/lib/Jifty/Plugin/REST/Dispatcher.pm
index ad222b6..880af1b 100644
--- a/lib/Jifty/Plugin/REST/Dispatcher.pm
+++ b/lib/Jifty/Plugin/REST/Dispatcher.pm
@@ -33,6 +33,7 @@ on GET    '/=/model'            => \&list_models;
 
 on POST   '/=/model/*'          => \&create_item;
 on PUT    '/=/model/*/*/*'      => \&replace_item;
+on PUT    '/=/model/*/*/*/*'    => \&replace_item_field;
 on DELETE '/=/model/*/*/*'      => \&delete_item;
 
 on GET    '/=/search/*/**'      => \&search_items;
@@ -810,6 +811,39 @@ Implemented by redispatching to a CreateModel or UpdateModel action.
 
 sub replace_item { _dispatch_to_action('Update') }
 
+=head2 replace_item_field $model, $column, $key, $field
+
+Loads up a model of type C<$model> which has a column C<$column> with a value C<$key>.
+Sets the value of the field based on the request payload.
+Returns 404 if it doesn't exist.
+
+=cut
+
+sub replace_item_field {
+    my ( $model, $column, $key, $field ) = ( model($1), $2, $3, $4 );
+    my $rec = $model->new;
+    $rec->load_by_cols( $column => $key );
+    $rec->id          or abort(404);
+    $rec->current_user_can(update => $field) or abort (403);
+
+    # Check that the field is actually a column (and not some other method)
+    abort(404) unless valid_column($model, $field);
+
+    my $buffer;
+    Jifty->web->request->body->read($buffer, Jifty->web->request->content_length);
+
+
+
+    my $method = "set_".$field;
+    my ($val,$msg) = $rec->$method($buffer);
+
+    if (!$val) {
+        Jifty->web->response->status( 500 );
+    }
+
+    outs($msg);
+}
+
 =head2 delete_item
 
 Implemented by redispatching to a DeleteModel action.
diff --git a/t/TestApp-Plugin-REST/t/02-basic-use.t b/t/TestApp-Plugin-REST/t/02-basic-use.t
index f29abfd..e4bf3b0 100644
--- a/t/TestApp-Plugin-REST/t/02-basic-use.t
+++ b/t/TestApp-Plugin-REST/t/02-basic-use.t
@@ -9,7 +9,7 @@ This is a template for your own tests. Copy it and modify it.
 
 =cut
 
-use Jifty::Test::Dist tests => 88, actual_server => 1;
+use Jifty::Test::Dist tests => 91, actual_server => 1;
 use Jifty::Test::WWW::Mechanize;
 
 my $server  = Jifty::Test->make_server;
@@ -80,6 +80,12 @@ $mech->get_ok('/=/model/user/id/1/email.yml');
 is(get_content(), 'test at example.com');
 
 # on PUT    '/=/model/*/*/*' => \&replace_item;
+
+# on PUT    '/=/model/*/*/*/*' => \&replace_item_field;
+$mech->put_ok('/=/model/user/id/1/email', {   content_type => 'application/x-www-form-urlencoded',content => 'test2 at other.example.com'});
+$mech->get_ok('/=/model/user/id/1/email.yml');
+is(get_content(), 'test2 at other.example.com');
+
 # on DELETE '/=/model/*/*/*' => \&delete_item;
 
 # on POST   '/=/model/*'     => \&create_item;
@@ -92,7 +98,7 @@ ok(!$response->is_success, "create via POST to model with disallowed create acti
 # on GET    '/=/search/*/**' => \&search_items;
 $mech->get_ok('/=/search/user/id/1.yml');
 my $content = get_content();
-is_deeply($content, [{ name => 'test', email => 'test at example.com', id => 1, tasty => undef, group_id => undef, group => undef }]);
+is_deeply($content, [{ name => 'test', email => 'test2 at other.example.com', id => 1, tasty => undef, group_id => undef, group => undef }]);
 
 $mech->get_ok('/=/search/user/__not/id/1.yml');
 $content = get_content();
@@ -100,7 +106,7 @@ is_deeply($content, [{ name => 'moose', email => 'moose at example.com', id => 2, t
 
 $mech->get_ok('/=/search/user/id/1/name/test.yml');
 $content = get_content();
-is_deeply($content, [{ name => 'test', email => 'test at example.com', id => 1, tasty => undef, group_id => undef, group => undef }]);
+is_deeply($content, [{ name => 'test', email => 'test2 at other.example.com', id => 1, tasty => undef, group_id => undef, group => undef }]);
 
 $u1->set_group_id($g1->id);
 is($u1->group_id, $g1->id);
@@ -108,12 +114,12 @@ is($u1->group->id, $g1->id);
 
 $mech->get_ok('/=/search/user/id/1/name/test.yml');
 $content = get_content();
-is_deeply($content, [{ name => 'test', email => 'test at example.com', id => 1, tasty => undef, group_id => $g1->id, group => { id => $g1->id, name => 'test group'} }]);
+is_deeply($content, [{ name => 'test', email => 'test2 at other.example.com', id => 1, tasty => undef, group_id => $g1->id, group => { id => $g1->id, name => 'test group'} }]);
 
 
 $mech->get_ok('/=/search/user/id/1/name/test/email.yml');
 $content = get_content();
-is_deeply($content, ['test at example.com']);
+is_deeply($content, ['test2 at other.example.com']);
 
 $mech->get('/=/search/Usery/id/1.yml');
 is($mech->status,'404');

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list