Tables/Controller/Tables.pm

filter: Statementstmt 25.1% 54 / 215 Branchbran 13.1% 10 / 76 Conditioncond 14.8% 7 / 47 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 14 totaltotal 20.1% 79 / 392 CC 65 SCAR 51.9
CC
65
Cov
21%
table
380.0
save
306.0
navigate
132.0
CRAP 179.0
↑ Summary Tables.pm »
1 package Mojolicious::Plugin::Tables::Controller::Tables;
2  
3 3 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # 'shipped' is a special stash slot for passing serialisable things to json or templates.
6 # Other stash slots can be complex objects for use by templates.
7  
8 6 ▶sub ok {
Subroutine ok: called CC=6 SCAR=24.0
Pod undocumented
9 6 my $c = shift;
10 6 my $log = $c->app->log;
11 6 ▶ my $user_id = $c->stash('user_id') || die 'no user_id in stash';
Condition50%1/2
$c->stash('user_id') || die('no user_id in stash')
l!l
60
MC/DC0%0/1
$c->stash('user_id') || die('no user_id in stash')
$c->stash('user_id')
Truth table50%1/2
A
A= $c->stash('user_id')
Aresult
11
00
12 6 ▶ if ($c->allowed($user_id, 'tables')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->allowed($user_id, "tables")) 6 0 6
13 6 ▶ my $logourl = $c->app->config('logourl') || $c->url_for('/img/tables.png');
Condition33%1/3
$c->app->config('logourl') || $c->url_for('/img/tables.png')
l!l&&r!l&&!r
060
MC/DC0%0/2
$c->app->config('logourl') || $c->url_for('/img/tables.png')
$c->app->config('logourl') $c->url_for('/img/tables.png')
Truth table33%1/3
A || B
A= $c->app->config('logourl')B= $c->url_for('/img/tables.png')
ABresult
1X1
011
000
14 6 my $model = $c->app->config('model');
15 $c->shipped( tablist => $model->{tablist},
16 bytable => $model->{bytable},
17 6 logourl => $logourl );
18 6 $c->stash( schema=>$model->{schema} );
19 6 return 1;
20 }
21 0 my $error = "tables activity unauthorised";
22 0 ▶ $c->fail($error) if (($c->stash('format')||'x') eq 'json');
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash("format") || "x") eq "json" 0 0 0
Condition0%0/2
$c->stash('format') || 'x'
l!l
00
MC/DC0%0/1
$c->stash('format') || 'x'
$c->stash('format')
Truth table0%0/2
A
A= $c->stash('format')
Aresult
11
00
23 0 $c->add_flash(errors=>$error);
24 0 $c->redirect_to('/');
25 0 return;
26 }
27  
28 # more fine-grained permission control..
29 6 ▶sub allowed { 1 }
Subroutine allowed: called CC=1 SCAR=0.0
Pod undocumented
30  
31 2 ▶sub page {
Subroutine page: called CC=2 SCAR=7.2
Pod undocumented
32 2 my $c = shift;
33 2 ▶ $c->shipped(start_with=>$c->param('start_with')) if $c->param('start_with')
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("start_with") 0 2 2
34 }
35  
36 4 ▶sub table_ok {
Subroutine table_ok: called CC=3 SCAR=11.1
Pod undocumented
37 4 my $c = shift;
38 4 my $table = $c->stash('table');
39 4 my $schema = $c->stash('schema');
40 4 my $bytable= $c->shipped('bytable');
41 4 ▶ my $tinfo = $bytable->{$table} || die "no table $table";
Condition50%1/2
$bytable->{$table} || die("no table $table")
l!l
40
MC/DC0%0/1
$bytable->{$table} || die("no table $table")
$bytable->{$table}
Truth table50%1/2
A
A= $bytable->{$table}
Aresult
11
00
42 4 my $source = $tinfo->{source};
43 4 ▶ my $rs = $schema->resultset($source) || die "no source $source";
Condition50%1/2
$schema->resultset($source) || die("no source $source")
l!l
40
MC/DC0%0/1
$schema->resultset($source) || die("no source $source")
$schema->resultset($source)
Truth table50%1/2
A
A= $schema->resultset($source)
Aresult
11
00
44 4 $c->stash(rs=>$rs);
45 4 $c->shipped(table=>$table, tinfo=>$tinfo);
46 4 1;
47 }
48  
49 4 ▶sub id_ok {
Subroutine id_ok: called CC=2 SCAR=7.2
Pod undocumented
50 4 my $c = shift;
51 4 my $id = $c->stash('id');
52 4 my $rs = $c->stash('rs');
53 4 my @ids = split(/-\|-/, $id); # as made by see Row::compound_ids
54 4 ▶ if (my $row = $rs->find(@ids)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $rs->find(@ids)) 4 0 4
55 4 $c->stash(row => $row);
56 4 $c->shipped(id => $id);
57 4 return 1;
58 }
59 0 my $table = $c->stash('table');
60 0 die "no entry [$id] for table [$table]\n";
61 }
62  
63 0 ▶sub table {
Subroutine table: not called CC=19 SCAR=59.4
Pod undocumented
64 0 my $c = shift;
65 0 my $log = $c->app->log;
66 0 my $table = $c->stash('table');
67 0 my $rs = $c->stash('rs');
68 0 my $tinfo = $c->shipped('tinfo');
69  
70 0 ▶ if (($c->stash('format')||'html') eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if (($c->stash("format") || "html") eq "html") 0 0 0
Condition0%0/2
$c->stash('format') || 'html'
l!l
00
MC/DC0%0/1
$c->stash('format') || 'html'
$c->stash('format')
Truth table0%0/2
A
A= $c->stash('format')
Aresult
11
00
71 0 $c->redirect_to("/tables?start_with=$table");
72 0 return;
73 }
74  
75 0 my $columns = $tinfo->{columns};
76 0 my $bycolumn = $tinfo->{bycolumn};
77 0 my $has_a = $tinfo->{has_a};
78 0 my @has_a = map { $_->{parent} } values %$has_a;
79  
80 0 my @order_bys;
81 0 for (my $i=0; 1; $i++) {
82 0 ▶ my $col = $c->param("order[$i][column]") // last;
Condition0%0/2
$c->param("order[$i][column]") // (last)
l!l
00
MC/DC0%0/1
$c->param("order[$i][column]") // (last)
$c->param("order[$i][column]")
Truth table0%0/2
A
A= $c->param("order[$i][column]")
Aresult
11
00
83 0 ▶ my $dir = $c->param("order[$i][dir]" ) // last;
Condition0%0/2
$c->param("order[$i][dir]") // (last)
l!l
00
MC/DC0%0/1
$c->param("order[$i][dir]") // (last)
$c->param("order[$i][dir]")
Truth table0%0/2
A
A= $c->param("order[$i][dir]")
Aresult
11
00
84 0 push @order_bys, {col=>$col, dir=>$dir};
85 }
86 0 ▶ my $draw = $c->param('draw') || 0;
Condition0%0/2
$c->param('draw') || 0
l!l
00
MC/DC0%0/1
$c->param('draw') || 0
$c->param('draw')
Truth table0%0/2
A
A= $c->param('draw')
Aresult
11
00
87 0 ▶ my $start = $c->param('start') || 0;
Condition0%0/2
$c->param('start') || 0
l!l
00
MC/DC0%0/1
$c->param('start') || 0
$c->param('start')
Truth table0%0/2
A
A= $c->param('start')
Aresult
11
00
88 0 ▶ my $length = $c->param('length') || -1;
Condition0%0/2
$c->param('length') || -1
l!l
00
MC/DC0%0/1
$c->param('length') || -1
$c->param('length')
Truth table0%0/2
A
A= $c->param('length')
Aresult
11
00
89 0 my $search_v = $c->param('search[value]');
90 0 my $search_r = $c->param('search[regex]');
91  
92 # q1: count all candiates
93 0 my $recordsTotal = $rs->count;
94 0 my $recordsFiltered = $recordsTotal;
95
96 0 my $attrs = {};
97 0 my $where = [];
98  
99 # q2: count filtered candidates
100 0 ▶ if ($search_v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_v) 0 0 0
101 0 my $like_v = { ilike => "%$search_v%" };
102 0 for my $col (@$columns) {
103 0 my $info = $bycolumn->{$col};
104 0 ▶ next if $info->{fkey};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"fkey"} 0 0 0
105 0 ▶ next unless $info->{data_type} eq 'varchar';
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"data_type"} eq "varchar" 0 0 0
106 0 push @$where, {$col=>$like_v};
107 }
108 0 $recordsFiltered = $rs->search($where, $attrs)->count;
109 }
110  
111 # q3: full
112 0 ▶ $attrs->{order_by} = [ map { {"-$_->{dir}" => $columns->[$_->{col}] } } @order_bys ]
Branch: 1 branch
BranchTrueFalseTotal
if @order_bys 0 0 0
113 if @order_bys;
114 0 $attrs->{offset} = $start;
115 0 ▶ $attrs->{rows} = $length if $length>0;
Branch: 1 branch
BranchTrueFalseTotal
if $length > 0 0 0 0
116 #$log->debug("where " . $c->dumper($where));
117 #$log->debug("attrs " . $c->dumper($columns));
118 #$log->debug("odbys " . $c->dumper(\@order_bys));
119 #$log->debug("attrs " . $c->dumper($attrs));
120 0 my @rows = $rs->search($where, $attrs);
121  
122 my @data = map {
123 0 my $row = $_;
124 0 my $datum = {DT_RowId=>$row->compound_ids};
125 0 for (@$columns) {
126 0 my $info = $bycolumn->{$_};
127 0 ▶ if ($info->{fkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"fkey"}) 0 0 0
128 0 $datum->{$_} = undef;
129 0 ▶ if (my $frow = $row->$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frow = $row->$_) 0 0 0
130 0 $datum->{$_} = sprintf('<a href="#">%s</a>',$frow)
131 }
132 0 next;
133 }
134 0 $datum->{$_} = $row->present($_, $info);
135 }
136 $datum
137 0 } @rows;
138  
139 0 my $bundle = {
140 draw => $draw,
141 recordsTotal => $recordsTotal,
142 recordsFiltered => $recordsFiltered,
143 data => \@data,
144 };
145  
146 0 ▶ if (($c->stash('format')||'data') eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if (($c->stash("format") || "data") eq "json") 0 0 0
Condition0%0/2
$c->stash('format') || 'data'
l!l
00
MC/DC0%0/1
$c->stash('format') || 'data'
$c->stash('format')
Truth table0%0/2
A
A= $c->stash('format')
Aresult
11
00
147 0 $c->render( json => $bundle );
148 0 return;
149 }
150 0 $c->render(data => $c->dumper($bundle));
151 }
152  
153 0 ▶sub edit {}
Subroutine edit: not called CC=1 SCAR=0.0
Pod undocumented
154 3 ▶sub view {}
Subroutine view: called CC=1 SCAR=0.0
Pod undocumented
155 0 ▶sub add { shift->render(template=>'tables/edit', dml_mode=>'add', row=>undef) }
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
156 0 ▶sub del {
Subroutine del: not called CC=1 SCAR=6.9
Pod undocumented
157 0 my $c = shift;
158 0 $c->add_stash(messages => "Confirm.. all you see here will be removed");
159 0 $c->render(template=>'tables/view', dml_mode=>'del');
160 }
161  
162 0 ▶sub nuke {
Subroutine nuke: not called CC=2 SCAR=17.9
Pod undocumented
163 0 my $c = shift;
164 0 my $table= $c->stash('table');
165 0 my $row = $c->stash('row');
166 0 my $id = $c->shipped('id');
167 0 my $row0 = "$row";
168 0 my $hits = $row->nuke;
169 0 ▶ $c->add_flash(messages=>"REMOVED $row0 at key $id and all subordinate info.. $hits records in total") if $hits;
Branch: 1 branch
BranchTrueFalseTotal
if $hits 0 0 0
170 0 my $url = $c->url_for('/tables')->query(start_with=>$table);
171 0 $c->redirect_to($url);
172  
173 }
174  
175 0 ▶sub save {
Subroutine save: not called CC=17 SCAR=57.2
Pod undocumented
176 0 my $c = shift;
177 0 my $table = $c->stash('table');
178 0 my $rs = $c->stash('rs');
179 0 my $row = $c->stash('row');
180 0 my $schema = $c->stash('schema');
181 0 my $id = $c->shipped('id');
182 0 my $tinfo = $c->shipped('tinfo');
183 0 my $bytable= $c->shipped('bytable');
184  
185 0 my $columns = $tinfo->{columns};
186 0 my $bycolumn = $tinfo->{bycolumn};
187  
188 0 ▶ if ($row) { # update
Branch: 1 branch
BranchTrueFalseTotal
if ($row) { } 0 0 0
189 0 for my $col (@$columns) {
190 0 my $info = $bycolumn->{$col};
191 0 ▶ if (defined (my $val = $c->param($col))) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $val = $c->param($col))) { } 0 0 0
elsif ($c->param("${col}_pre_checkbox")) { } 0 0 0
192 0 ▶ $val = undef if $val eq '' && $info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" and $info->{"is_nullable"} 0 0 0
Condition0%0/3
$val eq "" and $info->{"is_nullable"}
!ll&&!rl&&r
000
MC/DC0%0/2
$val eq "" and $info->{"is_nullable"}
$val eq "" $info->{"is_nullable"}
Truth table0%0/3
A and B
A= $val eq ""B= $info->{"is_nullable"}
ABresult
0X0
100
111
193 0 $row->$col($val);
194 } elsif ($c->param("${col}_pre_checkbox")) {
195 0 ▶ my $val = $info->{is_nullable}? undef: 'f';
Branch: 1 branch
BranchTrueFalseTotal
$info->{'is_nullable'} ? : 0 0 0
196 0 $row->$col($val);
197 }
198 }
199 0 $row->update;
200 0 $c->add_flash(messages=>"updated: $row");
201  
202 } else { # insert
203 0 $row = $rs->new_result({});
204 0 for my $col (@$columns) {
205 0 my $info = $bycolumn->{$col};
206 0 ▶ if (defined (my $val = $c->param($col))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $val = $c->param($col))) 0 0 0
207 0 ▶ next if $val eq '' && $info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" and $info->{"is_nullable"} 0 0 0
Condition0%0/3
$val eq "" and $info->{"is_nullable"}
!ll&&!rl&&r
000
MC/DC0%0/2
$val eq "" and $info->{"is_nullable"}
$val eq "" $info->{"is_nullable"}
Truth table0%0/3
A and B
A= $val eq ""B= $info->{"is_nullable"}
ABresult
0X0
100
111
208 0 $row->$col($val);
209 }
210 }
211 0 ▶ if (eval { $row->insert }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $row->insert } }) { } 0 0 0
212 0 $row->discard_changes;
213 0 $id = $row->id;
214 0 $c->add_flash(messages=>"inserted at key $id: $row");
215 } else {
216 0 my $err = $@;
217 0 $c->app->log->error("inserting a $table: $err");
218 0 $c->add_stash(errors => "Database rejected the new record: $err");
219 # if came from an add-child..
220 0 ▶ if (my $psource = $c->param('psource') and
Branch: 1 branch
BranchTrueFalseTotal
if (my $psource = $c->param('psource') and my $child = $c->param('child') and my $parent_id = $c->param('parent_id')) { } 0 0 0
Condition0%0/6
my $psource = $c->param("psource") and my $child = $c->param("child") and my $parent_id = $c->param("parent_id")
!ll&&!rl&&r
000
my $psource = $c->param("psource") and my $child = $c->param("child")
!ll&&!rl&&r
000
MC/DC0%0/3
my $psource = $c->param("psource") and my $child = $c->param("child") and my $parent_id = $c->param("parent_id")
my $psource = $c->param("psource") my $child = $c->param("child") my $parent_id = $c->param("parent_id")
Truth table0%0/4
A and B and C
A= my $psource = $c->param("psource")B= my $child = $c->param("child")C= my $parent_id = $c->param("parent_id")
ABCresult
0XX0
10X0
1100
1111
221 my $child = $c->param('child') and
222 my $parent_id = $c->param('parent_id')) {
223 0 my $prs = $schema->resultset($psource);
224 0 my $prow = $prs->find($parent_id);
225 0 my $ptable = $prow->table;
226 0 my $pinfo = $bytable->{$ptable};
227 0 $c->shipped(id=>$parent_id, table=>$ptable, tinfo=>$pinfo);
228 0 $c->render(template=>'tables/view', row=>$prow, child=>$child);
229 # else a stand-alone add.
230 } else {
231 0 $c->render(template=>'tables/edit', dml_mode=>'add', row=>undef);
232 }
233 0 return;
234 }
235 }
236 0 ▶ my $redirect_to = $c->param('redirect_to') || "/tables/$table/$id/view";
Condition0%0/2
$c->param('redirect_to') || "/tables/$table/$id/view"
l!l
00
MC/DC0%0/1
$c->param('redirect_to') || "/tables/$table/$id/view"
$c->param('redirect_to')
Truth table0%0/2
A
A= $c->param('redirect_to')
Aresult
11
00
237 0 $c->redirect_to($redirect_to);
238 }
239  
240 1 ▶sub children {
Subroutine children: called CC=11 SCAR=27.0
Pod undocumented
241 1 my $c = shift;
242 1 my $tinfo = $c->shipped('tinfo');
243 1 my $has_many = $tinfo->{has_many};
244 1 my $children = $c->stash('children');
245 1 my $row = $c->stash('row');
246 1 ▶ my $offset = $c->param('offset') || 0;
Condition50%1/2
$c->param('offset') || 0
l!l
01
MC/DC0%0/1
$c->param('offset') || 0
$c->param('offset')
Truth table50%1/2
A
A= $c->param('offset')
Aresult
11
00
247 1 ▶ my $limit = $c->param('limit') || 10;
Condition50%1/2
$c->param('limit') || 10
l!l
01
MC/DC0%0/1
$c->param('limit') || 10
$c->param('limit')
Truth table50%1/2
A
A= $c->param('limit')
Aresult
11
00
248 1 ▶ die "$children: unknown has-many collection" unless $has_many->{$children};
Branch: 1 branch
BranchTrueFalseTotal
unless $has_many->{$children} 0 1 1
249 1 my $cpkey = $has_many->{$children}->{cpkey};
250 1 my $dir = 'asc';
251 1 my $attrs = {};
252 1 ▶ if ($offset > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($offset > 0) { } 0 1 1
elsif ($offset == -1) { } 0 1 1
elsif ($offset == -2) { } 0 1 1
253 0 $attrs->{offset} = $offset;
254 } elsif ($offset == -1) { # get last page
255 0 $dir = 'desc';
256 } elsif ($offset == -2) { # get all.. in fact, apply safety valve of 1000
257 0 $limit = 1000;
258 }
259 1 $attrs->{order_by} = {"-$dir"=>$cpkey};
260 1 ▶ $attrs->{rows} = $limit if $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit 1 0 1
261 #$c->app->log->debug("invoke child group $children with " . $c->dumper($attrs));
262 1 my @rows = map { +{id=>$_->id, label=>"$_"} } $row->$children({}, $attrs);
263 1 ▶ @rows = reverse @rows if $dir eq 'desc';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "desc" 0 1 1
264 1 ▶ if (($c->stash('format')||'html') eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if (($c->stash("format") || "html") eq "json") 1 0 1
Condition50%1/2
$c->stash('format') || 'html'
l!l
10
MC/DC0%0/1
$c->stash('format') || 'html'
$c->stash('format')
Truth table50%1/2
A
A= $c->stash('format')
Aresult
11
00
265 1 $c->render( json => \@rows );
266 1 return;
267 }
268 0 $c->render(data => $c->dumper(\@rows));
269 }
270  
271 0 ▶sub navigate {
Subroutine navigate: not called CC=11 SCAR=48.8
Pod undocumented
272 0 my $c = shift;
273 0 my $log = $c->app->log;
274 0 ▶ my $to = $c->param('to') || 'next';
Condition0%0/2
$c->param('to') || 'next'
l!l
00
MC/DC0%0/1
$c->param('to') || 'next'
$c->param('to')
Truth table0%0/2
A
A= $c->param('to')
Aresult
11
00
275 0 my $table = $c->stash('table');
276 0 my $rs = $c->stash('rs');
277 0 my $row = $c->stash('row');
278 0 my $tinfo = $c->shipped('tinfo');
279 0 my $id = $c->shipped('id');
280 0 my $pkeys = $tinfo->{pkeys};
281 0 ▶ die "cannot navigate to $to" unless $to =~ /start|end|next|prev/;
Branch: 1 branch
BranchTrueFalseTotal
unless $to =~ /start|end|next|prev/u 0 0 0
282 0 ▶ die "not supported for multi-barrel pkeys" if @$pkeys > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$pkeys > 1 0 0 0
283 0 my $pkey = $pkeys->[0];
284 0 my $attrs = { rows=>1, select=>[$pkey] };
285 0 my $rhs;
286 0 for ($to) {
287 0 ▶ if (/next/ ) { $attrs->{order_by} = {-asc =>$pkey }; $rhs = {'>'=>$id}; last }
Branch: 1 branch
BranchTrueFalseTotal
if (/next/u) 0 0 0
288 0 ▶ if (/prev/ ) { $attrs->{order_by} = {-desc=>$pkey }; $rhs = {'<'=>$id}; last }
Branch: 1 branch
BranchTrueFalseTotal
if (/prev/u) 0 0 0
289 0 ▶ if (/start/) { $attrs->{order_by} = {-asc =>$pkey }; last }
Branch: 1 branch
BranchTrueFalseTotal
if (/start/u) 0 0 0
290 0 ▶ if (/end/ ) { $attrs->{order_by} = {-desc=>$pkey }; last }
Branch: 1 branch
BranchTrueFalseTotal
if (/end/u) 0 0 0
291 }
292 0 my $newid = $id;
293 0 my $where = {};
294 0 ▶ $where->{$pkey} = $rhs if $rhs;
Branch: 1 branch
BranchTrueFalseTotal
if $rhs 0 0 0
295 0 ▶ if (my $hit = $rs->search($where, $attrs)->first) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hit = $rs->search($where, $attrs)->first) 0 0 0
296 0 $newid = $hit->id;
297 }
298 0 $c->redirect_to("/tables/$table/$newid/view");
299 }
300  
301 3 1;
302  
↑ Summary Tables.pm »