Showing 18 changed files with 72 additions and 64 deletions
+53 -35
mojo/lib/Mojolicious/Plugin/AutoRoute.pm
... ...
@@ -1,7 +1,7 @@
1 1
 package Mojolicious::Plugin::AutoRoute;
2 2
 use Mojo::Base 'Mojolicious::Plugin';
3 3
 
4
-our $VERSION = '0.09';
4
+our $VERSION = '0.12';
5 5
 
6 6
 sub register {
7 7
   my ($self, $app, $conf) = @_;
... ...
@@ -25,6 +25,8 @@ sub register {
25 25
     
26 26
     return if $path =~ /\.\./;
27 27
     
28
+    $path =~ s/\/+$//;
29
+    
28 30
     my $found;
29 31
     for my $dir (@{$c->app->renderer->paths}) {
30 32
       if (-f "$dir/$top_dir/$path.html.ep") {
... ...
@@ -38,7 +40,11 @@ sub register {
38 40
   # Index
39 41
   $r->route('/')
40 42
     ->over($condition_name)
41
-    ->to(cb => sub { shift->render("/$top_dir/index", 'mojo.maybe' => 1) });
43
+    ->to(cb => sub {
44
+      my $self = shift;
45
+      $self->render("/$top_dir/index", 'mojo.maybe' => 1);
46
+      $self->stash('mojo.finished') ? undef : $self->render_not_found;
47
+    });
42 48
   
43 49
   # Route
44 50
   $r->route('/(*__auto_route_plugin_path)')
... ...
@@ -47,18 +53,15 @@ sub register {
47 53
       my $c = shift;
48 54
       
49 55
       my $path = $c->stash('__auto_route_plugin_path');
56
+      $path =~ s/\/+$//;
50 57
       
51 58
       $c->render("/$top_dir/$path", 'mojo.maybe' => 1);
59
+      $c->stash('mojo.finished') ? undef : $c->render_not_found;
52 60
     });
53 61
   
54 62
   # Finish rendering Helper
55 63
   $app->helper(finish_rendering => sub {
56
-    my $self = shift;
57
-    
58
-    $self->stash->{'mojo.routed'} = 1;
59
-    $self->rendered;
60
-    
61
-    return $self;
64
+    warn "finish_rendering is DEPRECATED. no more needed";
62 65
   });
63 66
 }
64 67
 
... ...
@@ -70,7 +73,7 @@ Mojolicious::Plugin::AutoRoute - Mojolicious Plugin to create routes automatical
70 73
 
71 74
 =head1 CAUTION
72 75
 
73
-B<This is beta release. Implementation will be changed without warnings>. 
76
+B<This is beta release and very experimental. Implementation will be changed without warnings>.
74 77
 
75 78
 =head1 SYNOPSIS
76 79
 
... ...
@@ -96,6 +99,37 @@ Routes corresponding to URL is created .
96 99
 If you like C<PHP>, this plugin is very good.
97 100
 You only put file into C<auto> directory.
98 101
 
102
+=head1 EXAMPLE
103
+
104
+  use Mojolicious::Lite;
105
+  
106
+  # AutoRoute
107
+  plugin 'AutoRoute';
108
+  
109
+  # Custom routes
110
+  get '/create/:id' => template '/create';
111
+  
112
+  @@ auto/index.html.ep
113
+  /
114
+  
115
+  @@ auto/foo.html.ep
116
+  /foo
117
+  
118
+  @@ auto/bar.html.ep
119
+  /bar
120
+  
121
+  @@ auto/foo/bar/baz.html.ep
122
+  /foo/bar/baz
123
+  
124
+  @@ auto/json.html.ep
125
+  <%
126
+    $self->render(json => {foo => 1});
127
+    return;
128
+  %>
129
+  
130
+  @@ create.html.ep
131
+  /create/<%= $id %>
132
+
99 133
 =head1 OPTIONS
100 134
 
101 135
 =head2 route
... ...
@@ -112,37 +146,21 @@ Default is C<$app->routes>.
112 146
 
113 147
 Top directory. default is C<auto>.
114 148
 
115
-=head1 METHODS
149
+=head1 FUNCTIONS
116 150
 
117
-L<Mojolicious::Plugin::AutoRoute> inherits all methods from
118
-L<Mojolicious::Plugin> and implements the following new ones.
151
+=head2 template(Mojolicious::Plugin::AutoRoute::Util)
119 152
 
120
-=head1 HELPER
153
+If you want to create custom route, use C<template> function.
121 154
 
122
-=head2 finish_rendering
123
-
124
-You can render data, json, not found and exeption from template
125
-using C<finish_rendering> helper.
126
-
127
-  @@ index.html.ep
128
-  $self->render(data => 'foo');
129
-  $self->finish_rendering;
130
-  return;
131
-
132
-  @@ index.html.ep
133
-  $self->render(json => {foo => 1});
134
-  $self->finish_rendering;
135
-  return;
155
+  use Mojolicious::Plugin::AutoRoute::Util 'template';
156
+  
157
+  # Mojolicious Lite
158
+  any '/foo' => template 'foo';
136 159
 
137
-  @@ index.html.ep
138
-  $self->render_not_found;
139
-  $self->finish_rendering;
140
-  return;
160
+  # Mojolicious
161
+  $r->any('/foo' => template 'foo');
141 162
 
142
-  @@ index.html.ep
143
-  $self->render_exception;
144
-  $self->finish_rendering;
145
-  return;
163
+C<template> is return callback to call C<render_maybe>.
146 164
 
147 165
 =head2 register
148 166
 
+19
mojo/lib/Mojolicious/Plugin/AutoRoute/Util.pm
... ...
@@ -0,0 +1,19 @@
1
+package Mojolicious::Plugin::AutoRoute::Util;
2
+
3
+use strict;
4
+use warnings;
5
+use base 'Exporter';
6
+
7
+our @EXPORT_OK = ('template');
8
+
9
+sub template {
10
+  my $template = shift;
11
+  
12
+  return sub {
13
+    my $self = shift;
14
+    $self->render($template, 'mojo.maybe' => 1);
15
+    $self->stash('mojo.finished') ? undef : $self->render_not_found;
16
+  };
17
+}
18
+
19
+1;
-1
templates/api/revs.html.ep
... ...
@@ -23,6 +23,5 @@
23 23
       tag_names => \@tag_names
24 24
     }
25 25
   );
26
-  $self->finish_rendering;
27 26
   return;
28 27
 %>
-2
templates/archive.html.ep
... ...
@@ -29,7 +29,6 @@
29 29
   my $type = $git->object_type($user, $project, "$rev^{}");
30 30
   if (!$type || $type eq 'blob') {
31 31
     $self->render_not_found;
32
-    $self->finish_rendering;
33 32
     return;
34 33
   }
35 34
   
... ...
@@ -59,7 +58,6 @@
59 58
   
60 59
   unless ($success) {
61 60
     $self->render_exeption;
62
-    $self->finish_rendering;
63 61
     return;
64 62
   }
65 63
   
-1
templates/auto/_admin/users.html.ep
... ...
@@ -35,7 +35,6 @@
35 35
       else {
36 36
         $self->flash(message => "User $user is deleted.");
37 37
         $self->redirect_to('current');
38
-        $self->finish_rendering;
39 38
         return;
40 39
       }
41 40
     }
-2
templates/auto/_new.html.ep
... ...
@@ -7,7 +7,6 @@
7 7
   # Authentication
8 8
   unless ($api->logined) {
9 9
     $self->redirect_to('/');
10
-    $self->finish_rendering;
11 10
     return;
12 11
   }
13 12
   
... ...
@@ -67,7 +66,6 @@
67 66
         }
68 67
         else {
69 68
           $self->redirect_to("/$user/$project");
70
-          $self->finish_rendering;
71 69
           return;
72 70
         }
73 71
       }
-1
templates/auto/_start.html.ep
... ...
@@ -45,7 +45,6 @@
45 45
         # Redirect
46 46
         $self->flash(admin_user_created => 1);
47 47
         $self->redirect_to('/_login');
48
-        $self->finish_rendering;
49 48
         return;
50 49
       }
51 50
       else { $errors = $vresult->messages }
-1
templates/auto/index.html.ep
... ...
@@ -6,7 +6,6 @@
6 6
   # Goto Start page
7 7
   unless (defined $manager->admin_user) {
8 8
     $self->redirect_to('/_start');
9
-    $self->finish_rendering;
10 9
     return;
11 10
   }
12 11
 %>
-3
templates/auto/reset-password.html.ep
... ...
@@ -17,7 +17,6 @@
17 17
   }
18 18
   elsif (!$logined_admin && !$logined_user) {
19 19
     $self->redirect_to('/');
20
-    $self->finish_rendering;
21 20
     return;
22 21
   }
23 22
   
... ...
@@ -77,8 +76,6 @@
77 76
           session(password => $password_encrypted);
78 77
           $self->redirect_to($url);
79 78
         }
80
-        
81
-        $self->finish_rendering;
82 79
         return;
83 80
       }
84 81
       else { $errors = ["User $user don't exists"] }
-2
templates/branches.html.ep
... ...
@@ -18,7 +18,6 @@
18 18
     # Forbbiden
19 19
     unless ($api->logined($user)) {
20 20
       $self->redirect_to('/');
21
-      $self->finish_rendering;
22 21
       return;    
23 22
     }
24 23
 
... ...
@@ -46,7 +45,6 @@
46 45
       else {
47 46
         $self->flash(message => "Branch $branch is deleted.");
48 47
         $self->redirect_to;
49
-        $self->finish_rendering;
50 48
         return;
51 49
       }
52 50
     }
-1
templates/commit.html.ep
... ...
@@ -15,7 +15,6 @@
15 15
   my $commit = $git->get_commit($user, $project, $rev);
16 16
   unless ($commit) {
17 17
     $self->render_not_found;
18
-    $self->finish_rendering;
19 18
     return;
20 19
   }
21 20
   my $author_date
-1
templates/compare.html.ep
... ...
@@ -33,7 +33,6 @@
33 33
   
34 34
   if (!$start_commit || !$end_commit) {
35 35
     $self->render_not_found;
36
-    $self->finish_rendering;
37 36
     return;
38 37
   }
39 38
   
-3
templates/fork.html.ep
... ...
@@ -10,14 +10,12 @@
10 10
   # Can fork?
11 11
   unless ($api->logined) {
12 12
     $self->redirect_to('/');
13
-    $self->finish_rendering;
14 13
     return;
15 14
   }
16 15
   
17 16
   # Repository is already exists
18 17
   if (app->manager->exists_project($current_user, $project)) {
19 18
     $self->redirect_to("/$current_user/$project");
20
-    $self->finish_rendering;
21 19
     return;
22 20
   }
23 21
   # Fork
... ...
@@ -31,7 +29,6 @@
31 29
       flash(message => "Repository is forked from /$user/$project.");
32 30
       $self->redirect_to("/$current_user/$project");
33 31
     }
34
-    $self->finish_rendering;
35 32
     return;
36 33
   }
37 34
 %>
-1
templates/project.html.ep
... ...
@@ -14,7 +14,6 @@
14 14
   
15 15
   unless (app->manager->exists_project($user, $project)) {
16 16
     $self->render_not_found;
17
-    $self->finish_rendering;
18 17
     return;
19 18
   }
20 19
 
-2
templates/raw.html.ep
... ...
@@ -26,7 +26,5 @@
26 26
   $self->res->headers->content_disposition($content_disposition);
27 27
   $self->res->headers->content_type($type);
28 28
   $self->render(data => $blob_raw);
29
-  $self->finish_rendering;
30
-  
31 29
   return;
32 30
 %>
-5
templates/settings.html.ep
... ...
@@ -9,7 +9,6 @@
9 9
   # Authentication
10 10
   unless ($api->logined($user)) {
11 11
     $self->redirect_to('/');
12
-    $self->finish_rendering;
13 12
     return;
14 13
   }
15 14
   
... ...
@@ -47,7 +46,6 @@
47 46
         else {
48 47
           flash(message => "Repository name is renamed to $to_project");
49 48
           $self->redirect_to("/$user/$to_project/settings");
50
-          $self->finish_rendering;
51 49
           return;
52 50
         }
53 51
       }
... ...
@@ -68,7 +66,6 @@
68 66
     else {
69 67
       flash(message => 'Description is saved.');
70 68
       $self->redirect_to('current');
71
-      $self->finish_rendering;
72 69
       return;
73 70
     }
74 71
   }
... ...
@@ -84,7 +81,6 @@
84 81
     else {
85 82
       flash(message => "Default branch is changed to $default_branch.");
86 83
       $self->redirect_to('current');
87
-      $self->finish_rendering;
88 84
       return;
89 85
     }
90 86
   }
... ...
@@ -103,7 +99,6 @@
103 99
     else {
104 100
       flash(message => "Repository $project is deleted.");
105 101
       $self->redirect_to("/$user");
106
-      $self->finish_rendering;
107 102
       return;
108 103
     }
109 104
   }
-1
templates/user-settings.html.ep
... ...
@@ -9,7 +9,6 @@
9 9
   # Authentication
10 10
   unless ($api->logined($user)) {
11 11
     $self->redirect_to('/');
12
-    $self->finish_rendering;
13 12
     return;
14 13
   }
15 14
 %>
-2
templates/user.html.ep
... ...
@@ -3,7 +3,6 @@
3 3
   if (my $deleted_project = param('deleted_project')) {
4 4
     flash('deleted_project', $deleted_project);
5 5
     $self->redirect_to('current');
6
-    $self->finish_rendering;
7 6
     return;
8 7
   }
9 8
   
... ...
@@ -12,7 +11,6 @@
12 11
   # Projects
13 12
   unless (app->manager->exists_user($user)) {
14 13
     $self->render_not_found;
15
-    $self->finish_rendering;
16 14
     return;
17 15
   }
18 16
   my $projects = app->manager->projects($user);