File Coverage

blib/lib/Mojolicious/Plugin/Tables.pm
Criterion Covered Total %
statement 88 100 88.0
branch 11 18 61.1
condition 12 22 54.5
subroutine 9 11 81.8
pod 1 1 100.0
total 121 152 79.6


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Tables;
2 3     3   300239 use Mojo::Base 'Mojolicious::Plugin';
  3         10  
  3         18  
3              
4 3     3   488 use File::Basename 'dirname';
  3         22  
  3         159  
5 3     3   19 use File::Spec::Functions 'catdir';
  3         6  
  3         129  
6              
7 3     3   1566 use Mojolicious::Plugin::Tables::Model;
  3         13  
  3         4840  
8              
9             our $VERSION = '1.06';
10              
11             sub register {
12 3     3 1 216 my ($self, $app, $conf) = @_;
13              
14 3         34 my $log = $app->log;
15              
16             $app->helper(add_stash => sub {
17 0     0   0 my ($c, $slot, $val) = @_;
18 0   0     0 push @{$c->stash->{$slot} ||= []}, $val;
  0         0  
19 3         254 });
20              
21             $app->helper(add_flash => sub {
22 0     0   0 my ($c, $slot, $val) = @_;
23 0   0     0 push @{$c->session->{new_flash}{$slot} ||= []}, $val;
  0         0  
24 3         493 });
25              
26             $app->helper(shipped => sub {
27             # special stash slot for sending json-able structs to js;
28             # get/set logic here cloned from Mojo::Util::_stash.
29 45     45   144045 my $c = shift;
30 45   100     166 my $shipped = $c->stash->{shipped} ||= {};
31 45 100       580 return $shipped unless @_;
32 41 100 66     267 return $shipped->{$_[0]} unless @_>1 || ref $_[0];
33 22 50       136 my $values = ref $_[0]? $_[0] : {@_};
34 22         164 @$shipped{keys %$values} = values %$values;
35 3         287 });
36              
37 3         277 $app->config(default_theme=>'redmond');
38 3   50     114 $app->defaults(layout => $conf->{layout} || 'tables');
39              
40 3   50     60 my $model_class = $conf->{model_class} ||= 'Mojolicious::Plugin::Tables::Model';
41 3 50       231 eval "require $model_class" or die;
42              
43 3         87 $model_class->log($log);
44 3         90 my $schema = $model_class->setup($conf);
45 3         60 my $model = $schema->model;
46 3         69 $app->config(model=>$model);
47              
48             $app->hook(before_dispatch => sub {
49 8     8   133324 my $c = shift;
50             # Move first part and slash from path to base path when deployed under a path
51 8 50       61 if ($c->req->headers->to_string =~ /X-Forwarded/) {
52 0         0 my $part0 = shift @{$c->req->url->path->leading_slash(0)};
  0         0  
53 0         0 push @{$c->req->url->base->path->trailing_slash(0)}, $part0;
  0         0  
54 0         0 $c->shipped(urlbase => $c->req->url->base);
55             } else {
56 8         670 $c->shipped(urlbase => '');
57             }
58             # capture https into base
59 8 50 50     43 if ($c->req->headers->header('X-Forwarded-HTTPS')
      33        
60             || ($c->req->headers->header('X-Forwarded-Proto')||'') eq 'https') {
61 0         0 $c->req->url->base->scheme('https')
62             }
63 3         132 });
64            
65 3         308 my $plugin_resources = catdir dirname(__FILE__), 'Tables', 'resources';
66 3         12 push @{$app->routes->namespaces}, 'Mojolicious::Plugin::Tables::Controller';
  3         23  
67 3         46 push @{$app->renderer->paths}, catdir($plugin_resources, 'templates');
  3         20  
68 3         68 push @{$app->static->paths}, catdir($plugin_resources, 'public');
  3         22  
69              
70             # Arrange for custom table-specific template overrides, when present.
71             # We do this by wrapping the standard ep-handler with a
72             # pre-processor to test for existence of the custom template.
73             # Any internal caching by EP renderer is preserved even for overrides.
74              
75 3         50 my $handlers = $app->renderer->handlers;
76 3         33 my $base_ep = $handlers->{ep};
77             $handlers->{ep} = sub {
78 12     12   23561 my ($renderer, $c, $output, $options) = @_;
79             {
80 12   100     32 my $table = $c->stash('table') || last;
  12         66  
81 8         143 my $custom_template = "$table/$options->{template}";
82 8   100     83 my $custom_path = $renderer->template_path ({%$options,
83             template=>$custom_template}) || last;
84 2         299 $options->{template} = $custom_template;
85 2 50       11 $log->debug("custom 'tables' template at $custom_path") if $ENV{TABLES_DEBUG};
86             }
87 12         1492 $base_ep->(@_)
88 3         26 };
89              
90 3         13 my $r = $app->routes;
91 3 50   2   72 $r->get('/' => sub{shift->redirect_to('tables')}) unless $conf->{nohome};
  2         11378  
92              
93 3         1263 my @crud_gets = (qw/view edit del nuke navigate/);
94 3         12 my @crud_posts = (qw/save/);
95 3         13 my $fmts = [format=>[qw/html json/]];
96              
97 3         26 for ($r->under('tables') ->to('auth#ok' )) {
98 3         1070 for ($_->under() ->to('tables#ok' )) {
99 3         632 $_->get ->to('#page' );
100 3         621 for ($_->under(':table') ->to('#table_ok' )) {
101 3         1096 $_->any('/'=>$fmts) ->to('#table', format=>'html' );
102 3         721 $_->get('add') ->to('#add' );
103 3         1023 $_->post('save') ->to('#save' );
104 3         903 for ($_->under(':id') ->to('#id_ok' )) {
105 3         935 my $r = $_;
106 3         19 $r->get( $_=>$fmts) ->to("#$_", format=>'html') for @crud_gets;
107 3         4829 $r->post($_=>$fmts) ->to("#$_", format=>'html') for @crud_posts;
108 3         937 $r->get('add_child/:child'=>$fmts)->to('#view', format=>'html' );
109 3         1190 $r->any(':children'=>$fmts) ->to('#children', format=>'html' );
110             }
111             }
112             }
113             }
114              
115 3         1057 my @tablist = @{$model->{tablist}};
  3         21  
116 3         29 $log->info ("'Tables' Framework enabled.");
117 3         53 $log->debug("Route namespaces are..");
118 3         28 $log->debug("--> $_") for @{$app->routes->namespaces};
  3         18  
119 3         183 $log->debug("Renderer paths are..");
120 3         31 $log->debug("--> $_") for @{$app->renderer->paths};
  3         18  
121 3         79 $log->debug("Static paths are..");
122 3         20 $log->debug("--> $_") for @{$app->static->paths};
  3         15  
123 3         85 $log->info ("/tables routes are in place. ".scalar(@tablist)." tables available");
124 3         29 $log->debug("--> $_") for @tablist;
125              
126 3 50       90 $log->debug($app->dumper($model)) if $ENV{TABLES_DEBUG};
127              
128             }
129              
130             1;
131             __END__