[Jifty-commit] jifty branch, master, updated. 85610121ff43bc954fe9af72dfbc09adfa286d60

Jifty commits jifty-commit at lists.jifty.org
Thu Apr 29 07:32:04 EDT 2010


The branch, master has been updated
       via  85610121ff43bc954fe9af72dfbc09adfa286d60 (commit)
      from  02e2acff10a9d21a9c11ada8b9cadd2cd415a366 (commit)

Summary of changes:
 lib/Jifty/Record.pm                                |   24 +++++++++++++---
 .../lib/TestApp/Plugin/REST/Model/User.pm          |    5 +++
 t/TestApp-Plugin-REST/t/02-basic-use.t             |   29 ++++++++++++++-----
 t/TestApp-Plugin-REST/t/03-format.t                |   19 +++++++++---
 4 files changed, 59 insertions(+), 18 deletions(-)

- Log -----------------------------------------------------------------
commit 85610121ff43bc954fe9af72dfbc09adfa286d60
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Apr 29 19:30:47 2010 +0800

    New "serialized as" keyword for columns, which makes jifty_serialize_format inflates the specificed columns of the referenced object.

diff --git a/lib/Jifty/Record.pm b/lib/Jifty/Record.pm
index e8af645..b09f5b1 100644
--- a/lib/Jifty/Record.pm
+++ b/lib/Jifty/Record.pm
@@ -861,12 +861,26 @@ sub jifty_serialize_format {
     my %data;
 
     # XXX: maybe just test ->virtual?
-    for ($record->readable_attributes) {
-        next if UNIVERSAL::isa($record->column($_)->refers_to,
+    for my $column (grep { $_->readable } $record->columns ) {
+        next if UNIVERSAL::isa($column->refers_to,
                                'Jifty::DBI::Collection');
-        next if $record->column($_)->container;
-
-        $data{$_} = Jifty::Util->stringify($record->_value($_));
+        next if $column->container;
+        my $name = $column->aliased_as || $column->name;
+
+        if ((my $refers_to      = $column->refers_to) &&
+            (my $serialize_meta = $column->attributes->{serialized})) {
+            my $column_data = $record->$name();
+            if ( $column_data && $column_data->id ) {
+                $name = $serialize_meta->{name} if $serialize_meta->{name};
+                $data{$name} = { map { $_ => scalar $record->$name->$_ } @{$serialize_meta->{columns} } };
+            }
+            else {
+                $data{$name} = undef;
+            }
+        }
+        else {
+            $data{$name} = Jifty::Util->stringify($record->_value($name));
+        }
     }
 
     return \%data;
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 77ecccf..4eb9bee 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
@@ -16,6 +16,11 @@ column 'email' =>
 column 'tasty' =>
   type is 'boolean',
   is immutable;
+
+column group_id => refers_to TestApp::Plugin::REST::Model::Group,
+  label is 'Group',
+  serialized as { name => 'group', columns => [qw(id name)] } ;
+
 };
 
 
diff --git a/t/TestApp-Plugin-REST/t/02-basic-use.t b/t/TestApp-Plugin-REST/t/02-basic-use.t
index 18cd573..93333d9 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 => 81;
+use Jifty::Test::Dist tests => 86;
 use Jifty::Test::WWW::Mechanize;
 
 my $server  = Jifty::Test->make_server;
@@ -26,6 +26,11 @@ my $u1 = TestApp::Plugin::REST::Model::User->new(
 $u1->create( name => 'test', email => 'test at example.com' );
 ok( $u1->id );
 
+my $g1 = TestApp::Plugin::REST::Model::Group->new(
+    current_user => TestApp::Plugin::REST::CurrentUser->superuser );
+$g1->create( name => 'test group' );
+ok( $g1->id );
+
 # on GET    '/=/model'       => \&list_models;
 
 $mech->get_ok("$URL/=/model.yml", "Got model list");
@@ -55,9 +60,8 @@ is($mech->status,'200');
 $mech->get_ok('/=/model/User.yml');
 my %keys =  %{get_content()};
 
-is((0+keys(%keys)), 4, "The model has 4 keys");
-is_deeply([sort keys %keys], [sort qw/id name email tasty/]);
-
+is((0+keys(%keys)), 5, "The model has 5 keys");
+is_deeply([sort keys %keys], [sort qw/group_id id name email tasty/]);
 
 # on GET    '/=/model/*/*'   => \&list_model_items;
 $mech->get_ok('/=/model/user/id.yml');
@@ -68,7 +72,7 @@ is($#rows,0);
 # on GET    '/=/model/*/*/*' => \&show_item;
 $mech->get_ok('/=/model/user/id/1.yml');
 my %content = %{get_content()};
-is_deeply(\%content, { name => 'test', email => 'test at example.com', id => 1, tasty => undef });
+is_deeply(\%content, { name => 'test', email => 'test at example.com', id => 1, tasty => undef, group_id => undef, group => undef });
 
 # on GET    '/=/model/*/*/*/*' => \&show_item_Field;
 $mech->get_ok('/=/model/user/id/1/email.yml');
@@ -87,15 +91,24 @@ 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 }]);
+is_deeply($content, [{ name => 'test', email => 'test at example.com', id => 1, tasty => undef, group_id => undef, group => undef }]);
 
 $mech->get_ok('/=/search/user/__not/id/1.yml');
 $content = get_content();
-is_deeply($content, [{ name => 'moose', email => 'moose at example.com', id => 2, tasty => undef }]);
+is_deeply($content, [{ name => 'moose', email => 'moose at example.com', id => 2, tasty => undef, group_id => undef, group => undef }]);
+
+$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 }]);
+
+$u1->set_group_id($g1->id);
+is($u1->group_id, $g1->id);
+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 }]);
+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'} }]);
+
 
 $mech->get_ok('/=/search/user/id/1/name/test/email.yml');
 $content = get_content();
diff --git a/t/TestApp-Plugin-REST/t/03-format.t b/t/TestApp-Plugin-REST/t/03-format.t
index 3c94761..65f2236 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 => 102;
+use Jifty::Test::Dist tests => 103;
 use Jifty::Test::WWW::Mechanize;
 
 my $server  = Jifty::Test->make_server;
@@ -14,10 +14,15 @@ $mech->requests_redirectable([]);
 
 ok(1, 'Loaded the test script');
 
+my $g1 = TestApp::Plugin::REST::Model::Group->new(
+    current_user => TestApp::Plugin::REST::CurrentUser->superuser );
+$g1->create( name => 'test group' );
+ok( $g1->id );
+
 my $u1 = TestApp::Plugin::REST::Model::User->new(
     current_user => TestApp::Plugin::REST::CurrentUser->superuser,
 );
-$u1->create(name => 'test', email => 'test at example.com');
+$u1->create(name => 'test', email => 'test at example.com', group_id => $g1->id);
 ok($u1->id);
 
 our $FORMAT_NUMBER;
@@ -98,7 +103,7 @@ result_of '/=/model' => sub {
 };
 
 result_of '/=/model/User' => sub {
-    is(scalar keys %{ $_[0] }, 4, 'four keys in the user record');
+    is(scalar keys %{ $_[0] }, 5, '5 keys in the user record');
 };
 
 result_of '/=/model/user/id' => sub {
@@ -111,6 +116,8 @@ result_of '/=/model/user/id/1' => sub {
         email => 'test at example.com',
         id    => 1,
         tasty => undef,
+        group_id => 1,
+        group => { name => 'test group', id => 1 },
     });
 };
 
@@ -142,6 +149,8 @@ result_of_post '/=/model/user' => {
         id    => $id,
         name  => 'moose',
         tasty => undef,
+        group_id => undef,
+        group => undef,
     });
 };
 
@@ -166,11 +175,11 @@ TODO: {
 # 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 }]);
+is_deeply($content, [{ name => 'test', email => 'test at example.com', id => 1, tasty => undef, group_id => 1, group => { name => 'test group', id => 1 } }]);
 
 $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 }]);
+is_deeply($content, [{ name => 'test', email => 'test at example.com', id => 1, tasty => undef, group_id => 1, group => { name => 'test group', id => 1  } }]);
 
 $mech->get_ok('/=/search/user/id/1');
 $content = get_content();

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


More information about the Jifty-commit mailing list