From 05cf8fa24cf1ead477d1ca2f4d38c55f8502aeff Mon Sep 17 00:00:00 2001 From: Chris Nehren Date: Fri, 20 Jun 2008 19:21:30 +0000 Subject: [PATCH] Fixed: One can now turn off inheritUrlFromParent, and the code is now more robust: moved from Asset->update to Asset->fixUrl. --- docs/changelog/7.x.x.txt | 2 ++ lib/WebGUI/Asset.pm | 44 ++++++++++------------------------------ t/Asset/Asset.t | 11 ++++++++-- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 9f9a893a9..985236cc9 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -4,6 +4,8 @@ - fixed: Thingy Data now retains File and Images if they aren't explicitly deleted - fixed: Rich Editor no longer makes other form controls show up as code in Safari 3 - fixed: Shop->hasShippingAddress help grammatical error + - fixed: can now turn off inheritUrlFromParent and the code is now more + robust, moved from Asset->update to Asset->fixUrl. 7.5.13 - fixed: storage locations for some assets in packages not imported correctly diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index bc81c3a49..d2051de09 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -598,6 +598,16 @@ sub fixUrl { } $url = $self->session->url->urlize($url); + # if we're inheriting the URL from our parent, set that appropriately + if($self->get('inheritUrlFromParent')) { + my @parts = split(m{/},$url); + + # don't do anything unless we need to + if("/$url" ne $self->getParent->getUrl . '/' . $parts[-1]) { + $url = $self->getParent->getUrl . '/' . $parts[-1]; + } + } + # fix urls used by uploads and extras # and those beginning with http my @badUrls = ( @@ -2006,38 +2016,6 @@ sub update { next; } - # similarly, if this is the new-to-7.5 inheritUrlFromParent field, - # do the same. - if($property eq 'inheritUrlFromParent') { - next unless $properties->{inheritUrlFromParent} == 1; - - # if we're still here, we have the property in the DB. so process it. - # only prepend the URL once - my $parentUrl = $self->getParent->getUrl; - - # handle either being passed a new URL or updating the current one - my $currentUrl; - if(exists $properties->{url}) { - $currentUrl = $properties->{url}; - } - else { - $currentUrl = $self->getUrl; - } - - # if there's only one / then leave it alone. - unless($currentUrl =~ tr{/}{} == 1) { - $currentUrl =~ s{/[^/]+$}{}; - } - - # prepend if it's not a match - my $newUrl = $currentUrl; - if($currentUrl ne $parentUrl) { - $newUrl = $parentUrl . '/' . $currentUrl; - } - - # replace the non-prepended value in the properties hash with this value - $self->{_properties}{url} = $newUrl; - } # use the update value my $value = $properties->{$property}; @@ -2568,7 +2546,7 @@ sub outputWidgetMarkup { my $storage = WebGUI::Storage->get($session, $assetId); my $content = $self->view; WebGUI::Macro::process($session, \$content); - my $jsonContent = objToJson( { "asset$assetId" => { content => $content } } ); + my $jsonContent = to_json( { "asset$assetId" => { content => $content } } ); $storage->addFileFromScalar("$assetId.js", "data = $jsonContent"); my $jsonUrl = $storage->getUrl("$assetId.js"); diff --git a/t/Asset/Asset.t b/t/Asset/Asset.t index 998a05fc0..031311053 100644 --- a/t/Asset/Asset.t +++ b/t/Asset/Asset.t @@ -146,7 +146,7 @@ $canViewMaker->prepare( }, ); -plan tests => 97 +plan tests => 98 + scalar(@fixIdTests) + scalar(@fixTitleTests) + 2*scalar(@getTitleTests) #same tests used for getTitle and getMenuTitle @@ -766,15 +766,19 @@ $properties2 = { url => 'inheriturlfromparent02', }; - my $iufpAsset2 = $iufpAsset->addChild($properties2, $properties2->{id}); $iufpAsset2->update( { inheritUrlFromParent => 1 } ); +$iufpAsset2->commit; is($iufpAsset2->getUrl, '/inheriturlfromparent01/inheriturlfromparent02', 'inheritUrlFromParent works'); # works for setting, now try disabling. Should not change the URL. $iufpAsset2->update( { inheritUrlFromParent => 0 } ); +$iufpAsset2->commit; is($iufpAsset2->getUrl, '/inheriturlfromparent01/inheriturlfromparent02', 'setting inheritUrlFromParent to 0 works'); +# also make sure that it is actually disabled +is($iufpAsset2->get('inheritUrlFromParent'), 0, "disabling inheritUrlFromParent actually works"); + # works for setting and disabling, now ensure it recurses my $properties3 = { @@ -785,8 +789,11 @@ my $properties3 = { url => 'inheriturlfromparent03', }; my $iufpAsset3 = $iufpAsset2->addChild($properties3, $properties3->{id}); +$iufpAsset3->commit; $iufpAsset2->update( { inheritUrlFromParent => 1 } ); +$iufpAsset2->commit; $iufpAsset3->update( { inheritUrlFromParent => 1 } ); +$iufpAsset3->commit; is($iufpAsset3->getUrl, '/inheriturlfromparent01/inheriturlfromparent02/inheriturlfromparent03', 'inheritUrlFromParent recurses properly');