Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed helper export bug in Mojolicious::Plugin::EPRenderer
  • Loading branch information
kraih committed Dec 31, 2013
1 parent 8de63d8 commit 206045e
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

4.64 2013-12-30
4.64 2014-01-01
- Fixed helper export bug in Mojolicious::Plugin::EPRenderer.

4.63 2013-12-19
- Deprecated Mojolicious::secret in favor of Mojolicious::secrets.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Loader.pm
Expand Up @@ -29,17 +29,17 @@ sub load {
}

sub search {
my ($self, $namespace) = @_;
my ($self, $ns) = @_;

my (@modules, %found);
for my $directory (@INC) {
next unless -d (my $path = catdir $directory, split(/::|'/, $namespace));
next unless -d (my $path = catdir $directory, split(/::|'/, $ns));

# List "*.pm" files in directory
opendir(my $dir, $path);
for my $file (grep /\.pm$/, readdir $dir) {
next if -d catfile splitdir($path), $file;
my $class = "${namespace}::" . fileparse $file, qr/\.pm/;
my $class = "${ns}::" . fileparse $file, qr/\.pm/;
push @modules, $class unless $found{$class}++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious.pm
Expand Up @@ -649,7 +649,7 @@ that have been bundled for internal use.
=head2 Mojolicious Artwork
Copyright (C) 2010-2013, Sebastian Riedel.
Copyright (C) 2010-2014, Sebastian Riedel.
Licensed under the CC-SA License, Version 4.0
L<http://creativecommons.org/licenses/by-sa/4.0>.
Expand Down Expand Up @@ -957,7 +957,7 @@ Zak B. Elep
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2008-2013, Sebastian Riedel.
Copyright (C) 2008-2014, Sebastian Riedel.
This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -84,10 +84,10 @@ sub run {
# Find all available commands
my (@commands, %seen);
my $loader = Mojo::Loader->new;
for my $namespace (@{$self->namespaces}) {
for my $module (@{$loader->search($namespace)}) {
for my $ns (@{$self->namespaces}) {
for my $module (@{$loader->search($ns)}) {
next unless my $command = _command($module);
$command =~ s/^${namespace}:://;
$command =~ s/^${ns}:://;
push @commands, [$command => $module] unless $seen{$command}++;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Plugin/EPRenderer.pm
Expand Up @@ -2,7 +2,7 @@ package Mojolicious::Plugin::EPRenderer;
use Mojo::Base 'Mojolicious::Plugin';

use Mojo::Template;
use Mojo::Util qw(encode md5_sum monkey_patch);
use Mojo::Util qw(encode md5_sum monkey_patch steady_time);

sub register {
my ($self, $app, $conf) = @_;
Expand Down Expand Up @@ -51,10 +51,10 @@ sub register {
}

sub _helpers {
my ($namespace, $helpers) = @_;
my ($ns, $helpers) = @_;
for my $name (grep {/^\w+$/} keys %$helpers) {
monkey_patch $namespace, $name,
sub { $helpers->{$name}->($namespace->_C, @_) };
monkey_patch $ns, $name,
sub { $ns->_C->app->renderer->helpers->{$name}->($ns->_C, @_) };
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugins.pm
Expand Up @@ -34,8 +34,8 @@ sub load_plugin {

# Try all namespaces
my $class = $name =~ /^[a-z]/ ? camelize($name) : $name;
for my $namespace (@{$self->namespaces}) {
my $module = "${namespace}::$class";
for my $ns (@{$self->namespaces}) {
my $module = "${ns}::$class";
return $module->new if _load($module);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -133,9 +133,9 @@ sub _class {
if ($field->{app}) { push @classes, $field->{app} }

# Specific namespace
elsif (defined(my $namespace = $field->{namespace})) {
if ($class) { push @classes, $namespace ? "${namespace}::$class" : $class }
elsif ($namespace) { push @classes, $namespace }
elsif (defined(my $ns = $field->{namespace})) {
if ($class) { push @classes, $ns ? "${ns}::$class" : $class }
elsif ($ns) { push @classes, $ns }
}

# All namespaces
Expand Down
20 changes: 16 additions & 4 deletions t/mojolicious/embedded_lite_app.t
Expand Up @@ -171,6 +171,7 @@ $t->get_ok('/just/works/too')->status_is(200)->content_is("It just works!\n");

# Template from myapp.pl
$t->get_ok('/x/1/')->status_is(200)->content_is(<<'EOF');
myapp
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
Expand All @@ -196,13 +197,14 @@ $t->get_ok('/x/1/url/☃')->status_is(200)

# Route to template from myapp.pl
$t->get_ok('/x/1/template/menubar')->status_is(200)
->content_is("works ♥!Insecure!Insecure!\n");
->content_is("myapp\nworks ♥!Insecure!Insecure!\n");

# Missing template from myapp.pl
$t->get_ok('/x/1/template/does_not_exist')->status_is(404);

# Template from myapp.pl with unicode prefix
$t->get_ok('/x/♥/')->status_is(200)->content_is(<<'EOF');
myapp
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
Expand All @@ -228,13 +230,18 @@ $t->get_ok('/x/♥/url/☃')->status_is(200)

# Route to template from myapp.pl with unicode prefix
$t->get_ok('/x/♥/template/menubar')->status_is(200)
->content_is("works ♥!Insecure!Insecure!\n");
->content_is("myapp\nworks ♥!Insecure!Insecure!\n");

# Missing template from myapp.pl with unicode prefix
$t->get_ok('/x/♥/template/does_not_exist')->status_is(404);

# A little bit of everything from myapp2.pl
$t->get_ok('/y/1')->status_is(200)->content_is("works 4!\nInsecure too!");
$t->get_ok('/y/1')->status_is(200)
->content_is("myapp2\nworks 4!\nInsecure too!");

# Route to template from myapp.pl again (helpers sharing the same name)
$t->get_ok('/x/1/template/menubar')->status_is(200)
->content_is("myapp\nworks ♥!Insecure!Insecure!\n");

# Caching helper from myapp2.pl
$t->get_ok('/y/1/cached?cache=foo')->status_is(200)->content_is('foo');
Expand All @@ -246,7 +253,8 @@ $t->get_ok('/y/1/cached?cache=fail')->status_is(200)->content_is('foo');
$t->get_ok('/y/1/2')->status_is(404);

# myapp2.pl with unicode prefix
$t->get_ok('/y/♥')->status_is(200)->content_is("works 3!\nInsecure too!");
$t->get_ok('/y/♥')->status_is(200)
->content_is("myapp2\nworks 3!\nInsecure too!");

# Caching helper from myapp2.pl with unicode prefix
$t->get_ok('/y/♥/cached?cache=bar')->status_is(200)->content_is('bar');
Expand All @@ -263,6 +271,7 @@ $t->get_ok('/host')->status_is(200)->content_is('main application!');
# Template from myapp.pl with domain
$t->get_ok('/' => {Host => 'mojolicious.org'})->status_is(200)
->content_is(<<'EOF');
myapp
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
Expand All @@ -279,6 +288,7 @@ $t->get_ok('/host' => {Host => 'mojolicious.org'})->status_is(200)
# Template from myapp.pl with domain again
$t->get_ok('/' => {Host => 'mojolicio.us'})->status_is(200)
->content_is(<<'EOF');
myapp
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
Expand All @@ -295,6 +305,7 @@ $t->get_ok('/host' => {Host => 'mojolicio.us'})->status_is(200)
# Template from myapp.pl with wildcard domain
$t->get_ok('/' => {Host => 'example.com'})->status_is(200)
->content_is(<<'EOF');
myapp
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
Expand All @@ -319,6 +330,7 @@ $t->get_ok('/host' => {Host => 'foo.bar.example.com'})->status_is(200)
# Template from myapp.pl with wildcard domain and unicode prefix
$t->get_ok('/♥/123/' => {Host => 'foo-bar.de'})->status_is(200)
->content_is(<<'EOF');
myapp
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
Expand Down
4 changes: 4 additions & 0 deletions t/mojolicious/external/myapp.pl
Expand Up @@ -5,6 +5,9 @@
# Default for config file tests
app->defaults(secret => 'Insecure!');

# Helpers sharing the same name in different embedded applications
helper same_name => sub {'myapp'};

# Load plugin
plugin 'Config';

Expand Down Expand Up @@ -54,4 +57,5 @@
__DATA__
@@ menubar.html.ep
%= same_name
<%= $config->{just} %><%= $config->{one} %><%= $config->{two} %>
4 changes: 4 additions & 0 deletions t/mojolicious/external/myapp2.pl
Expand Up @@ -6,6 +6,9 @@
# Default for config file tests
app->defaults(secret => 'Insecure too!');

# Helpers sharing the same name in different embedded applications
helper same_name => sub {'myapp2'};

# Caching helper for state variable test
helper my_cache => sub { state $cache = shift->param('cache') };

Expand All @@ -30,4 +33,5 @@
__DATA__
@@ menubar.html.ep
%= same_name
%= stash('message') || 'works 4!'
1 change: 1 addition & 0 deletions t/mojolicious/external_lite_app.t
Expand Up @@ -17,6 +17,7 @@ my $t = Test::Mojo->new;

# Template from myapp.pl
$t->get_ok('/')->status_is(200)->content_is(<<'EOF');
myapp
works ♥!Insecure!Insecure!
too!works!!!Mojolicious::Plugin::Config::Sandbox
Expand Down

0 comments on commit 206045e

Please sign in to comment.