Fix newByPropertyHashRef, which is required for class dispatch. Add tests. Fix addChild to use newByPropertyHashRef.
This commit is contained in:
parent
36d1636f06
commit
caa1f330b8
3 changed files with 19 additions and 9 deletions
|
|
@ -1740,8 +1740,10 @@ sub newById {
|
||||||
|
|
||||||
=head2 newByPropertyHashRef ( session, properties )
|
=head2 newByPropertyHashRef ( session, properties )
|
||||||
|
|
||||||
Constructor. This creates a standalone asset with no parent. It does not persist that object
|
Constructor. This is a class method. It creates a standalone asset with no parent, with a
|
||||||
to the database.
|
varying class, determined by the className entry in the properties hash ref.
|
||||||
|
|
||||||
|
The object created is not persisted to the database.
|
||||||
|
|
||||||
=head3 session
|
=head3 session
|
||||||
|
|
||||||
|
|
@ -1751,10 +1753,6 @@ A reference to the current session.
|
||||||
|
|
||||||
A hash reference of Asset properties.
|
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
|
=cut
|
||||||
|
|
||||||
sub newByPropertyHashRef {
|
sub newByPropertyHashRef {
|
||||||
|
|
@ -1762,9 +1760,10 @@ sub newByPropertyHashRef {
|
||||||
my $session = shift;
|
my $session = shift;
|
||||||
my $properties = shift || {};
|
my $properties = shift || {};
|
||||||
$properties->{className} //= $class;
|
$properties->{className} //= $class;
|
||||||
|
$properties->{session} = $session;
|
||||||
my $className = $class->loadModule($session, $properties->{className});
|
my $className = $class->loadModule($session, $properties->{className});
|
||||||
return undef unless (defined $className);
|
return undef unless (defined $className);
|
||||||
my $object = $className->new($session, $properties);
|
my $object = $className->new($properties);
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ sub addChild {
|
||||||
$session->db->commit;
|
$session->db->commit;
|
||||||
$properties->{assetId} = $id;
|
$properties->{assetId} = $id;
|
||||||
$properties->{parentId} = $self->getId;
|
$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);
|
my $newAsset = $temp->addRevision($properties, $now, $options);
|
||||||
$self->updateHistory("added child ".$id);
|
$self->updateHistory("added child ".$id);
|
||||||
$session->http->setStatus(201,"Asset Creation Successful");
|
$session->http->setStatus(201,"Asset Creation Successful");
|
||||||
|
|
|
||||||
13
t/Asset.t
13
t/Asset.t
|
|
@ -20,7 +20,7 @@ use Test::More;
|
||||||
use Test::Deep;
|
use Test::Deep;
|
||||||
use Test::Exception;
|
use Test::Exception;
|
||||||
|
|
||||||
plan tests => 46;
|
plan tests => 49;
|
||||||
|
|
||||||
my $session = WebGUI::Test->session;
|
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';
|
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";
|
note "new, fetching from db";
|
||||||
my $asset;
|
my $asset;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue