[Jifty-commit] r5979 - in JiftyX-ModelHelpers/trunk: lib/JiftyX t/Simapp/t

Jifty commits jifty-commit at lists.jifty.org
Tue Nov 4 03:03:42 EST 2008


Author: gugod
Date: Tue Nov  4 03:03:41 2008
New Revision: 5979

Modified:
   JiftyX-ModelHelpers/trunk/   (props changed)
   JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm
   JiftyX-ModelHelpers/trunk/t/Simapp/t/m-helper.t

Log:
 r24715 at yra:  gugod | 2008-11-04 11:07:52 +0800
 Allow the M() method to treat last element in its param to be the
 parameter hash that passed to the constructor of model class, but only
 when that element is a hashref.
 
 r24716 at yra:  gugod | 2008-11-04 11:14:37 +0800
 document about how to pass params to model constructors.
 


Modified: JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm
==============================================================================
--- JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm	(original)
+++ JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm	Tue Nov  4 03:03:41 2008
@@ -6,10 +6,20 @@
 
 our @ISA = qw(Exporter);
 our @EXPORT = qw(M);
+use YAML;
 
 sub M {
     my ($model, @params) = @_;;
-    my $record = Jifty->app_class(Model => $model)->new;
+
+    return Jifty->app_class(Model => $model)->new() unless @params;
+
+    my $params_to_new = pop @params;
+    unless (ref($params_to_new) eq 'HASH') {
+        push @params, $params_to_new;
+        $params_to_new = {};
+    }
+
+    my $record = Jifty->app_class(Model => $model)->new(%$params_to_new);
     if (@params) {
         if (index($model, "Collection") > 0) {
             my %params = (@params);
@@ -160,6 +170,15 @@
 Practially this is the mostly used scenario, that's why I this
 decision to lett it represent a collection of "all" instead of "none".
 
+If you need to set "current_user" to different ones when you construct
+a new model object, you can do it like this:
+
+    my $u = Jifty->app_class('CurrentUser')->superuser;
+    $book = M("Book", isbn => "978-0099410676", { current_user => $u });
+
+If the last argument to the M() method is a hashref, it is then passed
+to the C<new> method of the model class.
+
 =head2 The auto-generated model functions.
 
 Optionally, C<JiftyX::ModelHelpers> generates two functions for each

Modified: JiftyX-ModelHelpers/trunk/t/Simapp/t/m-helper.t
==============================================================================
--- JiftyX-ModelHelpers/trunk/t/Simapp/t/m-helper.t	(original)
+++ JiftyX-ModelHelpers/trunk/t/Simapp/t/m-helper.t	Tue Nov  4 03:03:41 2008
@@ -2,11 +2,11 @@
 use warnings;
 use strict;
 
-use Jifty::Test::Dist tests => 6;
+use Jifty::Test::Dist tests => 10;
 use JiftyX::ModelHelpers qw(M);
+use Simapp::Model::Book;
 
 {
-    use Simapp::Model::Book;
     my $b = M("Book");
     is( ref($b), "Simapp::Model::Book" );
 }
@@ -36,3 +36,15 @@
     is( $b->count, 1 );
 }
 
+{
+    my $system_user = Simapp::CurrentUser->superuser;
+
+    my $b = M("Book", { current_user => $system_user });
+    my ($id) = $b->create(name => "Book Created by System User");
+
+    ok( $b->current_user->is_superuser );
+
+    ok($id, "Book create returned success");
+    ok($b->id, "New Book has valid id set");
+    is($b->id, $id, "Create returned the right id");
+}


More information about the Jifty-commit mailing list