File Coverage

blib/lib/RapidApp/Controller/DefaultRoot.pm
Criterion Covered Total %
statement 28 34 82.3
branch 1 4 25.0
condition 1 3 33.3
subroutine 11 12 91.6
pod 1 3 33.3
total 42 56 75.0


line stmt bran cond sub pod time code
1             package RapidApp::Controller::DefaultRoot;
2              
3 4     4   2226 use strict;
  4         12  
  4         105  
4 4     4   19 use warnings;
  4         9  
  4         88  
5              
6 4     4   21 use Moose;
  4         11  
  4         28  
7 4     4   23219 use namespace::autoclean;
  4         10  
  4         35  
8 4     4   307 BEGIN { extends 'Catalyst::Controller', 'RapidApp::ModuleDispatcher'; }
9              
10 4     4   24625 use RapidApp::Util qw(:all);
  4         8  
  4         2462  
11              
12             =head1 NAME
13              
14             RapidApp::Controller::DefaultRoot
15              
16             =head1 DESCRIPTION
17              
18             This controller is injected into the user's application unless they define a different controller
19             that extends ModuleDispatcher. This is merely the default, if they didn't specify one.
20              
21             =head1 CONFIGURATION
22              
23             This module (like any other RapidApp module) needs to have a list of its sub-modules.
24              
25             You can specify this using the Catalyst config system:
26              
27             MyApp::Controller::RapidApp::Root:
28             modules:
29             main: MyApp::Modules::Main
30              
31             along with any of the attributes for TopController.
32              
33             =head1 EXTENDING
34              
35             Rather than extending from this class (which would only save you a few lines), you can just
36             as easily write your own controller which inherits from TopController:
37              
38             package MyApp::Controller::Root;
39            
40             use Moose;
41             use namespace::autoclean;
42             BEGIN { extends 'Catalyst::Controller', 'RapidApp::ModuleDispatcher'; }
43            
44             __PACKAGE__->config( namespace => '' );
45            
46             sub approot :Path {
47             my ($self, $c, @args)= @_;
48             $self->dispatch($c, @args);
49             }
50            
51             sub end : ActionClass('RenderView') {}
52            
53             no Moose;
54             __PACKAGE__->meta->make_immutable;
55             1;
56              
57             The benefits you do gain from extending this default are the ability to plug in
58             other RapidApp features like Direct Links or Exception Logging. However, these
59             features could also be easily plugged into a custom top controller.
60              
61             =cut
62              
63             before 'COMPONENT' => sub {
64             my $class = shift;
65             my $app_class = ref $_[0] || $_[0];
66              
67             my $module_root_namespace = $app_class->module_root_namespace;
68             $class->config( namespace => $module_root_namespace || '' );
69             };
70              
71 0     0 1   sub process { (shift)->approot(@_) }
72              
73             # --- Update to GitHub PR #142 ---
74             # SWITCHED approot FROM :Chained BACK TO ORIGINAL :Path
75             #
76             # 'approot' is the special controller action which is the catch-all that hands
77             # off the dispatch into the Module controller hierarchy, which is a very non-standard
78             # setup in terms of the Catalyst dispatcher. The switch to :Chained in PR #142
79             # for approot results in it getting higher priority than locally defined :Path
80             # controllers - which we don't want - so for now we're switching just this action
81             # back to :Path. The rest of the controller actions that were switched to :Chained
82             # in PR #142 are working fine and will be kept as-is, since :Chained is still preferred.
83             # TODO: revisit this again to see if there is a better solution...
84             #
85             #sub approot :Chained :PathPrefix :Args {
86             sub approot :Path {
87 17     17 0 46068 my ($self, $c, @args) = @_;
88              
89             # Handle url string mode:
90 17 50 33     106 if(scalar(@args) == 1 && $args[0] =~ /^\//) {
91 0         0 my $url = $args[0];
92 0         0 $url =~ s/^\///; #<-- strip the leading '/' (needed for split below)
93            
94             # Automatically strip the namespace prefix if present:
95 0         0 my $ns = $self->config->{namespace};
96 0 0       0 $url =~ s/^${ns}\/?// if ($ns);
97            
98 0         0 @args = split(/\//,$url);
99             }
100            
101 17         174 $self->dispatch($c, @args);
102 4     4   31 }
  4         10  
  4         36  
103              
104 4     4 0 4400 sub end : ActionClass('RenderView') {}
  4     50   11  
  4         16  
105              
106 4     4   3256 no Moose;
  4         11  
  4         22  
107             __PACKAGE__->meta->make_immutable;
108             1;