From ec4713855ac0d92fd837254c3ff24f278c4465a9 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 13 Apr 2009 16:33:24 +0000 Subject: [PATCH] Convert Story asset to use Class::C3 properly. Add a method to duplicate the photo collateral JSON data, with tests. Make addRevision and duplicate use that method. --- lib/WebGUI/Asset/Story.pm | 66 ++++++++++++++++++++++++++++----------- t/Asset/Story.t | 22 ++++++++++++- 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/lib/WebGUI/Asset/Story.pm b/lib/WebGUI/Asset/Story.pm index beeaff2b8..d6f1d3ffe 100644 --- a/lib/WebGUI/Asset/Story.pm +++ b/lib/WebGUI/Asset/Story.pm @@ -76,16 +76,8 @@ sub addRevision { $newSelf->update($newProperties); - ##Make a copy of each storage location stored in the JSON for this new revision. - my $photoData = $newSelf->getPhotoData; - PHOTO: foreach my $photo (@{ $photoData }) { - next PHOTO unless $photo->{storageId}; - my $oldStorage = WebGUI::Storage->get($session, $photo->{storageId}); - my $newStorage = $oldStorage->copy; - $photo->{storageId} = $newStorage->getId; - } - $newSelf->setPhotoData($photoData); - + my $newPhotoData = $newSelf->duplicatePhotoData; + $newSelf->setPhotoData($newPhotoData); $newSelf->requestAutoCommit; return $newSelf; @@ -165,10 +157,48 @@ sub definition { properties => \%properties, autoGenerateForms => 0, }); - return $class->SUPER::definition($session, $definition); + return $class->next::method($session, $definition); } +#------------------------------------------------------------------- + +=head2 duplicate ( ) + +Extent the method from Asset to handle duplicating storage locations. + +=cut + +sub duplicate { + my $self = shift; + my $newSelf = $self->next::method(@_); + my $newPhotoData = $newSelf->duplicatePhotoData; + $newSelf->setPhotoData($newPhotoData); + return $newSelf; +} + +#------------------------------------------------------------------- + +=head2 duplicatePhotoData ( ) + +Duplicate photo data, particularly storage locations. Returns the duplicated +perl structure. + +=cut + +sub duplicatePhotoData { + my $self = shift; + my $session = $self->session; + my $photoData = $self->getPhotoData; + PHOTO: foreach my $photo (@{ $photoData }) { + next PHOTO unless $photo->{storageId}; + my $oldStorage = WebGUI::Storage->get($session, $photo->{storageId}); + my $newStorage = $oldStorage->copy; + $photo->{storageId} = $newStorage->getId; + } + return $photoData; +} + #------------------------------------------------------------------- =head2 exportAssetData ( ) @@ -180,7 +210,7 @@ Add the storage location to the export data. sub exportAssetData { my $self = shift; - my $data = $self->SUPER::exportAssetData; + my $data = $self->next::method; return $data; } @@ -492,7 +522,7 @@ is passed, it will use that template instead. sub prepareView { my $self = shift; - $self->SUPER::prepareView(); + $self->next::method(); my $templateId; my $topic = $self->topic; if ($topic) { @@ -518,7 +548,7 @@ Handle photos and photo metadata, like captions, etc. sub processPropertiesFromFormPost { my $self = shift; my $session = $self->session; - $self->SUPER::processPropertiesFromFormPost; + $self->next::method; my $form = $session->form; ##Handle old data first, to avoid iterating across a newly added photo. my $photoData = $self->getPhotoData; @@ -579,7 +609,7 @@ sub purge { } } $sth->finish; - return $self->SUPER::purge; + return $self->next::method; } #------------------------------------------------------------------- @@ -597,7 +627,7 @@ sub purgeRevision { my $storage = WebGUI::Storage->get($session, $self-$photo->{storageId}); $storage->delete if $storage; } - return $self->SUPER::purgeRevision; + return $self->next::method; } #------------------------------------------------------------------- @@ -676,7 +706,7 @@ sub setSize { $fileSize += $storage->getFileSize($file); } } - return $self->SUPER::setSize($fileSize); + return $self->next::method($fileSize); } #------------------------------------------------------------------- @@ -712,7 +742,7 @@ Extend the superclass to make sure that the asset always stays hidden from navig sub update { my $self = shift; my $properties = shift; - return $self->SUPER::update({%$properties, isHidden => 1}); + return $self->next::method({%$properties, isHidden => 1}); } #------------------------------------------------------------------- diff --git a/t/Asset/Story.t b/t/Asset/Story.t index ed151b7ee..346ce751b 100644 --- a/t/Asset/Story.t +++ b/t/Asset/Story.t @@ -20,7 +20,7 @@ use Test::More; # increment this value for each test you create use Test::Deep; use Data::Dumper; -my $tests = 37; +my $tests = 38; plan tests => 1 + $tests ; @@ -339,6 +339,26 @@ cmp_deeply( ok($viewVariables->{singlePhoto}, 'viewVariables: singlePhoto: there is just 1'); ok($viewVariables->{hasPhotos}, 'viewVariables: hasPhotos: it has photos'); +############################################################ +# +# duplicatePhotoData +# +############################################################ + +$photoData = $story->getPhotoData; +$photoData->[0]->{storageId} = ignore(); +$photoData->[1]->{storageId} = ignore(); +my $newPhotoData = $story->duplicatePhotoData; + +cmp_deeply( + $newPhotoData, + $photoData, + 'duplicatePhotoData: checking JSON data minus storage locations' +); + +WebGUI::Storage->get($session, $newPhotoData->[0]->{storageId})->delete; +WebGUI::Storage->get($session, $newPhotoData->[1]->{storageId})->delete; + } END {