Skip to content

Commit

Permalink
rename special attribute to extra
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 26, 2017
1 parent 25819ea commit ecc4174
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

7.38 2017-07-24
7.38 2017-07-26
- Added extra attribute to Mojolicious::Static. (jabberwok)

7.37 2017-07-21
- Added slugify method to Mojo::ByteStream. (Grinnz)
Expand Down
5 changes: 5 additions & 0 deletions lib/Mojolicious.pm
Expand Up @@ -539,6 +539,9 @@ L<Mojolicious::Static> object.
# Add another class with static files in DATA section
push @{$app->static->classes}, 'Mojolicious::Plugin::Fun';
# Remove built-in favicon
delete $app->static->extra->{'favicon.ico'};
=head2 types
my $types = $app->types;
Expand Down Expand Up @@ -1090,6 +1093,8 @@ Viliam Pucik
Wes Cravens
William Lindley
Yaroslav Korshak
Yuki Kimoto
Expand Down
49 changes: 21 additions & 28 deletions lib/Mojolicious/Static.pm
Expand Up @@ -9,21 +9,15 @@ use Mojo::Home;
use Mojo::Loader 'data_section';
use Mojo::Util 'md5_sum';

# Bundled files
my $PUBLIC = Mojo::Home->new(Mojo::Home->new->mojo_lib_dir)
->child('Mojolicious', 'resources', 'public');
my %EXTRA = $PUBLIC->list_tree->map(
sub { join('/', @{$_->to_rel($PUBLIC)}), $_->realpath->to_string })->each;

has classes => sub { ['main'] };
has extra => sub { +{%EXTRA} };
has paths => sub { [] };
has special => sub {

# Bundled files are handled specially
my $public = Mojo::Home->new(Mojo::Home->new->mojo_lib_dir)
->child('Mojolicious', 'resources', 'public');
return {
@{
$public->list_tree->map(
sub { join('/', @{$_->to_rel($public)}), $_->realpath; }
)
}
};
};

sub dispatch {
my ($self, $c) = @_;
Expand Down Expand Up @@ -57,9 +51,9 @@ sub file {
# Search DATA
if (my $asset = $self->_get_data_file($rel)) { return $asset }

# Search special files
my $special = $self->special;
return exists $special->{$rel} ? $self->_get_file($special->{$rel}) : undef;
# Search extra files
my $extra = $self->extra;
return exists $extra->{$rel} ? $self->_get_file($extra->{$rel}) : undef;
}

sub is_fresh {
Expand Down Expand Up @@ -168,7 +162,6 @@ Mojolicious::Static - Serve static files
my $static = Mojolicious::Static->new;
push @{$static->classes}, 'MyApp::Controller::Foo';
push @{$static->paths}, '/home/sri/public';
delete $static->special->{'mojo/jquery/jquery.js'};
=head1 DESCRIPTION
Expand Down Expand Up @@ -199,6 +192,17 @@ startup.
# Add another class with static files in DATA section and higher precedence
unshift @{$static->classes}, 'Mojolicious::Plugin::MoreFun';
=head2 extra
my $extra = $static->extra;
$static = $static->extra({'foo/bar.txt' => '/home/sri/myapp/bar.txt'};
Paths for extra files to be served from locations other than L</"paths">, such
as images used by the built-in exception and not found pages.
# Remove built-in favicon
delete $static->extra->{'favicon.ico'};
=head2 paths
my $paths = $static->paths;
Expand All @@ -212,17 +216,6 @@ Directories to serve static files from, first one has the highest precedence.
# Add another "public" directory with higher precedence
unshift @{$static->paths}, '/home/sri/themes/blue/public';
=head2 special
my $special_files = $static->special;
my $icon_abs_path = $static->special->{'favicon.ico'};
Paths for which special files will be served: a hash mapping HTTP request paths
to filenames. This is populated by default with the favicon, jQuery, and other
files used by the built-in exception and not_found pages. Deleting an entry
will suppress the serving of its special file; that path will then be handled
as a normal route.
=head1 METHODS
L<Mojolicious::Static> inherits all methods from L<Mojo::Base> and implements
Expand Down
8 changes: 5 additions & 3 deletions t/mojolicious/production_app.t
Expand Up @@ -33,11 +33,13 @@ is $t->app, $t->app->commands->app, 'applications are equal';
is $t->app->static->file('hello.txt')->slurp,
"Hello Mojo from a static file!\n", 'right content';
is $t->app->static->file('does_not_exist.html'), undef, 'no file';
isnt $t->app->static->file('mojo/jquery/jquery.js'), undef, 'find built-in jQuery';
delete $t->app->static->special->{'mojo/jquery/jquery.js'};
is $t->app->static->file('mojo/jquery/jquery.js'), undef, 'suppress built-in jQuery';
is $t->app->moniker, 'mojolicious_test', 'right moniker';

# Remove extra files
isnt $t->app->static->file('mojo/jquery/jquery.js'), undef, 'found jQuery';
delete $t->app->static->extra->{'mojo/jquery/jquery.js'};
is $t->app->static->file('mojo/jquery/jquery.js'), undef, 'no jQuery';

# Default namespaces
is_deeply $t->app->routes->namespaces,
['MojoliciousTest::Controller', 'MojoliciousTest'], 'right namespaces';
Expand Down

0 comments on commit ecc4174

Please sign in to comment.