FormHandler/Model/CDBI.pm

filter: Statementstmt 11.0% 16 / 145 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 57 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 28.5% 4 / 14 Podpod 90.0% 9 / 10 totaltotal 8.3% 29 / 348 CC 70 SCAR 52.9
CC
70
Cov
6%
update_model
420.0
lookup_options
240.0
init_value
182.0
CRAP 198.4
↑ Summary FormHandler/Field/Upload.pm »
1 package # hide from Pause
2 HTML::FormHandler::Model::CDBI;
3 # ABSTRACT: Class::DBI model class (non-functioning)
4  
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 extends 'HTML::FormHandler';
9  
10  
11 1 HTML::FormHandler::Model::CDBI->meta->make_immutable;
12  
13  
14 0 ▶sub init_item {
Subroutine init_item: not called CC=2 SCAR=17.9
Pod documented
15 0 my $self = shift;
16  
17 0 ▶ my $item_id = $self->item_id or return;
Branch: 1 branch
BranchTrueFalseTotal
my $item_id = $self->item_id or return 0 0 0
18 0 return $self->item_class->retrieve($item_id);
19 }
20  
21 0 ▶sub BUILDARGS {
Subroutine BUILDARGS: not called CC=1 SCAR=6.9
Pod documented
22 0 my ( $self, @args ) = @_;
23 0 return {@args};
24 }
25  
26  
27 0 ▶sub guess_field_type {
Subroutine guess_field_type: not called CC=10 SCAR=47.0
Pod documented
28 0 my ( $self, $column, $class ) = @_;
29  
30 0 ▶ $class ||= $self->item_class;
Condition0%0/3
$class ||= $self->item_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$class ||= $self->item_class
$class $self->item_class
Truth table0%0/3
A ||= B
A= $classB= $self->item_class
ABresult
1X1
011
000
31  
32 0 ▶ return unless $class && $class->isa('Class::DBI');
Branch: 1 branch
BranchTrueFalseTotal
unless $class and $class->isa("Class::DBI") 0 0 0
Condition0%0/3
$class and $class->isa("Class::DBI")
!ll&&!rl&&r
000
MC/DC0%0/2
$class and $class->isa("Class::DBI")
$class $class->isa("Class::DBI")
Truth table0%0/3
A and B
A= $classB= $class->isa("Class::DBI")
ABresult
0X0
100
111
33  
34 0 my @return;
35  
36 # Is it a direct has_a relationship?
37 0 ▶ if ( my $meta = $class->meta_info('has_a')->{$column} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $meta = $class->meta_info('has_a')->{$column}) { } 0 0 0
elsif ($meta = $class->meta_info('has_many')->{$column}) { } 0 0 0
elsif ($column =~ /_time$/) { } 0 0 0
38 0 my $f_class = $meta->foreign_class;
39  
40 0 ▶ @return =
Branch: 1 branch
BranchTrueFalseTotal
$f_class->isa('DateTime') ? : 0 0 0
41 $f_class->isa('DateTime') ? ('DateTime') :
42 ( 'Select', $f_class );
43  
44 # Otherwise, check for has_many
45 }
46 elsif ( $meta = $class->meta_info('has_many')->{$column} ) {
47  
48 0 my $f_class = $meta->foreign_class;
49 # Is there a mapping table in between? If so need to find the
50 # actual class for lookups -- call recursively
51 0 ▶ if ( @{ $meta->args->{mapping} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$meta->args->{"mapping"};}) 0 0 0
52 0 my $t;
53 0 ( $t, $f_class ) = $self->guess_field_type( $meta->args->{mapping}[0], $f_class );
54 }
55 0 @return = ( 'Multiple', $f_class );
56 }
57 elsif ( $column =~ /_time$/ ) {
58 0 @return = ('DateTime');
59 }
60 else {
61 0 @return = ('Text');
62 }
63  
64 0 ▶ return wantarray ? @return : $return[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
65 }
66  
67  
68 0 ▶sub lookup_options {
Subroutine lookup_options: not called CC=15 SCAR=54.8
Pod documented
69 0 my ( $self, $field ) = @_;
70  
71 0 ▶ my $class = $self->item_class or return;
Branch: 1 branch
BranchTrueFalseTotal
my $class = $self->item_class or return 0 0 0
72 0 ▶ return unless $class->isa('Class::DBI');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa("Class::DBI") 0 0 0
73 0 my $field_name = $field->name;
74 0 my ( $type, $f_class ) = $self->guess_field_type( $field_name, $class );
75 0 ▶ return unless $f_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $f_class 0 0 0
76  
77 # label column
78 0 my $label_column = $field->label_column;
79 0 ▶ return unless $f_class->find_column($label_column);
Branch: 1 branch
BranchTrueFalseTotal
unless $f_class->find_column($label_column) 0 0 0
80 # active column
81 0 ▶ my $active_col =
Branch: 1 branch
BranchTrueFalseTotal
$self->can('active_column') ? : 0 0 0
82 $self->can('active_column') ? $self->active_column :
83 $field->active_column;
84 0 ▶ $active_col = '' unless $f_class->find_column($active_col);
Branch: 1 branch
BranchTrueFalseTotal
unless $f_class->find_column($active_col) 0 0 0
85 # sort column
86 0 my $sort_col = $field->sort_column;
87 0 ▶ $sort_col =
Branch: 1 branch
BranchTrueFalseTotal
defined $sort_col && $f_class->find_column($sort_col) ? : 0 0 0
Condition0%0/3
defined $sort_col and $f_class->find_column($sort_col)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sort_col and $f_class->find_column($sort_col)
defined $sort_col $f_class->find_column($sort_col)
Truth table0%0/3
A and B
A= defined $sort_colB= $f_class->find_column($sort_col)
ABresult
0X0
100
111
88 defined $sort_col && $f_class->find_column($sort_col) ? $sort_col :
89 $label_column;
90  
91 0 my $criteria = {};
92 0 my $primary_key = $f_class->primary_column;
93 # In cases where the f_class is the same as the item's class don't
94 # include item in the option list -- don't want to be able to have item point to itself
95 # Obviously, this doesn't prevent circular references.
96 0 ▶ $criteria->{"$primary_key"} = { '!=', $self->item->id }
Branch: 1 branch
BranchTrueFalseTotal
if $f_class eq ref $self->item 0 0 0
97 if $f_class eq ref $self->item;
98  
99 # If there's an active column, only select active OR items already selected
100 0 ▶ if ($active_col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active_col) 0 0 0
101 0 my @or = ( $active_col => 1 );
102 # But also include any existing non-active
103 0 ▶ push @or, ( "$primary_key" => $field->init_value ) # init_value is scalar or array ref
Branch: 1 branch
BranchTrueFalseTotal
if $self->item and defined $field->init_value 0 0 0
Condition0%0/3
$self->item and defined $field->init_value
!ll&&!rl&&r
000
MC/DC0%0/2
$self->item and defined $field->init_value
$self->item defined $field->init_value
Truth table0%0/3
A and B
A= $self->itemB= defined $field->init_value
ABresult
0X0
100
111
104 if $self->item && defined $field->init_value;
105 0 $criteria->{'-or'} = \@or;
106 }
107  
108 0 my @rows = $f_class->search( $criteria, { order_by => $sort_col } );
109  
110 return [
111 map {
112 0 my $label = $_->$label_column;
113 0 ▶ $_->id, $active_col && !$_->$active_col ? "[ $label ]" : "$label"
Branch: 1 branch
BranchTrueFalseTotal
$active_col && !$_->$active_col ? : 0 0 0
Condition0%0/3
$active_col and not $_->$active_col
!ll&&!rl&&r
000
MC/DC0%0/2
$active_col and not $_->$active_col
$active_col not $_->$active_col
Truth table0%0/3
A and not(B)
A= $active_colB= not $_->$active_col
ABresult
0X0
100
111
114 } @rows
115 ];
116  
117 }
118  
119  
120 0 ▶sub init_value {
Subroutine init_value: not called CC=13 SCAR=52.0
Pod documented
121 0 my ( $self, $field, $item ) = @_;
122  
123 0 my $column = $field->name;
124  
125 0 ▶ $item ||= $self->item;
Condition0%0/3
$item ||= $self->item
l!l&&r!l&&!r
000
MC/DC0%0/2
$item ||= $self->item
$item $self->item
Truth table0%0/3
A ||= B
A= $itemB= $self->item
ABresult
1X1
011
000
126 0 ▶ return if $field->writeonly;
Branch: 1 branch
BranchTrueFalseTotal
if $field->writeonly 0 0 0
127 return
128 unless $item &&
129 ( $item->can($column) ||
130 0 ▶ ( ref $item eq 'HASH' && exists $item->{$column} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $item and $item->can($column) || ref $item eq "HASH" && exists $item->{$column} 0 0 0
Condition0%0/9
$item and $item->can($column) || ref $item eq "HASH" && exists $item->{$column}
!ll&&!rl&&r
000
$item->can($column) or ref $item eq "HASH" and exists $item->{$column}
l!l&&r!l&&!r
000
ref $item eq "HASH" and exists $item->{$column}
!ll&&!rl&&r
000
MC/DC0%0/4
$item and $item->can($column) || ref $item eq "HASH" && exists $item->{$column}
$item $item->can($column) ref $item eq "HASH" exists $item->{$column}
Truth table0%0/5
A and B or C and D
A= $itemB= $item->can($column)C= ref $item eq "HASH"D= exists $item->{$column}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
131 0 my @values;
132 0 ▶ if ( ref $item eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item eq 'HASH') { } 0 0 0
elsif (not $item->isa('Class::DBI')) { } 0 0 0
133 0 ▶ @values = $item->{$column} if ref($item) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $item eq "HASH" 0 0 0
134 }
135 elsif ( !$item->isa('Class::DBI') ) {
136 0 @values = $item->$column;
137 }
138 else {
139 @values =
140 0 ▶ map { ref $_ && $_->isa('Class::DBI') ? $_->id : $_ } $item->$column;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_->isa('Class::DBI') ? : 0 0 0
Condition0%0/3
ref $_ and $_->isa("Class::DBI")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ and $_->isa("Class::DBI")
ref $_ $_->isa("Class::DBI")
Truth table0%0/3
A and B
A= ref $_B= $_->isa("Class::DBI")
ABresult
0X0
100
111
141 }
142  
143 0 ▶ my $value = @values > 1 ? \@values : shift @values;
Branch: 1 branch
BranchTrueFalseTotal
@values > 1 ? : 0 0 0
144 0 $field->init_value($value);
145 0 $field->value($value);
146 }
147  
148  
149 0 ▶sub validate_model {
Subroutine validate_model: not called CC=2 SCAR=17.9
Pod documented
150 0 my ($self) = @_;
151  
152 0 ▶ return unless $self->validate_unique;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_unique 0 0 0
153 0 return 1;
154 }
155  
156  
157 0 ▶sub validate_unique {
Subroutine validate_unique: not called CC=9 SCAR=45.0
Pod documented
158 0 my ($self) = @_;
159  
160 0 my @unique = map { $_->name } grep { $_->unique } $self->fields;
161 0 ▶ return 1 unless @unique;
Branch: 1 branch
BranchTrueFalseTotal
unless @unique 0 0 0
162  
163 0 my $item = $self->item;
164  
165 0 ▶ my $class = ref($item) || $self->item_class;
Condition0%0/3
ref $item || $self->item_class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $item || $self->item_class
ref $item $self->item_class
Truth table0%0/3
A || B
A= ref $itemB= $self->item_class
ABresult
1X1
011
000
166 0 my $found_error = 0;
167 0 for my $field ( map { $self->field($_) } @unique ) {
168 0 ▶ next if $field->errors;
Branch: 1 branch
BranchTrueFalseTotal
if $field->errors 0 0 0
169 0 my $value = $field->value;
170 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
171 0 my $name = $field->name;
172 # unique means there can only be on in the database like it.
173 0 ▶ my $match = $class->search( { $name => $value } )->first || next;
Condition0%0/2
$class->search({$name, $value})->first || (next)
l!l
00
MC/DC0%0/1
$class->search({$name, $value})->first || (next)
$class->search({$name, $value})->first
Truth table0%0/2
A
A= $class->search({$name, $value})->first
Aresult
11
00
174 0 ▶ next if $self->items_same( $item, $match );
Branch: 1 branch
BranchTrueFalseTotal
if $self->items_same($item, $match) 0 0 0
175 0 ▶ my $field_error = $field->unique_message ||
Condition0%0/2
$field->unique_message || 'Value must be unique in the database'
l!l
00
MC/DC0%0/1
$field->unique_message || 'Value must be unique in the database'
$field->unique_message
Truth table0%0/2
A
A= $field->unique_message
Aresult
11
00
176 'Value must be unique in the database';
177 0 $field->add_error($field_error);
178 0 $found_error++;
179 }
180 0 return $found_error;
181 }
182  
183 0 ▶sub update_model {
Subroutine update_model: not called CC=20 SCAR=60.4
Pod undocumented
184 0 my ($self) = @_;
185  
186 # Grab either the item or the object class.
187 0 my $item = $self->item;
188 0 ▶ my $class = ref($item) || $self->item_class;
Condition0%0/3
ref $item || $self->item_class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $item || $self->item_class
ref $item $self->item_class
Truth table0%0/3
A || B
A= ref $itemB= $self->item_class
ABresult
1X1
011
000
189 0 my $updated_or_created;
190  
191 # get a hash of all fields
192 0 my %fields = map { $_->name, $_ } grep { !$_->noupdate } $self->fields;
193 # First process the normal and has_a columns
194 # as that data is directly stored in the object
195 0 my %data;
196 # Loads columns (including has_a)
197 0 foreach my $col ( $class->columns('All') ) {
198 0 ▶ next unless exists $fields{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $fields{$col} 0 0 0
199 0 my $field = delete $fields{$col};
200 # If the field is flagged "clear" then set to NULL.
201 0 my $value = $field->value;
202 0 ▶ if ($item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item) { } 0 0 0
203 0 my $cur = $item->$col;
204 0 ▶ next unless $value || $cur;
Branch: 1 branch
BranchTrueFalseTotal
unless $value or $cur 0 0 0
Condition0%0/3
$value or $cur
l!l&&r!l&&!r
000
MC/DC0%0/2
$value or $cur
$value $cur
Truth table0%0/3
A or B
A= $valueB= $cur
ABresult
1X1
011
000
205 0 ▶ next if $value && $cur && $value eq $cur;
Branch: 1 branch
BranchTrueFalseTotal
if $value and $cur and $value eq $cur 0 0 0
Condition0%0/6
$value and $cur and $value eq $cur
!ll&&!rl&&r
000
$value and $cur
!ll&&!rl&&r
000
MC/DC0%0/3
$value and $cur and $value eq $cur
$value $cur $value eq $cur
Truth table0%0/4
A and B and C
A= $valueB= $curC= $value eq $cur
ABCresult
0XX0
10X0
1100
1111
206 0 $item->$col($value);
207 }
208 else {
209 0 $data{$col} = $value;
210 }
211 }
212  
213 0 ▶ if ($item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item) { } 0 0 0
214 0 $item->update;
215 0 $updated_or_created = 'updated';
216 }
217 else {
218 0 $item = $class->create( \%data );
219 0 $self->item($item);
220 0 $updated_or_created = 'created';
221 }
222  
223 # Now check for mapping/has_many in any left over fields
224  
225 0 for my $field_name ( keys %fields ) {
226 0 ▶ next unless $class->meta_info('has_many');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->meta_info("has_many") 0 0 0
227 0 ▶ next unless my $meta = $class->meta_info('has_many')->{$field_name};
Branch: 1 branch
BranchTrueFalseTotal
unless my $meta = $class->meta_info("has_many")->{$field_name} 0 0 0
228  
229 0 my $field = delete $fields{$field_name};
230 0 my $value = $field->value;
231  
232 # Figure out which values to keep and which to add
233 0 my %keep;
234 0 ▶ %keep = map { $_ => 1 } ref $value ? @$value : ($value)
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
ref $value ? : 0 0 0
235 if defined $value;
236  
237 # Get foreign class and its key that points to $class
238 0 my $foreign_class = $meta->foreign_class;
239 0 my $foreign_key = $meta->args->{foreign_key};
240 0 my $related_key = $meta->args->{mapping}->[0];
241 0 ▶ die "Failed to find related_key for field [$field] in class [$class]"
Branch: 1 branch
BranchTrueFalseTotal
unless $related_key 0 0 0
242 unless $related_key;
243  
244 # Delete any items that are not to be kept
245 0 for ( $foreign_class->search( { $foreign_key => $item } ) ) {
246 0 ▶ $_->delete unless delete $keep{ $_->$related_key };
Branch: 1 branch
BranchTrueFalseTotal
unless delete $keep{$_->$related_key} 0 0 0
247 }
248  
249 # Add in new ones
250 $foreign_class->create(
251 {
252 $foreign_key => $item,
253 $related_key => $_,
254 }
255 0 ) for keys %keep;
256 }
257  
258 # Save item in form object
259 0 $self->item($item);
260 0 return $item;
261 }
262  
263  
264 0 ▶sub items_same {
Subroutine items_same: not called CC=5 SCAR=34.0
Pod documented
265 0 my ( $self, $item1, $item2 ) = @_;
266  
267 # returns true if both are undefined
268 0 ▶ return 1 if not defined $item1 and not defined $item2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item1 or defined $item2 0 0 0
Condition0%0/3
defined $item1 or defined $item2
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $item1 or defined $item2
defined $item1 defined $item2
Truth table0%0/3
A or B
A= defined $item1B= defined $item2
ABresult
1X1
011
000
269 # return false if either undefined
270 0 ▶ return unless defined $item1 and defined $item2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item1 and defined $item2 0 0 0
Condition0%0/3
defined $item1 and defined $item2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $item1 and defined $item2
defined $item1 defined $item2
Truth table0%0/3
A and B
A= defined $item1B= defined $item2
ABresult
0X0
100
111
271 0 return $self->obj_key($item1) eq $self->obj_key($item2);
272 }
273  
274  
275 0 ▶sub obj_key {
Subroutine obj_key: not called CC=2 SCAR=17.9
Pod documented
276 0 my ( $self, $item ) = @_;
277 return join '|', $item->table,
278 0 ▶ map { $_ . '=' . ( $item->$_ || '.' ) } $item->primary_columns;
Condition0%0/2
$item->$_ || '.'
l!l
00
MC/DC0%0/1
$item->$_ || '.'
$item->$_
Truth table0%0/2
A
A= $item->$_
Aresult
11
00
279 }
280  
281 1 __PACKAGE__->meta->make_immutable;
282 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
283 1 1;
284  
285 __END__
↑ Summary FormHandler/Field/Upload.pm »