Fix newByPropertyHashRef, which is required for class dispatch. Add tests. Fix addChild to use newByPropertyHashRef.

This commit is contained in:
Colin Kuskie 2010-01-20 18:44:48 -08:00
parent 36d1636f06
commit caa1f330b8
3 changed files with 19 additions and 9 deletions

View file

@ -1740,8 +1740,10 @@ sub newById {
=head2 newByPropertyHashRef ( session, properties )
Constructor. This creates a standalone asset with no parent. It does not persist that object
to the database.
Constructor. This is a class method. It creates a standalone asset with no parent, with a
varying class, determined by the className entry in the properties hash ref.
The object created is not persisted to the database.
=head3 session
@ -1751,10 +1753,6 @@ A reference to the current session.
A hash reference of Asset properties.
=head4 className
If className is not passed, the class used to call this method will be used in its place.
=cut
sub newByPropertyHashRef {
@ -1762,9 +1760,10 @@ sub newByPropertyHashRef {
my $session = shift;
my $properties = shift || {};
$properties->{className} //= $class;
$properties->{session} = $session;
my $className = $class->loadModule($session, $properties->{className});
return undef unless (defined $className);
my $object = $className->new($session, $properties);
my $object = $className->new($properties);
return $object;
}

View file

@ -85,7 +85,7 @@ sub addChild {
$session->db->commit;
$properties->{assetId} = $id;
$properties->{parentId} = $self->getId;
my $temp = WebGUI::Asset->new($session,$properties) || croak "Couldn't create a new $properties->{className} asset!";
my $temp = WebGUI::Asset->newByPropertyHashRef($session, $properties) || croak "Couldn't create a new $properties->{className} asset!";
my $newAsset = $temp->addRevision($properties, $now, $options);
$self->updateHistory("added child ".$id);
$session->http->setStatus(201,"Asset Creation Successful");

View file

@ -20,7 +20,7 @@ use Test::More;
use Test::Deep;
use Test::Exception;
plan tests => 46;
plan tests => 49;
my $session = WebGUI::Test->session;
@ -141,6 +141,17 @@ my $session = WebGUI::Test->session;
is $class, undef, '... returns undef if the class cannot be found';
}
{
note "newByPropertyHashRef";
my $asset;
$asset = WebGUI::Asset->newByPropertyHashRef($session, {className => 'WebGUI::Asset::Snippet', title => 'The Shawshank Snippet'});
isa_ok $asset, 'WebGUI::Asset::Snippet';
is $asset->title, 'The Shawshank Snippet', 'title is assigned from the property hash';
my $a2 = WebGUI::Asset::Snippet->newByPropertyHashRef($session, {});
isa_ok $asset, 'WebGUI::Asset::Snippet';
}
{
note "new, fetching from db";
my $asset;