[Jifty-commit] jifty branch, master, updated. 47b987a6ee0991b22f16e696780341aada671cb7

Jifty commits jifty-commit at lists.jifty.org
Thu Apr 29 08:54:57 EDT 2010


The branch, master has been updated
       via  47b987a6ee0991b22f16e696780341aada671cb7 (commit)
       via  5438fa46d86009df0d64cba79a289e65d24334d5 (commit)
      from  783d37f056b6b6fbf84f90cc0b844c89fb5397f8 (commit)

Summary of changes:
 lib/Jifty/Dispatcher.pm                            |    2 +-
 lib/Jifty/Plugin/REST/Dispatcher.pm                |   82 ++++++++++++--------
 lib/Jifty/Record.pm                                |   25 ++++++-
 .../lib/TestApp/Plugin/REST/Model/User.pm          |    2 +-
 t/TestApp-Plugin-REST/t/02-basic-use.t             |    6 +-
 t/TestApp-Plugin-REST/t/03-format.t                |    2 +-
 6 files changed, 81 insertions(+), 38 deletions(-)

- Log -----------------------------------------------------------------
commit 5438fa46d86009df0d64cba79a289e65d24334d5
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Apr 29 19:57:45 2010 +0800

    Use streamy interface for REST action.

diff --git a/lib/Jifty/Dispatcher.pm b/lib/Jifty/Dispatcher.pm
index dcce8f5..a0d1867 100644
--- a/lib/Jifty/Dispatcher.pm
+++ b/lib/Jifty/Dispatcher.pm
@@ -762,7 +762,7 @@ Take a coderef that returns a PSGI streamy response code.
 
 sub _do_stream {
     my ( $self, $code ) = @_;
-    $self->{stream} = $code->();
+    $self->{stream} = $code->($self);
     $self->_abort;
 }
 
diff --git a/lib/Jifty/Plugin/REST/Dispatcher.pm b/lib/Jifty/Plugin/REST/Dispatcher.pm
index 68c32be..8a495ca 100644
--- a/lib/Jifty/Plugin/REST/Dispatcher.pm
+++ b/lib/Jifty/Plugin/REST/Dispatcher.pm
@@ -39,7 +39,7 @@ on GET    '/=/search/*/**'      => \&search_items;
 
 on GET    '/=/action/*'         => \&list_action_params;
 on GET    '/=/action'           => \&list_actions;
-on POST   '/=/action/*'         => \&run_action;
+on POST   '/=/action/*'         => stream \&run_action_stream;
 
 on GET    '/='                  => \&show_help;
 on GET    '/=/help'             => \&show_help;
@@ -207,7 +207,12 @@ Returns the user's desired output format. Returns a hashref of:
 
 sub output_format {
     my $prefix = shift;
-    my $accept = (Jifty->web->request->env->{HTTP_ACCEPT} || '');
+    output_format2(Jifty->web->request->env, $prefix);
+}
+
+sub output_format2 {
+    my ($env, $prefix) = @_;
+    my $accept = ($env->{HTTP_ACCEPT} || '');
 
     my (@prefix, $url);
     if ($prefix) {
@@ -237,7 +242,7 @@ sub output_format {
             extension    => 'js',
             content_type => 'application/javascript; charset=UTF-8',
             freezer      => sub { 'var $_ = ' . Jifty::JSON::encode_json( @_ ) },
-        };
+        };use Data::Dumper;
     }
     elsif ($accept =~ qr{^(?:application/x-)?(?:perl|pl)$}i) {
         return {
@@ -331,7 +336,7 @@ sub render_as_html {
     my $prefix = shift;
     my $url = shift;
     my $content = shift;
-
+#    warn 'rendering to html: '.Dumper($content) ;use Data::Dumper;
     my $title = _("%1 - REST API", Jifty->config->framework('ApplicationName'));
 
     if (ref($content) eq 'ARRAY') {
@@ -922,7 +927,7 @@ sub show_action_form {
     last_rule;
 }
 
-=head2 run_action 
+=head2 run_action_stream
 
 Expects $1 to be the name of an action we want to run.
 
@@ -940,16 +945,18 @@ On an internal error, throws a C<500>.
 
 =cut
 
-sub run_action {
-    my ($action_name) = action($1);
-    Jifty::Util->require($action_name) or abort(404);
-    
+
+sub run_action_stream {
+    my $dispatcher = shift;
+    my $action_name = action($1);
+    Jifty::Util->require($action_name) or $dispatcher->_do_abort(404);
+
     my $args = Jifty->web->request->arguments;
     delete $args->{''};
 
-    my $action = $action_name->new( arguments => $args ) or abort(404);
+    my $action = $action_name->new( arguments => $args ) or $dispatcher->_do_abort(404);
 
-    Jifty->api->is_allowed( $action_name ) or abort(403);
+    Jifty->api->is_allowed( $action_name ) or $dispatcher->_do_abort(403);
 
     $action->validate;
 
@@ -958,27 +965,38 @@ sub run_action {
 
     if ($@) {
         warn $@;
-        abort(500);
+        $dispatcher->_do_abort(500);
     }
 
+    my $env = Jifty->web->request->env;
     my $rec = $action->{record};
-    if ($action->result->success && $rec and $rec->isa('Jifty::Record') and $rec->id) {
-        my @fragments = ('model', ref($rec), 'id', $rec->id);
-
-        my $path = join '/', '=', map { Jifty::Web->escape_uri($_) } @fragments;
-
-        my $extension = output_format(\@fragments)->{extension};
-        $path .= '.' . $extension;
-
-        my $url = Jifty->web->url(path => $path);
-
-        Jifty->web->response->status( 302 );
-        Jifty->web->response->header('Location' => $url);
-    }
-
-    outs(undef, $action->result->as_hash);
-
-    last_rule;
-}
+    return sub {
+        my $responder = shift;
+        my $writer;
+        my $code = 200;
+        my @headers;
+        if ($action->result->success && $rec and $rec->isa('Jifty::Record') and $rec->id) {
+            my @fragments = ('model', ref($rec), 'id', $rec->id);
+            my $path = join '/', '=', map { Jifty::Web->escape_uri($_) } @fragments;
+            my $extension = output_format2($env, \@fragments)->{extension};
+            $path .= '.' . $extension;
+            my $url = Jifty->web->url(path => $path);
+            push @headers, 'Location' => $url;
+            $code = 302;
+        }
 
-1;
+        my $format = output_format2($env, undef);
+        warn "==> using $format->{format}" if $main::DEBUG;
+
+        $writer = $responder->([$code,
+                                [@headers,
+                                 'Content-Type' => $format->{content_type}]]);
+        # XXX: use writer for streamy freezer.
+        my $res = $action->result->as_hash;
+        for ($format->{freezer}->($res)) {
+            Encode::_utf8_off($_);
+            $writer->write($_);
+        }
+        $writer->close;
+    };
+};
diff --git a/t/TestApp-Plugin-REST/t/02-basic-use.t b/t/TestApp-Plugin-REST/t/02-basic-use.t
index 3a0edb3..3888362 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 => 87;
+use Jifty::Test::Dist tests => 88, actual_server => 1;
 use Jifty::Test::WWW::Mechanize;
 
 my $server  = Jifty::Test->make_server;
@@ -159,6 +159,8 @@ $mech->get_ok('/=/action/DoSomething');
 is($mech->status, 200);
 $mech->get_ok('/=/action/TestApp::Plugin::REST::Action::DoSomething');
 is($mech->status, 200);
+$mech->get('/=/action/TestApp.Plugin.REST.Action.DoSomethingBad');
+is($mech->status, 404);
 $mech->get_ok('/=/action/TestApp.Plugin.REST.Action.DoSomething');
 is($mech->status, 200);
 
diff --git a/t/TestApp-Plugin-REST/t/03-format.t b/t/TestApp-Plugin-REST/t/03-format.t
index 65f2236..97f56e7 100644
--- a/t/TestApp-Plugin-REST/t/03-format.t
+++ b/t/TestApp-Plugin-REST/t/03-format.t
@@ -2,7 +2,7 @@
 use warnings;
 use strict;
 
-use Jifty::Test::Dist tests => 103;
+use Jifty::Test::Dist tests => 103, actual_server => 1;
 use Jifty::Test::WWW::Mechanize;
 
 my $server  = Jifty::Test->make_server;

commit 47b987a6ee0991b22f16e696780341aada671cb7
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Apr 29 20:54:28 2010 +0800

    Allow models to provide default columns for serialization.

diff --git a/lib/Jifty/Plugin/REST/Dispatcher.pm b/lib/Jifty/Plugin/REST/Dispatcher.pm
index 8a495ca..6223693 100644
--- a/lib/Jifty/Plugin/REST/Dispatcher.pm
+++ b/lib/Jifty/Plugin/REST/Dispatcher.pm
@@ -551,7 +551,7 @@ sub list_model_columns {
             $cols{ $col->name }->{ $_ } = Scalar::Defer::force($val)
                 if defined $val and length $val;
         }
-        if (my $serialized = $col->attributes->{serialized}) {
+        if (my $serialized = $model->column_serialized_as($col)) {
             $cols{ $col->name }->{serialized_as} = $serialized;
         }
         $cols{ $col->name }{writable} = 0 if exists $cols{$col->name}{writable} and $col->protected;
diff --git a/lib/Jifty/Record.pm b/lib/Jifty/Record.pm
index b09f5b1..9fb6668 100644
--- a/lib/Jifty/Record.pm
+++ b/lib/Jifty/Record.pm
@@ -847,6 +847,29 @@ sub schema_version {
     }
 }
 
+=head2 column_serialized_as
+
+
+
+=cut
+
+sub column_serialized_as {
+    my ($class, $column) = @_;
+    my $meta = $column->attributes->{serialized} or return;
+    $meta->{columns} ||= [$column->refers_to->default_serialized_as_columns]
+        if $column->refers_to;
+    return $meta;
+}
+
+=head2 default_serialized_as_columns
+
+=cut
+
+sub default_serialized_as_columns {
+    my $class = shift;
+    return ('id', $class->_brief_description);
+}
+
 =head2 jifty_serialize_format
 
 This is used to create a hash reference of the object's values. Unlike
@@ -868,7 +891,7 @@ sub jifty_serialize_format {
         my $name = $column->aliased_as || $column->name;
 
         if ((my $refers_to      = $column->refers_to) &&
-            (my $serialize_meta = $column->attributes->{serialized})) {
+            (my $serialize_meta = $record->column_serialized_as($column))) {
             my $column_data = $record->$name();
             if ( $column_data && $column_data->id ) {
                 $name = $serialize_meta->{name} if $serialize_meta->{name};
diff --git a/t/TestApp-Plugin-REST/lib/TestApp/Plugin/REST/Model/User.pm b/t/TestApp-Plugin-REST/lib/TestApp/Plugin/REST/Model/User.pm
index 4eb9bee..ce7eb18 100644
--- a/t/TestApp-Plugin-REST/lib/TestApp/Plugin/REST/Model/User.pm
+++ b/t/TestApp-Plugin-REST/lib/TestApp/Plugin/REST/Model/User.pm
@@ -19,7 +19,7 @@ column 'tasty' =>
 
 column group_id => refers_to TestApp::Plugin::REST::Model::Group,
   label is 'Group',
-  serialized as { name => 'group', columns => [qw(id name)] } ;
+  serialized as { name => 'group' } ;
 
 };
 
diff --git a/t/TestApp-Plugin-REST/t/02-basic-use.t b/t/TestApp-Plugin-REST/t/02-basic-use.t
index 3888362..efa7acc 100644
--- a/t/TestApp-Plugin-REST/t/02-basic-use.t
+++ b/t/TestApp-Plugin-REST/t/02-basic-use.t
@@ -62,7 +62,7 @@ my %keys =  %{get_content()};
 
 is((0+keys(%keys)), 5, "The model has 5 keys");
 is_deeply([sort keys %keys], [sort qw/group_id id name email tasty/]);
-is_deeply($keys{'group_id'}{serialized_as}, { name => 'group', columns => [qw(id name)] });
+is_deeply($keys{'group_id'}{serialized_as}, { name => 'group' });
 
 # on GET    '/=/model/*/*'   => \&list_model_items;
 $mech->get_ok('/=/model/user/id.yml');

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


More information about the Jifty-commit mailing list