File Coverage

lib/Mojolicious/Plugin/RelativeUrlFor.pm
Criterion Covered Total %
statement 21 24 87.5
branch 3 6 50.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 30 36 83.3


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::RelativeUrlFor;
2 3     3   4414 use Mojo::Base 'Mojolicious::Plugin';
  3         20713  
  3         27  
3              
4             our $VERSION = '0.04';
5              
6             sub register {
7 2     2 1 92 my ($self, $app, $conf) = @_;
8              
9             # url_for helper
10 2         140 my $url_for = *Mojolicious::Controller::url_for{CODE};
11              
12             # helper sub ref
13             my $rel_url_for = sub {
14 7     7   466515 my $c = shift;
15              
16             # create urls
17 7         34 my $url = $url_for->($c, @_)->to_abs;
18 7         9314 my $req_url = $c->req->url->to_abs;
19              
20             # return relative version if request url exists
21 7 50       3850 if ($req_url->to_string) {
22              
23             # repair if empty
24 7         2827 my $rel_url = $url->to_rel($req_url);
25 0 0       0 return Mojo::URL->new('./') unless $rel_url->to_string;
26 0         0 return $rel_url;
27             }
28              
29             # change nothing without request url
30 0         0 return $url;
31 2         10 };
32              
33             # register rel(ative)_url_for helpers
34 2         17 $app->helper(relative_url_for => $rel_url_for);
35 2         222 $app->helper(rel_url_for => $rel_url_for);
36              
37             # replace url_for helper
38 2 100       176 if ($conf->{replace_url_for}) {
39 3     3   3942 no strict 'refs';
  3         5  
  3         285  
40 3     3   30 no warnings 'redefine';
  3         5  
  3         346  
41 1         7 *Mojolicious::Controller::url_for = $rel_url_for;
42             }
43             }
44              
45             1;
46             __END__