From 2eed74891aeb662276fabcd69df922fa7630d2c3 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 14 Sep 2010 10:42:59 -0700 Subject: [PATCH] Restore addAttachments and restoreAttachments to the 7.x series. In 8.x, the object property will act like an arrayref, so they won't be necessary. Fixes bug #11861. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Asset/Template.pm | 54 ++++++++++++++++++++++++++++++++++++ t/Asset/Template.t | 30 +++++++++++++++++++- 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index edaca3695..53ce88cae 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -6,6 +6,7 @@ - fixed #11788: Calendar - Can't enter Midnight - Broke page layout - fixed #11855: Purging Shortcut from Trash causes loop - fixed #11865: URL with slash no longer works. + - fixed #11861: addAttachment method removed from WebGUI 7 thus breaking the API 7.10.0 - fixed #11812: Checking www_ajaxSave's response in the cart js, urlencoding post parameters diff --git a/lib/WebGUI/Asset/Template.pm b/lib/WebGUI/Asset/Template.pm index a928b4958..bdd0d2a65 100644 --- a/lib/WebGUI/Asset/Template.pm +++ b/lib/WebGUI/Asset/Template.pm @@ -48,6 +48,34 @@ These methods are available from this class: #------------------------------------------------------------------- +=head2 addAttachments ( new_attachments ) + +Adds attachments to this template. New attachments are added to the end of the current set of +attachments. + +=head3 new_attachments + +An arrayref of hashrefs, where each hashref should have at least url and type. All +other keys will be ignored. + +=cut + +sub addAttachments { + my ($self, $new_attachments) = @_; + my $attachments = $self->getAttachments(); + + foreach my $a (@{ $new_attachments }) { + push @{ $attachments }, { + url => $a->{url}, + type => $a->{type}, + }; + } + my $json = JSON->new->encode( $attachments ); + $self->update({ attachmentsJson => $json, }); +} + +#------------------------------------------------------------------- + =head2 cut ( ) Extend the base method to handle cutting the User Function Style template and destroying your site. @@ -704,6 +732,32 @@ sub purge { #------------------------------------------------------------------- +=head2 removeAttachments ( urls ) + +Removes attachments. + +=head3 urls + +C is an arrayref of URLs to remove. If C +is not defined, will remove all attachments for this revision. + +=cut + +sub removeAttachments { + my ($self, $urls) = @_; + + my @attachments = (); + + if ($urls) { + @attachments = grep { !isIn($_->{url}, @{ $urls }) } @{ $self->getAttachments() }; + } + + my $json = JSON->new->encode( \@attachments ); + $self->update({ attachmentsJson => $json, }); +} + +#------------------------------------------------------------------- + =head2 update Override update from Asset.pm to handle backwards compatibility with the old diff --git a/t/Asset/Template.t b/t/Asset/Template.t index c15281609..da42347c4 100644 --- a/t/Asset/Template.t +++ b/t/Asset/Template.t @@ -16,7 +16,7 @@ use WebGUI::Test; use WebGUI::Session; use WebGUI::Asset::Template; use Exception::Class; -use Test::More tests => 53; # increment this value for each test you create +use Test::More tests => 57; # increment this value for each test you create use Test::Deep; use Data::Dumper; use Test::Exception; @@ -133,6 +133,34 @@ is($att4->[1]->{url}, 'bar', 'rev still has bar'); is($att4->[2]->{url}, 'baz', 'rev does have new thing') or diag( $template3rev->get('attachmentsJson') ); is(@$att4, 3, 'rev is proper size'); +$template3rev->addAttachments([{ url => 'box', type => 'headScript', }, { url => 'bux', type => 'headScript', }, ]); +cmp_deeply( + [ map { $_->{url} } @{ $template3rev->getAttachments('headScript') } ], + [qw/foo bar baz box bux/], + 'addAttachments appends to the end' +) or diag $template3rev->get('attachmentsJson'); + +$template3rev->removeAttachments(['box']); +cmp_deeply( + [ map { $_->{url} } @{ $template3rev->getAttachments('headScript') } ], + [qw/foo bar baz bux/], + 'removeAttachments will remove urls by exact URL match' +) or diag $template3rev->get('attachmentsJson'); + +$template3rev->removeAttachments(['bu']); +cmp_deeply( + [ map { $_->{url} } @{ $template3rev->getAttachments('headScript') } ], + [qw/foo bar baz bux/], + '... checking that it is not treated like a wildcard' +) or diag $template3rev->get('attachmentsJson'); + +$template3rev->removeAttachments(); +cmp_deeply( + [ map { $_->{url} } @{ $template3rev->getAttachments('headScript') } ], + [ ], + '... checking that all attachments are removed' +) or diag $template3rev->get('attachmentsJson'); + $template3rev->purgeRevision(); ## Check how templates in the trash and clipboard are handled.