Implement keywords differently (successfully) in the Asset class. Extra tests to verify it in Asset.t
This commit is contained in:
parent
4eca8bb993
commit
8c759ed7bc
5 changed files with 44 additions and 18 deletions
|
|
@ -307,25 +307,16 @@ sub _build_className {
|
||||||
}
|
}
|
||||||
has keywords => (
|
has keywords => (
|
||||||
is => 'rw',
|
is => 'rw',
|
||||||
init_arg => undef,
|
|
||||||
builder => '_build_assetKeywords',
|
builder => '_build_assetKeywords',
|
||||||
lazy => 1,
|
lazy => 1,
|
||||||
|
traits => [ 'WebGUI::Definition::Meta::Settable' ],
|
||||||
);
|
);
|
||||||
sub _build_assetKeywords {
|
sub _build_assetKeywords {
|
||||||
my $session = shift->session;
|
|
||||||
return WebGUI::Keyword->new($session);
|
|
||||||
}
|
|
||||||
|
|
||||||
around keywords => sub {
|
|
||||||
my $orig = shift;
|
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
if (@_) {
|
my $session = $self->session;
|
||||||
return $self->$orig->setKeywordsForAsset({asset => $self, keywords => $_[0], });
|
my $keywords = WebGUI::Keyword->new($session);
|
||||||
}
|
return $keywords->getKeywordsForAsset({asset => $self, asArrayRef => 1 });
|
||||||
else {
|
}
|
||||||
return $self->$orig->getKeywordsForAsset({asset => $self});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
around BUILDARGS => sub {
|
around BUILDARGS => sub {
|
||||||
my $orig = shift;
|
my $orig = shift;
|
||||||
|
|
@ -374,7 +365,7 @@ around BUILDARGS => sub {
|
||||||
if (defined $properties) {
|
if (defined $properties) {
|
||||||
$properties->{session} = $session;
|
$properties->{session} = $session;
|
||||||
return $className->$orig($properties);
|
return $className->$orig($properties);
|
||||||
}
|
}
|
||||||
$session->errorHandler->error("Something went wrong trying to instanciate a '$className' with assetId '$assetId', but I don't know what!");
|
$session->errorHandler->error("Something went wrong trying to instanciate a '$className' with assetId '$assetId', but I don't know what!");
|
||||||
return undef;
|
return undef;
|
||||||
};
|
};
|
||||||
|
|
@ -2481,6 +2472,7 @@ sub write {
|
||||||
|
|
||||||
# update the asset's size, which also purges the cache.
|
# update the asset's size, which also purges the cache.
|
||||||
$self->setSize();
|
$self->setSize();
|
||||||
|
WebGUI::Keyword->new($self->session)->setKeywordsForAsset({ asset => $self, keywords => $self->keywords });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -148,6 +148,21 @@ sub get_all_property_list {
|
||||||
return @names;
|
return @names;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub get_all_settable_list {
|
||||||
|
my $self = shift;
|
||||||
|
my @names = ();
|
||||||
|
my %seen = ();
|
||||||
|
foreach my $meta ($self->get_all_class_metas) {
|
||||||
|
push @names,
|
||||||
|
grep { !$seen{$_}++ }
|
||||||
|
map { $_->name }
|
||||||
|
sort { $a->insertion_order <=> $b->insertion_order }
|
||||||
|
grep { $_->does('WebGUI::Definition::Meta::Settable') }
|
||||||
|
$meta->get_attributes;
|
||||||
|
}
|
||||||
|
return @names;
|
||||||
|
}
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 get_attributes ( )
|
=head2 get_attributes ( )
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ no warnings qw(uninitialized);
|
||||||
|
|
||||||
our $VERSION = '0.0.1';
|
our $VERSION = '0.0.1';
|
||||||
|
|
||||||
|
with 'WebGUI::Definition::Meta::Settable';
|
||||||
|
|
||||||
=head1 NAME
|
=head1 NAME
|
||||||
|
|
||||||
Package WebGUI::Definition::Meta::Property
|
Package WebGUI::Definition::Meta::Property
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ sub get {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
if (@_) {
|
if (@_) {
|
||||||
my $property = shift;
|
my $property = shift;
|
||||||
if ($self->meta->find_attribute_by_name($property)) {
|
if ($self->can($property)) {
|
||||||
return $self->$property;
|
return $self->$property;
|
||||||
}
|
}
|
||||||
return undef;
|
return undef;
|
||||||
|
|
@ -88,7 +88,7 @@ is not an attribute of the object, then it is silently ignored.
|
||||||
sub set {
|
sub set {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $properties = @_ % 2 ? shift : { @_ };
|
my $properties = @_ % 2 ? shift : { @_ };
|
||||||
my @orderedProperties = $self->getProperties;
|
my @orderedProperties = $self->meta->get_all_settable_list;
|
||||||
KEY: for my $property ( @orderedProperties ) {
|
KEY: for my $property ( @orderedProperties ) {
|
||||||
next KEY unless exists $properties->{$property};
|
next KEY unless exists $properties->{$property};
|
||||||
$self->$property($properties->{$property});
|
$self->$property($properties->{$property});
|
||||||
|
|
|
||||||
19
t/Asset.t
19
t/Asset.t
|
|
@ -20,6 +20,8 @@ use Test::More;
|
||||||
use Test::Deep;
|
use Test::Deep;
|
||||||
use Test::Exception;
|
use Test::Exception;
|
||||||
use WebGUI::Exception;
|
use WebGUI::Exception;
|
||||||
|
use WebGUI::Asset;
|
||||||
|
use WebGUI::Keyword;
|
||||||
|
|
||||||
|
|
||||||
my $session = WebGUI::Test->session;
|
my $session = WebGUI::Test->session;
|
||||||
|
|
@ -359,11 +361,26 @@ my $session = WebGUI::Test->session;
|
||||||
my $asset = $default->addChild({
|
my $asset = $default->addChild({
|
||||||
className => 'WebGUI::Asset::Snippet',
|
className => 'WebGUI::Asset::Snippet',
|
||||||
});
|
});
|
||||||
addToCleanup($asset);
|
WebGUI::Test->addToCleanup($asset);
|
||||||
can_ok($asset, 'keywords');
|
can_ok($asset, 'keywords');
|
||||||
$asset->keywords('chess set');
|
$asset->keywords('chess set');
|
||||||
is ($asset->keywords, 'chess set', 'set and get of keywords via direct accessor');
|
is ($asset->keywords, 'chess set', 'set and get of keywords via direct accessor');
|
||||||
is ($asset->get('keywords'), 'chess set', 'via get method');
|
is ($asset->get('keywords'), 'chess set', 'via get method');
|
||||||
|
my $keygate = WebGUI::Keyword->new($session);
|
||||||
|
is $keygate->getKeywordsForAsset({assetId => $asset->getId}), '', 'not persisted to the db';
|
||||||
|
$asset->write;
|
||||||
|
is $keygate->getKeywordsForAsset({assetId => $asset->assetId}), 'chess set', 'written to the db';
|
||||||
|
|
||||||
|
my $asset_copy = $asset->cloneFromDb;
|
||||||
|
is $asset->keywords, 'chess set', 'refreshed from db';
|
||||||
|
|
||||||
|
my $asset2 = $default->addChild({
|
||||||
|
className => 'WebGUI::Asset::Snippet',
|
||||||
|
keywords => 'checkmate',
|
||||||
|
});
|
||||||
|
WebGUI::Test->addToCleanup($asset2);
|
||||||
|
is $asset2->keywords, 'checkmate', 'keywords set on addChild';
|
||||||
|
is $keygate->getKeywordsForAsset({assetId => $asset2->assetId}), 'checkmate', '... and persisted to the db';
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue