File Coverage

blib/lib/Mojolicious/Plugin/Tables.pm
Criterion Covered Total %
statement 86 98 87.7
branch 11 18 61.1
condition 12 22 54.5
subroutine 9 11 81.8
pod 1 1 100.0
total 119 150 79.3


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Tables;
2 3     3   246869 use Mojo::Base 'Mojolicious::Plugin';
  3         9  
  3         19  
3              
4 3     3   465 use File::Basename 'dirname';
  3         21  
  3         169  
5 3     3   20 use File::Spec::Functions 'catdir';
  3         7  
  3         146  
6              
7 3     3   1509 use Mojolicious::Plugin::Tables::Model;
  3         12  
  3         4585  
8              
9             our $VERSION = '1.05';
10              
11             sub register {
12 3     3 1 204 my ($self, $app, $conf) = @_;
13              
14 3         33 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         570 });
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         514 });
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   138454 my $c = shift;
30 45   100     146 my $shipped = $c->stash->{shipped} ||= {};
31 45 100       525 return $shipped unless @_;
32 41 100 66     230 return $shipped->{$_[0]} unless @_>1 || ref $_[0];
33 22 50       121 my $values = ref $_[0]? $_[0] : {@_};
34 22         141 @$shipped{keys %$values} = values %$values;
35 3         274 });
36              
37 3         265 $app->config(default_theme=>'redmond');
38 3   50     113 $app->defaults(layout => $conf->{layout} || 'tables');
39              
40 3   50     60 my $model_class = $conf->{model_class} ||= 'Mojolicious::Plugin::Tables::Model';
41 3 50       273 eval "require $model_class" or die;
42              
43 3         79 $model_class->log($log);
44 3         100 my $schema = $model_class->setup($conf);
45 3         65 my $model = $schema->model;
46 3         77 $app->config(model=>$model);
47              
48             $app->hook(before_dispatch => sub {
49 8     8   126022 my $c = shift;
50             # Move first part and slash from path to base path when deployed under a path
51 8 50       74 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         658 $c->shipped(urlbase => '');
57             }
58             # capture https into base
59 8 50 50     42 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         137 });
64            
65 3         309 my $plugin_resources = catdir dirname(__FILE__), 'Tables', 'resources';
66 3         12 push @{$app->routes->namespaces}, 'Mojolicious::Plugin::Tables::Controller';
  3         21  
67 3         43 push @{$app->renderer->paths}, catdir($plugin_resources, 'templates');
  3         19  
68 3         55 push @{$app->static->paths}, catdir($plugin_resources, 'public');
  3         20  
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         48 my $handlers = $app->renderer->handlers;
76 3         35 my $base_ep = $handlers->{ep};
77             $handlers->{ep} = sub {
78 12     12   22700 my ($renderer, $c, $output, $options) = @_;
79             {
80 12   100     29 my $table = $c->stash('table') || last;
  12         48  
81 8         146 my $custom_template = "$table/$options->{template}";
82 8   100     80 my $custom_path = $renderer->template_path ({%$options,
83             template=>$custom_template}) || last;
84 2         304 $options->{template} = $custom_template;
85 2 50       10 $log->debug("custom 'tables' template at $custom_path") if $ENV{TABLES_DEBUG};
86             }
87 12         1316 $base_ep->(@_)
88 3         25 };
89              
90 3         15 my $r = $app->routes;
91 3 50   2   64 $r->get('/' => sub{shift->redirect_to('tables')}) unless $conf->{nohome};
  2         11382  
92              
93 3         1214 my $crud_gets = [qw/view edit del nuke navigate/];
94 3         12 my $crud_posts = [qw/save/];
95              
96 3         24 for ($r->under('tables') ->to('auth#ok' )) {
97 3         1048 for ($_->under() ->to('tables#ok' )) {
98 3         642 $_->get ->to('#page' );
99 3         594 for ($_->under(':table') ->to('#table_ok' )) {
100 3         921 $_->any ->to('#table' );
101 3         673 $_->get('add') ->to('#add' );
102 3         884 $_->post('save') ->to('#save' );
103 3         814 for ($_->under(':id') ->to('#id_ok' )) {
104 3         788 $_->get(':action'=>[action=>$crud_gets] );
105 3         888 $_->post(':action'=>[action=>$crud_posts] );
106 3         780 $_->get('add_child/:child')->to('#view' );
107 3         997 $_->any(':children') ->to('#children' );
108             }
109             }
110             }
111             }
112              
113 3         854 my @tablist = @{$model->{tablist}};
  3         16  
114 3         32 $log->info ("'Tables' Framework enabled.");
115 3         45 $log->debug("Route namespaces are..");
116 3         25 $log->debug("--> $_") for @{$app->routes->namespaces};
  3         14  
117 3         131 $log->debug("Renderer paths are..");
118 3         22 $log->debug("--> $_") for @{$app->renderer->paths};
  3         82  
119 3         87 $log->debug("Static paths are..");
120 3         18 $log->debug("--> $_") for @{$app->static->paths};
  3         13  
121 3         80 $log->info ("/tables routes are in place. ".scalar(@tablist)." tables available");
122 3         38 $log->debug("--> $_") for @tablist;
123              
124 3 50       77 $log->debug($app->dumper($model)) if $ENV{TABLES_DEBUG};
125              
126             }
127              
128             1;
129             __END__