Convert FilePump over to the new Crud.

This commit is contained in:
Colin Kuskie 2010-11-10 09:50:15 -08:00
parent 9abb4a8ee6
commit 2ad9fc1c16
3 changed files with 117 additions and 101 deletions

View file

@ -86,7 +86,7 @@ sub www_addBundleSave {
return $session->privilege->insufficient() unless canView($session); return $session->privilege->insufficient() unless canView($session);
my $form = $session->form; my $form = $session->form;
my $bundleName = $form->get('bundleName'); my $bundleName = $form->get('bundleName');
my $bundle = WebGUI::FilePump::Bundle->create($session, { my $bundle = WebGUI::FilePump::Bundle->new($session, {
bundleName => $bundleName, bundleName => $bundleName,
lastModified => time(), lastModified => time(),
}); });
@ -273,7 +273,7 @@ EOTABLE
; ;
my $rows = ''; my $rows = '';
my $files = $bundle->get($fileType); my $files = $bundle->$fileType;
foreach my $file (@{ $files }) { foreach my $file (@{ $files }) {
my $urlFrag = 'bundleId='.$bundleId.';fileType='.$type.';fileId='.$file->{fileId}; my $urlFrag = 'bundleId='.$bundleId.';fileType='.$type.';fileId='.$file->{fileId};
$rows .= sprintf '<tr><td>%s</td><td>%s</td><td>%s</td></tr>', $rows .= sprintf '<tr><td>%s</td><td>%s</td><td>%s</td></tr>',
@ -342,20 +342,20 @@ sub www_manage {
my $getABundle = WebGUI::FilePump::Bundle->getAllIterator($session,{ orderBy => 'bundleName' } ); my $getABundle = WebGUI::FilePump::Bundle->getAllIterator($session,{ orderBy => 'bundleName' } );
my $notYet = $i18n->get('not yet'); my $notYet = $i18n->get('not yet');
while (my $bundle = $getABundle->()) { while (my $bundle = $getABundle->()) {
my $lastModified = $bundle->get('lastModified'); my $lastModified = $bundle->lastModified;
my $lastBuild = $bundle->get('lastBuild'); my $lastBuild = $bundle->lastBuild;
my $build = ''; my $build = '';
if ($lastModified > $lastBuild) { if ($lastModified > $lastBuild) {
$build = sprintf q| <a href="%s">(%s)</a>|, $build = sprintf q| <a href="%s">(%s)</a>|,
$url->gateway($url->getRequestedUrl,'op=filePump;func=buildBundle;bundleId='.$bundle->getId), $url->gateway($url->getRequestedUrl,'op=filePump;func=buildBundle;bundleId='.$bundle->bundleId),
$i18n->get('build'); $i18n->get('build');
} }
$rows .= sprintf '<tr><td>%s</td><td><a href="%s">%s</a></td><td>%s</td><td>%s</td>', $rows .= sprintf '<tr><td>%s</td><td><a href="%s">%s</a></td><td>%s</td><td>%s</td>',
$session->icon->delete('op=filePump;func=deleteBundle;bundleId='.$bundle->getId), $session->icon->delete('op=filePump;func=deleteBundle;bundleId='.$bundle->bundleId),
$url->gateway($url->getRequestedUrl,'op=filePump;func=editBundle;bundleId='.$bundle->getId), $url->gateway($url->getRequestedUrl,'op=filePump;func=editBundle;bundleId='.$bundle->bundleId),
$bundle->get('bundleName'), $bundle->bundleName,
$bundle->get('lastModified') ? $dt->epochToHuman($lastModified) : $notYet, $bundle->lastModified ? $dt->epochToHuman($lastModified) : $notYet,
$bundle->get('lastBuild') ? $dt->epochToHuman($lastBuild).$build : $notYet, $bundle->lastBuild ? $dt->epochToHuman($lastBuild).$build : $notYet,
; ;
} }
my $output = sprintf <<EOHTML, $i18n->get('bundle name'), $i18n->get('last modified'), $i18n->get('last build'), $rows; my $output = sprintf <<EOHTML, $i18n->get('bundle name'), $i18n->get('last modified'), $i18n->get('last build'), $rows;

View file

@ -1,6 +1,61 @@
package WebGUI::FilePump::Bundle; package WebGUI::FilePump::Bundle;
use base qw/WebGUI::Crud WebGUI::JSONCollateral/; use Moose;
use WebGUI::Definition::Crud;
extends 'WebGUI::Crud';
define tableName => 'filePumpBundle';
define tableKey => 'bundleId';
has bundleId => (
required => 1,
is => 'ro',
);
property bundleName => (
label => 'bundleName',
fieldType => 'text',
builder => '_default_bundleName',
lazy => 1,
);
sub _default_bundleName {
my $session = shift->session;
my $i18n = WebGUI::International->new($session, 'FilePump');
return $i18n->get('new bundle');
}
property lastModified => (
label => 'lastModified',
fieldType => 'integer',
default => 0,
);
property lastBuild => (
label => 'lastBuild',
fieldType => 'integer',
default => 0,
);
property jsFiles => (
label => 'jsFiles',
fieldType => 'textarea',
default => sub { [] },
traits => ['Array', 'WebGUI::Definition::Meta::Property::Serialize',],
isa => 'WebGUI::Type::JSONArray',
coerce => 1,
);
property cssFiles => (
label => 'cssFiles',
fieldType => 'textarea',
default => sub { [] },
traits => ['Array', 'WebGUI::Definition::Meta::Property::Serialize',],
isa => 'WebGUI::Type::JSONArray',
coerce => 1,
);
property otherFiles => (
label => 'otherFiles',
fieldType => 'textarea',
default => sub { [] },
traits => ['Array', 'WebGUI::Definition::Meta::Property::Serialize',],
isa => 'WebGUI::Type::JSONArray',
coerce => 1,
);
with 'WebGUI::Role::Asset::JSONCollateral';
use strict; use strict;
use WebGUI::Asset; use WebGUI::Asset;
use WebGUI::International; use WebGUI::International;
@ -17,6 +72,44 @@ use Data::Dumper;
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 properties
=head3 tableName
filePumpBundle
=head3 tableKey
bundleId
=head3 sequenceKey
None. Bundles have no sequence amongst themselves.
=head3 properties
=head4 bundleName
The name of a bundle
=head4 lastBuild
The date the bundle was last built. This is used to generate the name of the bundled files
for this bundle.
=head4 lastModified
The date the bundle was last modified. With this, and the lastBuild date, you can determine
which bundles need to be rebuilt.
=head4 jsFiles, cssFiles, otherFiles
JSON blobs with files attached to the bundle. js = javascript, css = Cascading Style Sheets, other
means anything else.
=cut
#-------------------------------------------------------------------
=head2 addFile ( $type, $uri ) =head2 addFile ( $type, $uri )
Adds a file of the requested type to the bundle. Returns 1 if the add was successful. Adds a file of the requested type to the bundle. Returns 1 if the add was successful.
@ -42,7 +135,7 @@ sub addFile {
my $collateralType = $type eq 'JS' ? 'jsFiles' my $collateralType = $type eq 'JS' ? 'jsFiles'
: $type eq 'CSS' ? 'cssFiles' : $type eq 'CSS' ? 'cssFiles'
: 'otherFiles'; : 'otherFiles';
my $files = $self->get($collateralType); my $files = $self->$collateralType;
my $uriExists = $self->getJSONCollateralDataIndex($files, 'uri', $uri) != -1 ? 1 : 0; my $uriExists = $self->getJSONCollateralDataIndex($files, 'uri', $uri) != -1 ? 1 : 0;
return 0, 'Duplicate URI' if $uriExists; return 0, 'Duplicate URI' if $uriExists;
@ -91,13 +184,13 @@ the method returns 0, along with an error message.
sub build { sub build {
my ($self) = @_; my ($self) = @_;
my $newBuild = time(); my $newBuild = time();
my $originalBuild = $self->get('lastBuild'); my $originalBuild = $self->lastBuild;
##Whole lot of building ##Whole lot of building
my $error = undef; my $error = undef;
##JavaScript first ##JavaScript first
my $jsFiles = $self->get('jsFiles'); my $jsFiles = $self->jsFiles;
my $concatenatedJS = ''; my $concatenatedJS = '';
JSFILE: foreach my $jsFile (@{ $jsFiles }) { JSFILE: foreach my $jsFile (@{ $jsFiles }) {
my $uri = $jsFile->{uri}; my $uri = $jsFile->{uri};
@ -112,7 +205,7 @@ sub build {
return (0, $error) if ($error); return (0, $error) if ($error);
##CSS next ##CSS next
my $cssFiles = $self->get('cssFiles'); my $cssFiles = $self->cssFiles;
my $concatenatedCSS = ''; my $concatenatedCSS = '';
CSSFILE: foreach my $cssFile (@{ $cssFiles }) { CSSFILE: foreach my $cssFile (@{ $cssFiles }) {
my $uri = $cssFile->{uri}; my $uri = $cssFile->{uri};
@ -138,7 +231,7 @@ sub build {
} }
##Copy files over ##Copy files over
my $otherFiles = $self->get('otherFiles'); my $otherFiles = $self->otherFiles;
OTHERFILE: foreach my $file (@{ $otherFiles }) { OTHERFILE: foreach my $file (@{ $otherFiles }) {
my $uri = $file->{uri}; my $uri = $file->{uri};
my $results = $self->fetch($uri); my $results = $self->fetch($uri);
@ -287,84 +380,6 @@ sub _buildFile {
return 0; return 0;
} }
#-------------------------------------------------------------------
=head2 crud_definition
WebGUI::Crud definition for this class.
=head3 tableName
filePumpBundle
=head3 tableKey
bundleId
=head3 sequenceKey
None. Bundles have no sequence amongst themselves.
=head3 properties
=head4 bundleName
The name of a bundle
=head4 lastBuild
The date the bundle was last built. This is used to generate the name of the bundled files
for this bundle.
=head4 lastModified
The date the bundle was last modified. With this, and the lastBuild date, you can determine
which bundles need to be rebuilt.
=head4 jsFiles, cssFiles, otherFiles
JSON blobs with files attached to the bundle. js = javascript, css = Cascading Style Sheets, other
means anything else.
=cut
sub crud_definition {
my ($class, $session) = @_;
my $definition = $class->SUPER::crud_definition($session);
my $i18n = WebGUI::International->new($session, 'FilePump');
$definition->{tableName} = 'filePumpBundle';
$definition->{tableKey} = 'bundleId';
$definition->{sequenceKey} = '';
my $properties = $definition->{properties};
$properties->{bundleName} = {
fieldType => 'text',
defaultValue => $i18n->get('new bundle'),
};
$properties->{lastModified} = {
fieldType => 'integer',
defaultValue => 0,
};
$properties->{lastBuild} = {
fieldType => 'integer',
defaultValue => 0,
};
$properties->{jsFiles} = {
fieldType => 'textarea',
defaultValue => [],
serialize => 1,
};
$properties->{cssFiles} = {
fieldType => 'textarea',
defaultValue => [],
serialize => 1,
};
$properties->{otherFiles} = {
fieldType => 'textarea',
defaultValue => [],
serialize => 1,
};
return $definition;
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -514,7 +529,7 @@ sub fetchAsset {
return {} if Exception::Class->caught(); return {} if Exception::Class->caught();
##Check for a snippet, or snippet subclass? ##Check for a snippet, or snippet subclass?
my $guts = { my $guts = {
lastModified => $asset->get('lastModified'), lastModified => $asset->lastModified,
content => '', content => '',
}; };
if ($asset->isa('WebGUI::Asset::Snippet')) { if ($asset->isa('WebGUI::Asset::Snippet')) {
@ -522,7 +537,7 @@ sub fetchAsset {
WebGUI::Macro::process($self->session, \( $guts->{content} ) ); WebGUI::Macro::process($self->session, \( $guts->{content} ) );
} }
elsif ($asset->isa('WebGUI::Asset::File')) { elsif ($asset->isa('WebGUI::Asset::File')) {
$guts->{content} = $asset->getStorageLocation->getFileContentsAsScalar($asset->get('filename')); $guts->{content} = $asset->getStorageLocation->getFileContentsAsScalar($asset->filename);
} }
return $guts; return $guts;
} }
@ -640,7 +655,7 @@ Returns a urlized version of the bundle name, safe for URLs and filenames.
sub bundleUrl { sub bundleUrl {
my ($self) = @_; my ($self) = @_;
return $self->session->url->urlize($self->get('bundleName')); return $self->session->url->urlize($self->bundleName);
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -658,7 +673,7 @@ Another time stamp to use instead of the lastModified timestamp.
sub getPathClassDir { sub getPathClassDir {
my ($self, $lastBuild) = @_; my ($self, $lastBuild) = @_;
$lastBuild ||= $self->get('lastBuild'); $lastBuild ||= $self->lastBuild;
return Path::Class::Dir->new( return Path::Class::Dir->new(
$self->session->config->get('uploadsPath'), $self->session->config->get('uploadsPath'),
'filepump', 'filepump',

View file

@ -32,16 +32,17 @@ my $session = WebGUI::Test->session;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Tests # Tests
plan tests => 64; plan tests => 65;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# put your tests here # put your tests here
use WebGUI::FilePump::Bundle; use WebGUI::FilePump::Bundle;
my $bundle = WebGUI::FilePump::Bundle->create($session); my $bundle = WebGUI::FilePump::Bundle->new($session);
isa_ok($bundle, 'WebGUI::FilePump::Bundle'); isa_ok($bundle, 'WebGUI::FilePump::Bundle');
isa_ok($bundle, 'WebGUI::Crud'); isa_ok($bundle, 'WebGUI::Crud');
can_ok($bundle, qw/update write getJSONCollateralDataIndex/);
is($bundle->get('lastModified'), 0, 'by default, lastModified is 0'); is($bundle->get('lastModified'), 0, 'by default, lastModified is 0');
@ -449,7 +450,7 @@ ok(!-e $buildDir->stringify, 'delete deletes the current build directory');
my @jsFiles = qw/hoverhelp.js inputcheck.js/; my @jsFiles = qw/hoverhelp.js inputcheck.js/;
foreach my $jsFile (@jsFiles) { foreach my $jsFile (@jsFiles) {
my $bundle = WebGUI::FilePump::Bundle->create($session); my $bundle = WebGUI::FilePump::Bundle->new($session);
$bundle->addFile('JS', 'file:extras/'.$jsFile); $bundle->addFile('JS', 'file:extras/'.$jsFile);
lives_ok { $bundle->build } "built file $jsFile"; lives_ok { $bundle->build } "built file $jsFile";
$bundle->delete; $bundle->delete;