File Coverage

blib/lib/Mojolicious/Plugin/Tables/Model/Row.pm
Criterion Covered Total %
statement 34 61 55.7
branch 9 18 50.0
condition 3 7 42.8
subroutine 10 14 71.4
pod 0 7 0.0
total 56 107 52.3


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Tables::Model::Row;
2              
3 3     3   170241 use strict;
  3         9  
  3         106  
4 3     3   18 use warnings;
  3         7  
  3         96  
5              
6 3     3   16 use base 'DBIx::Class::Core';
  3         8  
  3         1503  
7              
8             __PACKAGE__-> load_components('InflateColumn::DateTime');
9              
10             use overload
11 12     12   13673 '""' => sub { shift->stringify_safely },
12 8     8   31384 'bool' => sub { 1 },
13 3     3   264121 fallback => 1;
  3         9  
  3         30  
14              
15             # errors during stringification of a db object can sometimes trigger
16             # deep recursion by well-meaning error messages deep in ORM internals.
17              
18             sub stringify_safely {
19 12     12 0 33 my $self = shift;
20 12   33     34 return eval { $self->stringify } // do {
  12         54  
21 0         0 my $err = $@;
22 0         0 my $class = ref $self;
23 0         0 my $fallback = "[*$class]".$self->id;
24 0         0 $self->log->error("stringifying $fallback: $err");
25 0         0 $fallback
26             }
27             }
28              
29             # override this for each ResultClass.
30              
31             sub stringify {
32 12     12 0 26 my $self = shift;
33 12 50       272 my $cd = $self->can('cd')
34             ? (':'.$self->cd)
35             : $self->compound_ids;
36 12 100       370 my $ds = $self->can('description')? $self->description:
    50          
37             $self->can('name' )? $self->name:
38             '';
39 12         93 my $label = $self->result_source->source_name;
40 12 100       812 sprintf '[%s] %s%s', $label, $cd, ($ds? " - $ds":'')
41             }
42              
43 0     0 0 0 sub log { shift->result_source->schema->log }
44              
45             sub compound_ids {
46 16     16 0 41833 my $self = shift;
47 16         428 join ('-|-', map { $self->get_column($_) } $self->primary_columns);
  16         3615  
48             }
49              
50             sub present {
51 4     4 0 3710 my ($self, $column, $info, %opts) = @_;
52 4         11 my $class = ref $self;
53             #$self->log->debug("present $column for $class using " . Dumper($info));
54 4   50     23 my $type = $info->{data_type} || 'varchar';
55              
56 4   50     136 my $val = $self->$column // return;
57              
58 4         80 for ($type) {
59 4 0       18 /timestamp/ && return ($opts{foredit}? $val->iso8601: $val->strftime('%F %T'));
    50          
60 4 50       18 /date/ && return ($val->ymd);
61 4 0       19 /boolean/ && return ($val?'Yes':'No');
    50          
62             }
63 4         22 return $val
64             }
65              
66             # generate the full pick-list that lets the fk $column pick from its parent,
67             # in a structure suitable for the 'select_field' tag. This version gets all
68             # (limited by safety check) but inherited versions are expected to do more
69             # context-sensitive filtering. Will work as a class method.
70             sub options {
71             #my ($self, $column, $cinfo, $pinfo, $schema, $bytable) = @_;
72 0     0 0   my ($xxx1, $xxxxx2, $xxxx3, $pinfo, $schema, $bytable) = @_;
73 0           my $ptable = $pinfo->{ptable};
74 0           my $ptabinfo = $bytable->{$ptable};
75 0           my $psource = $ptabinfo->{source};
76 0           my $prs = $schema->resultset($psource);
77 0           my $where = {};
78 0           my $attrs = {rows=>200};
79             my @options = map {
80 0           [ "$_" => $_->id ]
  0            
81             } $prs->search($where, $attrs);
82 0           return \@options;
83             }
84              
85             sub nuke {
86 0     0 0   my $self = shift;
87 0           my $s = $self->result_source;
88 0           my $schema = $s->schema;
89              
90 0           my @collections = grep { $s->relationship_info($_)->{attrs}{accessor} eq 'multi' }
  0            
91             $s->relationships;
92              
93 0           my $i = 0;
94             $schema->txn_do( sub {
95 0     0     for my $collection (@collections) {
96 0           $i += $_->nuke for $self->$collection->all;
97             }
98 0           $self->delete;
99 0           ++$i
100             } )
101 0           }
102              
103             1;
104