[Rt-commit] rt branch, squish-refactor, updated. rt-3.9.7-895-g5b572ed
? sunnavy
sunnavy at bestpractical.com
Mon Dec 27 03:21:10 EST 2010
The branch, squish-refactor has been updated
via 5b572ed8c64f9ce240c548ef81776193d9451c41 (commit)
via ac4105da4e97b07f3b981306a5f440593d261a27 (commit)
via ecaf1a8580703c3e47235a9bd168a83af034e2f9 (commit)
from a5e4e3867164e0d7e38a4bbc824987a4ce981de0 (commit)
Summary of changes:
etc/RT_Config.pm.in | 20 +++++++--
lib/RT.pm | 56 ++++++++++++++++++++++++
lib/RT/Interface/Web.pm | 21 ++++-----
lib/RT/Squish.pm | 53 ++++-------------------
lib/RT/Squish/CSS.pm | 71 +++----------------------------
lib/RT/Squish/JS.pm | 78 +++-------------------------------
share/html/Elements/Header | 12 ++++--
share/html/Elements/HeaderJavascript | 18 ++++----
share/html/NoAuth/css/dhandler | 4 +-
share/html/NoAuth/js/dhandler | 5 +-
t/api/squish.t | 19 ++------
t/web/squish.t | 12 +++---
12 files changed, 135 insertions(+), 234 deletions(-)
- Log -----------------------------------------------------------------
commit ecaf1a8580703c3e47235a9bd168a83af034e2f9
Author: sunnavy <sunnavy at bestpractical.com>
Date: Mon Dec 27 13:56:33 2010 +0800
JSFiles and CSSFiles in config
diff --git a/etc/RT_Config.pm.in b/etc/RT_Config.pm.in
index b27a89c..6c94047 100755
--- a/etc/RT_Config.pm.in
+++ b/etc/RT_Config.pm.in
@@ -2190,17 +2190,17 @@ C<Set(@Plugins, (qw(Extension::QuickDelete RT::Extension::CommandByMail)));>
Set(@Plugins, ());
-=item C<@JSFilesInHead>
+=item C<@JSFiles>
a list of js files to be included in head
Example:
-Set(@JSFilesInHead, qw/jquery-1.4.2.min.js jquery_noconflict.js jquery-ui-1.8.4.custom.min.js jquery.tablesorter.min.js ui.timepickr.js titlebox-state.js util.js userautocomplete.js history-folding.js/);
+Set(@JSFiles, qw/jquery-1.4.2.min.js jquery_noconflict.js jquery-ui-1.8.4.custom.min.js jquery.tablesorter.min.js ui.timepickr.js titlebox-state.js util.js userautocomplete.js history-folding.js/);
=cut
-Set(@JSFilesInHead, qw/
+Set(@JSFiles, qw/
jquery-1.4.2.min.js
jquery_noconflict.js
jquery-ui-1.8.4.custom.min.js
@@ -2222,7 +2222,7 @@ Set(@JSFilesInHead, qw/
Path to jsmin binary.
You can go to http://www.crockford.com/javascript/jsmin.html to get one.
-If specified, it'll be used to minify C<JSFilesInHead>.
+If specified, it'll be used to minify C<JSFiles>.
If fails, RT will then try module L<JavaScript::Minifier>.
If that still fails, RT will simply concatenate js files.
@@ -2232,6 +2232,18 @@ C<Set($JSMinPath, '/path/to/jsmin');>
=cut
+=item C<@CSSFiles>
+
+a list of css files to be included in head
+
+Example:
+
+Set(@CSSFiles, qw/foo.css/);
+
+=cut
+
+Set(@CSSFiles, qw//);
+
=back
=head1 Administrative Interface
commit ac4105da4e97b07f3b981306a5f440593d261a27
Author: sunnavy <sunnavy at bestpractical.com>
Date: Mon Dec 27 14:48:28 2010 +0800
helper methods: RT->AddJavaScripts, AddStyleSheets, JavaScripts and StyleSheets
diff --git a/lib/RT.pm b/lib/RT.pm
index 639f477..f53f14f 100755
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -754,6 +754,62 @@ sub CanonicalizeGeneratedPaths {
}
+=head2 AddJavaScripts
+
+helper method to add js files to C<JSFiles> config.
+to add extra css files, you can add the following line
+in the plugin's main file:
+
+ RT->AddJavaScript( 'foo.js', 'bar.js' );
+
+=cut
+
+sub AddJavaScripts {
+ my $self = shift;
+
+ my @old = RT->Config->Get('JSFiles');
+ RT->Config->Set( 'JSFiles', @old, @_ );
+ return RT->Config->Get('JSFiles');
+}
+
+=head2 AddStyleSheets
+
+helper method to add css files to C<CSSFiles> config
+
+to add extra css files, you can add the following line
+in the plugin's main file:
+
+ RT->AddStyleSheets( 'foo.css', 'bar.css' );
+
+=cut
+
+sub AddStyleSheets {
+ my $self = shift;
+ my @old = RT->Config->Get('CSSFiles');
+ RT->Config->Set( 'CSSFiles', @old, @_ );
+ return RT->Config->Get('CSSFiles');
+}
+
+=head2 JavaScripts
+
+helper method of RT->Config->Get('JSFiles')
+
+=cut
+
+sub JavaScripts {
+ return RT->Config->Get('JSFiles');
+}
+
+=head2 StyleSheets
+
+helper method of RT->Config->Get('CSSFiles')
+
+=cut
+
+sub StyleSheets {
+ return RT->Config->Get('CSSFiles');
+}
+
=head1 BUGS
Please report them to rt-bugs at bestpractical.com, if you know what's
commit 5b572ed8c64f9ce240c548ef81776193d9451c41
Author: sunnavy <sunnavy at bestpractical.com>
Date: Mon Dec 27 16:19:50 2010 +0800
refactor squish: kiss
diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 5f6fef4..7b72959 100755
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -70,32 +70,31 @@ use RT::Interface::Web::Session;
use Digest::MD5 ();
use Encode qw();
-=head2 SquishedCSS $name
+=head2 SquishedCSS $style
=cut
my %SQUISHED_CSS;
sub SquishedCSS {
- my $name = shift or die "need name";
- return $SQUISHED_CSS{$name} if $SQUISHED_CSS{$name};
+ my $style = shift or die "need name";
+ return $SQUISHED_CSS{$style} if $SQUISHED_CSS{$style};
require RT::Squish::CSS;
- my $css = RT::Squish::CSS->new( Name => $name );
- $SQUISHED_CSS{ $css->Name } = $css;
+ my $css = RT::Squish::CSS->new( Style => $style );
+ $SQUISHED_CSS{ $css->Style } = $css;
return $css;
}
-=head2 SquishedJS $name
+=head2 SquishedJS
=cut
-my %SQUISHED_JS;
+my $SQUISHED_JS;
sub SquishedJS {
- my $name = shift or die "need Name";
- return $SQUISHED_JS{$name} if $SQUISHED_JS{$name};
+ return $SQUISHED_JS if $SQUISHED_JS;
require RT::Squish::JS;
- my $js = RT::Squish::JS->new( Name => $name );
- $SQUISHED_JS{ $js->Name } = $js;
+ my $js = RT::Squish::JS->new();
+ $SQUISHED_JS = $js;
return $js;
}
diff --git a/lib/RT/Squish.pm b/lib/RT/Squish.pm
index 4e944b2..7acc386 100644
--- a/lib/RT/Squish.pm
+++ b/lib/RT/Squish.pm
@@ -48,20 +48,9 @@
=head1 SYNOPSIS
- use RT::Squish;
- my $squish = RT::Squish->new(
- Name => 'foo',
- Files => [ '/path/to/a', '/path/to/b' ],
- );
-
=head1 DESCRIPTION
-This module lets you create squished content of files.
-
-to add files automatically by the name, you can create method named
-UpdateFilesByName in your subclass to do this, see
-C<RT::Squish::CSS::UpdateFilesByName>
-and C<RT::Squish::JS::UpdateFilesByName> for example.
+base class of RT::Squish::JS and RT::Squish::CSS
=head1 METHODS
@@ -72,7 +61,7 @@ use warnings;
package RT::Squish;
use base 'Class::Accessor::Fast';
-__PACKAGE__->mk_accessors(qw/Name Files Content Key ModifiedTime ModifiedTimeString/);
+__PACKAGE__->mk_accessors(qw/Content Key ModifiedTime ModifiedTimeString/);
use Digest::MD5 'md5_hex';
use HTTP::Date;
@@ -82,7 +71,6 @@ use HTTP::Date;
ARGS is a hash of named parameters. Valid parameters are:
Name - name for this object
- Files - a reference to a list of files to be squished
=cut
@@ -92,10 +80,7 @@ sub new {
my $self = \%args;
bless $self, $class;
- $self->Files( $args{Files} || [] );
- $self->UpdateFilesByName() if $self->can('UpdateFilesByName');
-
- my $content = $self->SquishFiles;
+ my $content = $self->Squish;
$self->Content($content);
$self->Key( md5_hex $content );
$self->ModifiedTime( time() );
@@ -103,38 +88,18 @@ sub new {
return $self;
}
-=head2 SquishFiles
+=head2 Squish
-default squish action is just concatenating files.
-return the concatenated content.
-
-you can subclass this method to customize the squish way.
+virtual method which does nothing,
+you need to implement this method in subclasses.
=cut
-sub SquishFiles {
- my $self = shift;
- my $files = $self->Files;
- my $c = '';
- local $/;
- for my $file (@$files) {
- open my $fh, '<', $file or die "can't open $file: $!";
- $c .= <$fh>;
- }
-
- return $c;
+sub Squish {
+ $RT::Logger->error( "you need to implement this method in subclasses" );
+ return 1;
}
-=head2 Name
-
-this object's name, to distinguish with other objects
-
-=head2 Files
-
-arrayref of files to be squished.
-
-=head2 Key
-
=head2 Content
squished content
diff --git a/lib/RT/Squish/CSS.pm b/lib/RT/Squish/CSS.pm
index 957f827..15ffc04 100644
--- a/lib/RT/Squish/CSS.pm
+++ b/lib/RT/Squish/CSS.pm
@@ -49,9 +49,7 @@
=head1 SYNOPSIS
use RT::Squish::CSS;
- my $squish = RT::Squish::CSS->new(
- Name => 'aileron',
- );
+ my $squish = RT::Squish::CSS->new( Style => 'aileron');
=head1 DESCRIPTION
@@ -66,73 +64,18 @@ use warnings;
package RT::Squish::CSS;
use base 'RT::Squish', 'CSS::Squish';
-use List::MoreUtils 'uniq';
+__PACKAGE__->mk_accessors(qw/Style/);
-=head2 SquishFiles
+=head2 Squish
use CSS::Squish to squish css
=cut
-sub SquishFiles {
+sub Squish {
my $self = shift;
- return $self->concatenate( @{ $self->Files } );
-}
-
-=head2 AddFiles name => [...]
-
-add files to name
-
-this is mainly for plugins, e.g.
-
-to add extra css files for style 'aileron', you can add the following line
-in the plugin's main file:
-
- require RT::Squish::CSS;
- RT::Squish::CSS->AddFiles( aileron => ['/NoAuth/css/foo.css'] );
-
-=cut
-
-my %FILES_MAP;
-
-sub AddFiles {
- my $self = shift;
- my %args = @_;
-
- for my $name ( keys %args ) {
- next unless $name;
- my $files = $args{$name};
- $FILES_MAP{$name} ||= [];
- push @{ $FILES_MAP{$name} }, ref $files eq 'ARRAY' ? @$files : $files;
- }
- return 1;
-}
-
-=head2 UpdateFilesByName
-
-update files by name, it find files in the following places:
-
-1. if the name is a style name, add the style's corresponding main.css
-
-2. if there is a files map for the name, add the corresponding files.
-
-=cut
-
-sub UpdateFilesByName {
- my $self = shift;
-
- my $name = $self->Name;
- my $main = File::Spec->catfile( '/NoAuth', 'css', $name, 'main.css' );
-
- if ( -e File::Spec->catfile( $RT::MasonComponentRoot, $main ) ) {
- $self->Files( [ uniq @{$self->Files}, $main ] );
- }
-
- if ( $FILES_MAP{$name} ) {
- $self->Files( [ uniq @{$self->Files}, @{$FILES_MAP{$name}} ] );
- }
-
- return 1;
+ my $style = $self->Style;
+ return $self->concatenate( "$style/main.css", RT->Config->Get('CSSFiles') );
}
=head2 file_handle
@@ -144,7 +87,7 @@ subclass CSS::Squish::file_handle for RT
sub file_handle {
my $self = shift;
my $file = shift;
- my $content = $HTML::Mason::Commands::m->scomp($file) || '';
+ my $content = $HTML::Mason::Commands::m->scomp("/NoAuth/css/$file") || '';
open my $fh, '<', \$content or die "$!";
return $fh;
}
diff --git a/lib/RT/Squish/JS.pm b/lib/RT/Squish/JS.pm
index 00c4f2f..8b78e70 100644
--- a/lib/RT/Squish/JS.pm
+++ b/lib/RT/Squish/JS.pm
@@ -49,10 +49,7 @@
=head1 SYNOPSIS
use RT::Squish::JS;
- my $squish = RT::Squish::JS->new(
- Name => 'head',
- Files => ['...'],
- );
+ my $squish = RT::Squish::JS->new();
=head1 DESCRIPTION
@@ -67,87 +64,24 @@ use warnings;
package RT::Squish::JS;
use base 'RT::Squish';
-use List::MoreUtils 'uniq';
-=head2 SquishFiles
+=head2 Squish
-not just concatenate files, but also minify them
+not only concatenate files, but also minify them
=cut
-sub SquishFiles {
+sub Squish {
my $self = shift;
my $content;
- for my $file ( @{ $self->Files } ) {
- $content .= $HTML::Mason::Commands::m->scomp($file);
+ for my $file ( RT->Config->Get('JSFiles') ) {
+ $content .= $HTML::Mason::Commands::m->scomp("/NoAuth/js/$file");
}
return $self->Filter($content);
}
-=head2 AddFiles
-
-add files to name
-
-this is mainly for plugins, e.g.
-
-to add extra js files in 'head', you can add the following line
-in the plugin's main file:
-
- require RT::Squish::JS;
-
- RT::Squish::JS->AddFiles( head => ['/NoAuth/js/foo.js'] );
-
-=cut
-
-my %FILES_MAP;
-
-sub AddFiles {
- my $self = shift;
- my %args = @_;
-
- for my $name ( keys %args ) {
- next unless $name;
- my $files = $args{$name};
- $FILES_MAP{$name} ||= [];
- push @{ $FILES_MAP{$name} }, ref $files eq 'ARRAY' ? @$files : $files;
- }
- return 1;
-}
-
-=head2 UpdateFilesByName
-
-update files by name, it'll try to find files in the following places:
-
-1. if the name is 'head', add files in config item C<JSFilesInHead>.
-
-2. if there is a files map for the name, add the corresponding files.
-
-=cut
-
-sub UpdateFilesByName {
- my $self = shift;
- my $name = $self->Name;
-
- if ( $name eq 'head' ) {
- $self->Files(
- [
- uniq @{ $self->Files },
- map { "/NoAuth/js/$_" } RT->Config->Get('JSFilesInHead'),
- ]
- );
- }
-
- if ( $FILES_MAP{$name} ) {
-
- $self->Files( [ uniq @{$self->Files}, @{$FILES_MAP{$name}} ] );
- }
-
-
- return 1;
-}
-
sub Filter {
my $self = shift;
my $content = shift;
diff --git a/share/html/Elements/Header b/share/html/Elements/Header
index 32aa46a..79687c8 100755
--- a/share/html/Elements/Header
+++ b/share/html/Elements/Header
@@ -56,7 +56,11 @@
% }
<link rel="shortcut icon" href="<%RT->Config->Get('WebImagesURL')%>/favicon.png" type="image/png" />
-<link rel="stylesheet" href="<%RT->Config->Get('WebPath')%>/NoAuth/css/<% $style_path %>" type="text/css" media="all" />
+
+% for my $cssfile ( @css_files ) {
+<link rel="stylesheet" href="<%RT->Config->Get('WebPath')%>/NoAuth/css/<% $cssfile %>" type="text/css" media="all" />
+% }
+
<link rel="stylesheet" href="<%RT->Config->Get('WebPath')%>/NoAuth/css/site.css" type="text/css" media="all" />
<link rel="stylesheet" href="<%RT->Config->Get('WebPath')%>/NoAuth/css/print.css" type="text/css" media="print" />
@@ -102,16 +106,16 @@ $id =~ s|index$||g
if $id ne 'index';
$id =~ s|-$||g;
-my $style_path;
my $style = RT->Config->Get( 'WebDefaultStylesheet', $session{'CurrentUser'} );
+my @css_files;
if ( RT->Config->Get('DevelMode') ) {
- $style_path = "$style/main.css";
+ @css_files = ( "$style/main.css", RT->Config->Get('CSSFiles' ) );
}
else {
my $key =
RT::Interface::Web::SquishedCSS( $style )->Key;
- $style_path = "$style-squished-$key.css";
+ @css_files = "$style-squished-$key.css";
}
</%INIT>
diff --git a/share/html/Elements/HeaderJavascript b/share/html/Elements/HeaderJavascript
index ffeea1a..bc507dc 100644
--- a/share/html/Elements/HeaderJavascript
+++ b/share/html/Elements/HeaderJavascript
@@ -50,12 +50,8 @@ $focus => undef
$onload => undef
</%args>
-% if ( RT->Config->Get('DevelMode' ) ) {
-% for my $jsfile ( RT->Config->Get('JSFilesInHead') ) {
+% for my $jsfile ( @js_files ) {
<script type="text/javascript" src="<%RT->Config->Get('WebPath')%>/NoAuth/js/<% $jsfile %>"></script>
-% } }
-% else {
-<script type="text/javascript" src="<%RT->Config->Get('WebPath')%>/NoAuth/js/<% $js_path %>"></script>
% }
% if ( RT->Config->Get('MessageBoxRichText', $session{'CurrentUser'}) ) {
@@ -77,11 +73,13 @@ $onload => undef
<%INIT>
-my $js_path;
-if ( !RT->Config->Get('DevelMode') ) {
- my $name = 'head';
- my $key = RT::Interface::Web::SquishedJS( $name )->Key;
- $js_path = "$name-squished-$key.js";
+my @js_files;
+if ( RT->Config->Get('DevelMode') ) {
+ @js_files = RT->Config->Get('JSFiles' );
+}
+else {
+ my $key = RT::Interface::Web::SquishedJS()->Key;
+ @js_files = "squished-$key.js";
}
</%INIT>
diff --git a/share/html/NoAuth/css/dhandler b/share/html/NoAuth/css/dhandler
index 83b6ce8..0007974 100644
--- a/share/html/NoAuth/css/dhandler
+++ b/share/html/NoAuth/css/dhandler
@@ -51,8 +51,8 @@ my $squisher;
<%INIT>
my $arg = $m->dhandler_arg;
if ( $arg =~ m{(.*)-squished-[0-9a-f]{32}\.css$} ) {
- my $name = $1;
- my $squished = RT::Interface::Web::SquishedCSS( $name );
+ my $style = $1;
+ my $squished = RT::Interface::Web::SquishedCSS( $style );
# Don't send Last-Modified since we don't handle If-Modified-Since
#$r->header_out( 'Last-Modified' => $squished->ModifiedTimeString );
$m->out( $squished->Content );
diff --git a/share/html/NoAuth/js/dhandler b/share/html/NoAuth/js/dhandler
index a4caeda..9aeae39 100644
--- a/share/html/NoAuth/js/dhandler
+++ b/share/html/NoAuth/js/dhandler
@@ -51,9 +51,8 @@ my $content = '';
<%INIT>
my $arg = $m->dhandler_arg;
-if ( $arg =~ m{(.+?)-squished-[a-f0-9]{32}\.js$} ) {
- my $name = $1;
- my $squished = RT::Interface::Web::SquishedJS( $name );
+if ( $arg =~ m{squished-[a-f0-9]{32}\.js$} ) {
+ my $squished = RT::Interface::Web::SquishedJS();
# Don't send Last-Modified since we don't handle If-Modified-Since
#$r->header_out( 'Last-Modified' => $squished->ModifiedTimeString );
diff --git a/t/api/squish.t b/t/api/squish.t
index 526c930..87e9932 100644
--- a/t/api/squish.t
+++ b/t/api/squish.t
@@ -1,26 +1,17 @@
use strict;
use warnings;
use RT;
-use RT::Test nodb => 1, tests => 10;
+use RT::Test nodb => 1, tests => 7;
use RT::Squish;
use File::Temp 'tempfile';
-my ( $fh1, $file1 ) = tempfile( 'rttestXXXXXX', UNLINK => 1, TMPDIR => 1 );
-print $fh1 "this is file1\n";
-close $fh1;
-my ( $fh2, $file2 ) = tempfile( 'rttestXXXXXX', UNLINK => 1, TMPDIR => 1 );
-print $fh2 "this is file2\n";
-close $fh2;
-
-my $squish = RT::Squish->new( Name => 'foo', Files => [ $file1, $file2 ] );
-for my $method ( qw/Name Files Content ModifiedTime Key/ ) {
+my $squish = RT::Squish->new();
+for my $method ( qw/Content ModifiedTime ModifiedTimeString Key/ ) {
can_ok($squish, $method);
}
-
-is( $squish->Name, 'foo', 'Name' );
-is_deeply( $squish->Files, [ $file1, $file2 ], 'Files' );
-is( $squish->Content, "this is file1\nthis is file2\n", 'Content' );
like( $squish->Key, qr/[a-f0-9]{32}/, 'Key is like md5' );
ok( (time()-$squish->ModifiedTime) <= 2, 'ModifiedTime' );
+use RT::Squish::CSS;
+can_ok('RT::Squish::CSS', 'Style');
diff --git a/t/web/squish.t b/t/web/squish.t
index 23ca0ee..d915f30 100644
--- a/t/web/squish.t
+++ b/t/web/squish.t
@@ -21,7 +21,7 @@ diag "test squished files with devel mode disabled";
$m->back;
my ($js_link) =
- $m->content =~ m!src="([^"]+?-squished-([a-f0-9]{32})\.js)"!;
+ $m->content =~ m!src="([^"]+?squished-([a-f0-9]{32})\.js)"!;
$m->get_ok( $url . $js_link, 'follow squished js' );
$m->content_lacks( 'IE7=', 'no IE7.js by default' );
@@ -33,13 +33,13 @@ SKIP:
{
skip 'need plack server to reinitialize', 6
if $ENV{RT_TEST_WEB_HANDLER} && $ENV{RT_TEST_WEB_HANDLER} ne 'plack';
- require RT::Squish::JS;
- RT::Squish::JS->AddFiles( head => ['/NoAuth/js/IE7/IE7.js'] );
- require RT::Squish::CSS;
- RT::Squish::CSS->AddFiles( aileron => ['/NoAuth/css/print.css'] );
+ RT->AddJavaScripts( 'IE7/IE7.js' );
+ RT->AddStyleSheets( 'print.css' );
( $url, $m ) = RT::Test->started_ok;
$m->login;
+ use File::Slurp;
+ write_file('/tmp/x.html', $m->content);
$m->follow_link_ok( { url_regex => qr!aileron-squished-([a-f0-9]{32})\.css! },
'follow squished css' );
$m->content_like( qr!/\*\* End of .*?.css \*/!, 'squished css' );
@@ -48,7 +48,7 @@ SKIP:
$m->back;
my ($js_link) =
- $m->content =~ m!src="([^"]+?-squished-([a-f0-9]{32})\.js)"!;
+ $m->content =~ m!src="([^"]+?squished-([a-f0-9]{32})\.js)"!;
$m->get_ok( $url . $js_link, 'follow squished js' );
$m->content_contains( 'IE7=', 'has IE7.js' );
RT::Test->stop_server;
-----------------------------------------------------------------------
More information about the Rt-commit
mailing list