From ce3edcf743aa7816fa558e76db79c1f9cba70282 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 4 Jan 2010 15:34:24 -0800 Subject: [PATCH] Move get_tables from Meta/Class into Meta/Asset. s/getTables/meta->get_tables/; --- lib/WebGUI/Asset.pm | 8 ++++---- lib/WebGUI/AssetLineage.pm | 2 +- lib/WebGUI/AssetTrash.pm | 2 +- lib/WebGUI/AssetVersioning.pm | 4 ++-- lib/WebGUI/Definition/Meta/Asset.pm | 21 +++++++++++++++++++- lib/WebGUI/Definition/Meta/Class.pm | 21 -------------------- t/Asset.t | 30 +++++++++++++++++++++++++++-- t/Definition/Asset.t | 24 +++++++++++++++++------ 8 files changed, 74 insertions(+), 38 deletions(-) diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index e9ff0f051..296866d01 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -21,9 +21,9 @@ use JSON; use HTML::Packer; use WebGUI::Definition::Asset; -attribute assetName => 'asset', -attribute tableName => 'assetData', -attribute icon => 'assets.gif', +attribute assetName => 'asset'; +attribute tableName => 'assetData'; +attribute icon => 'assets.gif'; property title => ( tab => "properties", label => ['99','Asset'], @@ -288,7 +288,7 @@ around BUILDARGS => sub { my $placeHolders = [$assetId]; # join all the tables - foreach my $table ($className->getTables) { + foreach my $table ($className->meta->get_tables) { $sql .= ",".$table; $where .= " and (asset.assetId=".$table.".assetId and ".$table.".revisionDate=".$revisionDate.")"; } diff --git a/lib/WebGUI/AssetLineage.pm b/lib/WebGUI/AssetLineage.pm index ec7b7708a..7bcf0908f 100644 --- a/lib/WebGUI/AssetLineage.pm +++ b/lib/WebGUI/AssetLineage.pm @@ -617,7 +617,7 @@ sub getLineageSql { if ( ! eval { require $module; 1 }) { $self->session->errorHandler->fatal("Couldn't compile asset package: ".$className.". Root cause: ".$@) if ($@); } - foreach my $table ($self->getTables) { + foreach my $table ($self->meta->get_tables) { unless ($table eq "asset" || $table eq "assetData") { $tables .= " left join $table on assetData.assetId=".$table.".assetId and assetData.revisionDate=".$table.".revisionDate"; } diff --git a/lib/WebGUI/AssetTrash.pm b/lib/WebGUI/AssetTrash.pm index f91ddda53..d9a1fdcd2 100644 --- a/lib/WebGUI/AssetTrash.pm +++ b/lib/WebGUI/AssetTrash.pm @@ -197,7 +197,7 @@ sub purge { $outputSub->($i18n->get('Clearing asset tables')); $session->db->beginTransaction; $session->db->write("delete from metaData_values where assetId = ?",[$self->getId]); - foreach my $table ($self->getTables) { + foreach my $table ($self->meta->get_tables) { $session->db->write("delete from ".$table." where assetId=?", [$self->getId]); } $session->db->write("delete from asset where assetId=?", [$self->getId]); diff --git a/lib/WebGUI/AssetVersioning.pm b/lib/WebGUI/AssetVersioning.pm index ff551f2f9..5e8f33cac 100644 --- a/lib/WebGUI/AssetVersioning.pm +++ b/lib/WebGUI/AssetVersioning.pm @@ -131,7 +131,7 @@ sub addRevision { } # prime the tables - foreach my $table ($self->getTables) { + foreach my $table ($self->meta->get_tables) { unless ($table eq "assetData") { $self->session->db->write( "insert into ".$table." (assetId,revisionDate) values (?,?)", [$self->getId, $now]); } @@ -355,7 +355,7 @@ sub purgeRevision { if ($self->getRevisionCount > 1) { my $db = $self->session->db; $db->beginTransaction; - foreach my $table ($self->getTables) { + foreach my $table ($self->meta->get_tables) { $db->write("delete from ".$table." where assetId=? and revisionDate=?",[$self->getId, $self->get("revisionDate")]); } my $count = $db->quickScalar("select count(*) from assetData where assetId=? and status='pending'",[$self->getId]); diff --git a/lib/WebGUI/Definition/Meta/Asset.pm b/lib/WebGUI/Definition/Meta/Asset.pm index 7dd845569..0453aa4c8 100644 --- a/lib/WebGUI/Definition/Meta/Asset.pm +++ b/lib/WebGUI/Definition/Meta/Asset.pm @@ -86,6 +86,25 @@ The second is the i18n namespace to find the asset's name. =cut +#------------------------------------------------------------------- + +=head2 get_tables ( ) + +Returns an array of the names of all tables in every class used by +this Class. + +=cut + +sub get_tables { + my $self = shift; + my @properties = (); + my %seen = (); + push @properties, + grep { ! $seen{$_}++ } + map { $_->tableName } + $self->get_all_properties + ; + return @properties; +} 1; - diff --git a/lib/WebGUI/Definition/Meta/Class.pm b/lib/WebGUI/Definition/Meta/Class.pm index 02ba9d7c2..f44616611 100644 --- a/lib/WebGUI/Definition/Meta/Class.pm +++ b/lib/WebGUI/Definition/Meta/Class.pm @@ -106,27 +106,6 @@ sub get_property_list { #------------------------------------------------------------------- -=head2 get_tables ( ) - -Returns an array of the names of all tables in every class used by -this Class. - -=cut - -sub get_tables { - my $self = shift; - my @properties = (); - my %seen = (); - push @properties, - grep { ! $seen{$_}++ } - map { $_->tableName } - $self->get_all_properties - ; - return @properties; -} - -#------------------------------------------------------------------- - =head2 property_meta ( ) Returns the name of the class for properties. diff --git a/t/Asset.t b/t/Asset.t index 3569e22df..5ac3ed2b3 100644 --- a/t/Asset.t +++ b/t/Asset.t @@ -20,13 +20,13 @@ use Test::More; use Test::Deep; use Test::Exception; -plan tests => 26; +plan tests => 29; my $session = WebGUI::Test->session; { - note "session and title"; + note "new, session and title"; my $asset = WebGUI::Asset->new({session => $session, }); isa_ok $asset, 'WebGUI::Asset'; @@ -102,6 +102,24 @@ my $session = WebGUI::Test->session; isa_ok $asset, 'WebGUI::Asset::Snippet'; } +{ + note "Property inspection"; + my $asset = WebGUI::Asset->new({ + session => $session, + }); + + cmp_deeply( + [$asset->meta->get_all_properties], + array_each( + methods( + tableName => 'assetData', + ) + ), + 'all properties have the right tableName' + ); + +} + { note "getClassById"; my $class; @@ -114,3 +132,11 @@ my $session = WebGUI::Test->session; $class = WebGUI::Asset->getClassById($session, 'noIdHereBoss'); is $class, undef, '... returns undef if the class cannot be found'; } + +{ + note "new, fetching from db"; + my $asset; + $asset = WebGUI::Asset->new($session, 'PBasset000000000000001'); + isa_ok $asset, 'WebGUI::Asset'; + is $asset->title, 'Root', 'got the right asset'; +} diff --git a/t/Definition/Asset.t b/t/Definition/Asset.t index 18d8b43fe..75b4f2d4b 100644 --- a/t/Definition/Asset.t +++ b/t/Definition/Asset.t @@ -128,6 +128,12 @@ use WebGUI::Test; my $object2 = __PACKAGE__->new(tableName => 'notAsset'); ::is $object2->tableName, 'asset', 'tableName ignored in constructor'; + + ::cmp_deeply( + [ __PACKAGE__->meta->get_tables ], + [qw/asset/], + 'get_tables works for a simple asset' + ); } { @@ -188,6 +194,18 @@ use WebGUI::Test; 'checking inheritance of properties by name, insertion order' ); + ::cmp_deeply( + [ WGT::Class::AlsoAsset->meta->get_tables ], + [qw/asset/], + 'get_tables: checking inheritance' + ); + + ::cmp_deeply( + [ WGT::Class::Asset::Snippet->meta->get_tables ], + [qw/asset snippet/], + 'get_tables: checking inheritance on subclass' + ); + } { @@ -214,10 +232,4 @@ use WebGUI::Test; 'checking inheritance of properties by name, insertion order with an overridden property' ); - cmp_deeply( - [WGT::Class::Asset::NotherOne->meta->get_tables], - [qw/asset snippet/], - 'get_tables returns both tables' - ); - }