Model/DBIC.pm

filter: Statementstmt 95.8% 486 / 507 Branchbran 79.5% 256 / 322 Conditioncond 74.2% 150 / 202 MC/DCmcdc 49.0% 54 / 110 Subroutinesub 100% 34 / 34 Podpod 80.0% 4 / 5 totaltotal 83.3% 984 / 1180 CC 252 SCAR 29.9
CC
252
Cov
87%
_save_columns
35.8
_fill_nested
28.6
_save_relationships
27.9
CRAP 19.8
↑ Summary Constraint/DBIC/Unique.pm »
1 package HTML::FormFu::Model::DBIC;
2  
3 90 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 90 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 90 ▶use base 'HTML::FormFu::Model';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 90 our $VERSION = '2.02'; # VERSION
8  
9 90 ▶use HTML::FormFu::Util qw( _merge_hashes );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 90 ▶use List::MoreUtils qw( none notall );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 90 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 90 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 90 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 90 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub options_from_model {
17 25 ▶ my ( $self, $base, $attrs ) = @_;
Subroutine options_from_model: called CC=16 SCAR=28.4
Pod documented
18  
19 25 my $form = $base->form;
20 25 my $resultset = _get_resultset( $base, $form, $attrs );
21 25 my $source = $resultset->result_source;
22  
23 25 my $id_col = $attrs->{id_column};
24 25 my $label_col = $attrs->{label_column};
25 25 my $condition = $attrs->{condition};
26 25 ▶ my $attributes = $attrs->{attributes} || {};
Condition50%1/2
$attrs->{'attributes'} || {}
l!l
025
MC/DC0%0/1
$attrs->{'attributes'} || {}
$attrs->{'attributes'}
Truth table50%1/2
A
A= $attrs->{'attributes'}
Aresult
11
00
27  
28 my $enum_col =
29 first {
30 1 ▶ lc( $base->name ) eq lc($_);
Subroutine __ANON__: called CC=1 SCAR=0.0
31 }
32 grep {
33 25 my $data_type = $source->column_info($_)->{data_type};
34 87 ▶ defined $data_type && $data_type =~ /enum/i
Branch: 1 branch
BranchTrueFalseTotal
defined $data_type and $data_type =~ /enum/i 87 0 87
35 } $source->columns;
36  
37 25 ▶ if ( defined $enum_col ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $enum_col) 1 24 25
38 return map {
39 3 [ $_, $_ ]
40 1 } @{ $source->column_info($enum_col)->{extra}{list} };
41 }
42  
43 24 ▶ if ( !defined $id_col ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id_col) 24 0 24
44 24 ($id_col) = $source->primary_columns;
45 }
46  
47 24 ▶ if ( !defined $label_col ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $label_col) 24 0 24
48  
49 # use first text column
50 ($label_col) = grep {
51 24 my $data_type = $source->column_info($_)->{data_type};
52 73 ▶ defined $data_type && $data_type =~ /text|varchar/i
Branch: 1 branch
BranchTrueFalseTotal
defined $data_type and $data_type =~ /text|varchar/i 73 0 73
53 } $source->columns;
54 }
55 24 ▶ $label_col = $id_col if !defined $label_col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label_col 0 24 24
56  
57 24 ▶ if ( defined( my $from_stash = $attrs->{condition_from_stash} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $from_stash = $attrs->{"condition_from_stash"})) 6 18 24
58 $condition
59 = $condition
60 6 ▶ ? { %{$condition} }
Branch: 1 branch
BranchTrueFalseTotal
$condition ? : 0 6 6
61 : {}; # avoid overwriting attrs->{condition}
62 6 for my $name ( keys %$from_stash ) {
63 6 ▶ croak "config value must not be a reference" if ref $from_stash->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if ref $from_stash->{$name} 0 6 6
64 6 my $value = $form->stash->{ $from_stash->{$name} };
65 6 $condition->{$name} = $value;
66 }
67 }
68  
69 # save the expanded condition for later use
70 24 ▶ $attrs->{'-condition'} = $condition if ($condition);
Branch: 1 branch
BranchTrueFalseTotal
if $condition 8 16 24
71  
72 24 $attributes->{'-columns'} = [ $id_col, $label_col ];
73  
74 24 my $result = $resultset->search( $condition, $attributes );
75  
76 24 my @defaults = $result->all;
77 24 my $has_column = $source->has_column($label_col);
78  
79 24 ▶ if ( $attrs->{localize_label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{'localize_label'}) { } 1 23 24
80 @defaults = map {
81 1 ▶ { value => $_->get_column($id_col),
Branch: 1 branch
BranchTrueFalseTotal
$has_column ? : 2 0 2
82 label_loc => $has_column ? $_->get_column($label_col) : $_->$label_col,
83 }
84 } @defaults;
85 }
86 else {
87 23 @defaults = map { [
88 72 ▶ $_->get_column($id_col),
Branch: 1 branch
BranchTrueFalseTotal
$has_column ? : 72 0 72
89 $has_column ? $_->get_column($label_col) : $_->$label_col,
90 ]
91 } @defaults;
92 }
93  
94 24 return @defaults;
95 }
96  
97 sub _get_resultset {
98 25 ▶ my ( $base, $form, $attrs ) = @_;
Subroutine _get_resultset: called CC=8 SCAR=26.2
99  
100 25 my $schema = $form->stash->{schema};
101 25 my $context = $form->stash->{context};
102  
103 25 ▶ if ( defined $schema ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $schema) { } 22 3 25
elsif (defined $context and defined $attrs->{'model'}) { } 3 0 3
elsif (defined $context) { } 0 0 0
104 22 ▶ my $rs_name = $attrs->{resultset} || ucfirst $base->name;
Condition66%2/3
$attrs->{'resultset'} || ucfirst $base->name
l!l&&r!l&&!r
1840
MC/DC0%0/2
$attrs->{'resultset'} || ucfirst $base->name
$attrs->{'resultset'} ucfirst $base->name
Truth table66%2/3
A || B
A= $attrs->{'resultset'}B= ucfirst $base->name
ABresult
1X1
011
000
105  
106 22 return $schema->resultset($rs_name);
107 }
108 ▶ elsif ( defined $context && defined $attrs->{model} ) {
Condition33%1/3
defined $context and defined $attrs->{"model"}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $context and defined $attrs->{"model"}
defined $context defined $attrs->{"model"}
Truth table33%1/3
A and B
A= defined $contextB= defined $attrs->{"model"}
ABresult
0X0
100
111
109  
110 3 my $model = $context->model( $attrs->{model} );
111  
112 3 ▶ if ( defined( my $rs = $attrs->{resultset} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rs = $attrs->{"resultset"})) 0 3 3
113 0 $model = $model->resultset($rs);
114 }
115  
116 3 return $model;
117 }
118 elsif ( defined $context ) {
119 0 my $model = $context->model;
120  
121 0 ▶ return $model if defined $model;
Branch: 1 branch
BranchTrueFalseTotal
if defined $model 0 0 0
122 }
123  
124 0 croak "need a schema or context";
125 }
126  
127 sub default_values {
128 65 ▶ my ( $self, $dbic, $attrs ) = @_;
Subroutine default_values: called CC=5 SCAR=16.4
Pod documented
129  
130 65 my $form = $self->form;
131 65 ▶ my $base = defined $attrs->{base} ? delete $attrs->{base} : $form;
Branch: 1 branch
BranchTrueFalseTotal
defined $attrs->{'base'} ? : 32 33 65
132  
133 $base = $form->get_all_element( { nested_name => $attrs->{nested_base} } )
134 if defined $attrs->{nested_base}
135 && ( !defined $base->nested_name
136 65 ▶ || $base->nested_name ne $attrs->{nested_base} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"nested_base"} and !defined($base->nested_name) || $base->nested_name ne $attrs->{"nested_base"} 6 59 65
Condition50%3/6
defined $attrs->{"nested_base"} and !defined($base->nested_name) || $base->nested_name ne $attrs->{"nested_base"}
!ll&&!rl&&r
5960
not defined $base->nested_name or $base->nested_name ne $attrs->{"nested_base"}
l!l&&r!l&&!r
600
MC/DC0%0/3
defined $attrs->{"nested_base"} and !defined($base->nested_name) || $base->nested_name ne $attrs->{"nested_base"}
defined $attrs->{"nested_base"} not defined $base->nested_name $base->nested_name ne $attrs->{"nested_base"}
Truth table25%1/4
A and not(B) or C
A= defined $attrs->{"nested_base"}B= not defined $base->nested_nameC= $base->nested_name ne $attrs->{"nested_base"}
ABCresult
0XX0
1000
11X1
1011
137  
138 65 _fill_in_fields( $base, $dbic );
139 65 _fill_nested( $self, $base, $dbic );
140  
141 65 return $form;
142 }
143  
144 # returns 0 if there is a node with nested_name set on the path from $field to $base
145 sub is_direct_child {
146 747 ▶ my ( $base, $field ) = @_;
Subroutine is_direct_child: called CC=4 SCAR=13.9
Pod undocumented
147  
148 747 ▶ while ( defined $field->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->parent) 1054 2 1056
149 1054 $field = $field->parent;
150  
151 1054 ▶ return 1 if $base == $field;
Branch: 1 branch
BranchTrueFalseTotal
if $base == $field 556 498 1054
152 498 ▶ return 0 if defined $field->nested_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $field->nested_name 189 309 498
153 }
154 }
155  
156 # fills in values for all direct children fields of $base
157 sub _fill_in_fields {
158 65 ▶ my ( $base, $dbic ) = @_;
Subroutine _fill_in_fields: called CC=20 SCAR=30.5
159 65 for my $field ( @{ $base->get_fields } ) {
160 242 my $name = $field->name;
161 242 my $config = $field->model_config;
162  
163 242 ▶ next if not defined $name || $config->{accessor};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name or $config->{"accessor"} 1 241 242
Condition66%2/3
defined $name or $config->{"accessor"}
l!l&&r!l&&!r
24101
MC/DC50%1/2
defined $name or $config->{"accessor"}
defined $name $config->{"accessor"}
Truth table66%2/3
A or B
A= defined $nameB= $config->{"accessor"}
ABresult
1X1
011
000
164 241 ▶ next if not is_direct_child( $base, $field );
Branch: 1 branch
BranchTrueFalseTotal
unless is_direct_child($base, $field) 44 197 241
165  
166 197 ▶ $name = $field->original_name if $field->original_name;
Branch: 1 branch
BranchTrueFalseTotal
if $field->original_name 58 139 197
167  
168 197 my $accessor = $config->{accessor};
169  
170 197 ▶ if ( defined $accessor ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $accessor) { } 2 195 197
elsif ($dbic->can($name)) { } 147 48 195
171 2 $field->default( $dbic->$accessor );
172 }
173 elsif ( $dbic->can($name) ) {
174 147 my $has_col = $dbic->result_source->has_column($name);
175 147 my $has_rel = $dbic->result_source->has_relationship($name);
176  
177 147 ▶ if ( $has_col && $has_rel ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($has_col and $has_rel) { } 0 147 147
elsif ($has_col) { } 134 13 147
elsif ($field->multi_value and $config->{'default_column'} || ref $dbic->$name && $dbic->$name->can('result_source')) { } 9 4 13
Condition66%2/3
$has_col and $has_rel
!ll&&!rl&&r
131340
MC/DC0%0/2
$has_col and $has_rel
$has_col $has_rel
Truth table66%2/3
A and B
A= $has_colB= $has_rel
ABresult
0X0
100
111
178  
179 # can't use direct accessor, if there's a rel of the same name
180 0 $field->default( $dbic->get_column($name) );
181 }
182 elsif ($has_col) {
183 134 $field->default( $dbic->$name );
184 }
185 ▶ elsif (
Condition77%7/9
$field->multi_value and $config->{"default_column"} || ref $dbic->$name && $dbic->$name->can("result_source")
!ll&&!rl&&r
319
$config->{"default_column"} or ref $dbic->$name and $dbic->$name->can("result_source")
l!l&&r!l&&!r
091
ref $dbic->$name and $dbic->$name->can("result_source")
!ll&&!rl&&r
109
MC/DC50%2/4
$field->multi_value and $config->{"default_column"} || ref $dbic->$name && $dbic->$name->can("result_source")
$field->multi_value $config->{"default_column"} ref $dbic->$name $dbic->$name->can("result_source")
Truth table60%3/5
A and B or C and D
A= $field->multi_valueB= $config->{"default_column"}C= ref $dbic->$nameD= $dbic->$name->can("result_source")
ABCDresult
0XXX0
100X0
10100
11XX1
10111
186 $field->multi_value
187 && ($config->{default_column}
188 || ( ref( $dbic->$name )
189 && $dbic->$name->can('result_source') ) ) )
190 {
191  
192 my ($col) = $config->{default_column}
193 9 ▶ || $dbic->$name->result_source->primary_columns;
Condition33%1/3
$config->{'default_column'} || $dbic->$name->result_source->primary_columns
l!l&&r!l&&!r
090
MC/DC0%0/2
$config->{'default_column'} || $dbic->$name->result_source->primary_columns
$config->{'default_column'} $dbic->$name->result_source->primary_columns
Truth table33%1/3
A || B
A= $config->{'default_column'}B= $dbic->$name->result_source->primary_columns
ABresult
1X1
011
000
194  
195 9 my $info = $dbic->result_source->relationship_info($name);
196  
197 9 ▶ if ( !defined $info or $info->{attrs}{accessor} eq 'multi' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $info or $info->{'attrs'}{'accessor'} eq 'multi') { } 7 2 9
Condition100%3/3
not defined $info or $info->{"attrs"}{"accessor"} eq "multi"
l!l&&r!l&&!r
522
MC/DC100%2/2
not defined $info or $info->{"attrs"}{"accessor"} eq "multi"
not defined $info $info->{"attrs"}{"accessor"} eq "multi"
Truth table100%3/3
not(A) or B
A= not defined $infoB= $info->{"attrs"}{"accessor"} eq "multi"
ABresult
1X1
011
000
198 7 my @defaults = $dbic->$name->get_column($col)->all;
199 7 $field->default( \@defaults );
200 }
201 else {
202  
203 # has_one/might_have
204 2 my ($pk) = $dbic->result_source->primary_columns;
205 2 $field->default( $dbic->$name->$pk );
206 }
207 }
208 else {
209  
210 # This field is a method expected to return the value
211 4 $field->default( $dbic->$name );
212 }
213 }
214  
215 # handle {label}
216  
217 197 ▶ if ( defined( my $label = $config->{label} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $label = $config->{"label"})) 3 194 197
218 3 my $has_rel = $dbic->result_source->has_relationship($label);
219  
220 3 ▶ if ($has_rel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_rel) { } 0 3 3
221  
222 # can't use direct accessor, if there's a rel of the same name
223 0 $field->label( $dbic->get_column($label) );
224 }
225 else {
226 3 $field->label( $dbic->$label );
227 }
228 }
229 }
230 }
231  
232 # loop over all child blocks with nested_name that is a method on the DBIC row
233 # and recurse
234 sub _fill_nested {
235 65 ▶ my ( $self, $base, $dbic ) = @_;
Subroutine _fill_nested: called CC=28 SCAR=33.5
236  
237 65 for my $block ( @{ $base->get_all_elements } ) {
238 284 ▶ next if $block->is_field && !$block->is_block;
Branch: 1 branch
BranchTrueFalseTotal
if $block->is_field and not $block->is_block 235 49 284
Condition100%3/3
$block->is_field and not $block->is_block
!ll&&!rl&&r
445235
MC/DC100%2/2
$block->is_field and not $block->is_block
$block->is_field not $block->is_block
Truth table100%3/3
A and not(B)
A= $block->is_fieldB= not $block->is_block
ABresult
0X0
100
111
239 49 ▶ next if !$block->can('nested_name');
Branch: 1 branch
BranchTrueFalseTotal
unless $block->can("nested_name") 0 49 49
240  
241 49 my $config = $block->model_config;
242  
243 # first handle {label}
244  
245 49 ▶ if ( defined( my $label = $config->{label} ) && $block->can('label') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $label = $config->{"label"}) and $block->can("label")) 1 48 49
Condition66%2/3
defined(my $label = $config->{"label"}) and $block->can("label")
!ll&&!rl&&r
4801
MC/DC50%1/2
defined(my $label = $config->{"label"}) and $block->can("label")
defined(my $label = $config->{"label"}) $block->can("label")
Truth table66%2/3
A and B
A= defined(my $label = $config->{"label"})B= $block->can("label")
ABresult
0X0
100
111
246 1 my $has_rel = $dbic->result_source->has_relationship($label);
247  
248 1 ▶ if ($has_rel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_rel) { } 0 1 1
249  
250 # can't use direct accessor, if there's a rel of the same name
251 0 $block->label( $dbic->get_column($label) );
252 }
253 else {
254 1 $block->label( $dbic->$label );
255 }
256 }
257  
258 49 my $rel = $block->nested_name;
259 49 ▶ next if !defined $rel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rel 23 26 49
260  
261 26 ▶ my $has_rel = $dbic->result_source->relationship_info($rel)
Condition100%6/6
$dbic->result_source->relationship_info($rel) || $dbic->can($rel) && $dbic->can('add_to_' . $rel)
l!l&&r!l&&!r
1547
$dbic->can($rel) and $dbic->can('add_to_' . $rel)
!ll&&!rl&&r
434
MC/DC100%3/3
$dbic->result_source->relationship_info($rel) || $dbic->can($rel) && $dbic->can('add_to_' . $rel)
$dbic->result_source->relationship_info($rel) $dbic->can($rel) $dbic->can('add_to_' . $rel)
Truth table100%4/4
A || B and C
A= $dbic->result_source->relationship_info($rel)B= $dbic->can($rel)C= $dbic->can('add_to_' . $rel)
ABCresult
1XX1
0111
00X0
0100
262 || ( $dbic->can($rel) && $dbic->can( 'add_to_' . $rel ) )
263 ; # many_to_many
264  
265 # recursing only when $rel is a relation or non-column accessor on $dbic
266 next
267 26 ▶ unless $has_rel
Branch: 1 branch
BranchTrueFalseTotal
unless $has_rel or $dbic->can($rel) and not $dbic->result_source->has_column($rel) 6 20 26
Condition100%6/6
$has_rel or $dbic->can($rel) and not $dbic->result_source->has_column($rel)
l!l&&r!l&&!r
1916
$dbic->can($rel) and not $dbic->result_source->has_column($rel)
!ll&&!rl&&r
421
MC/DC66%2/3
$has_rel or $dbic->can($rel) and not $dbic->result_source->has_column($rel)
$has_rel $dbic->can($rel) not $dbic->result_source->has_column($rel)
Truth table75%3/4
A or B and not(C)
A= $has_relB= $dbic->can($rel)C= not $dbic->result_source->has_column($rel)
ABCresult
1XX1
0111
00X0
0100
268 || ( $dbic->can($rel)
269 && !$dbic->result_source->has_column($rel) );
270  
271 20 ▶ if ( $block->is_repeatable && $block->increment_field_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->is_repeatable and $block->increment_field_names) { } 13 7 20
Condition66%2/3
$block->is_repeatable and $block->increment_field_names
!ll&&!rl&&r
7013
MC/DC50%1/2
$block->is_repeatable and $block->increment_field_names
$block->is_repeatable $block->increment_field_names
Truth table66%2/3
A and B
A= $block->is_repeatableB= $block->increment_field_names
ABresult
0X0
100
111
272  
273 # check there's a field name matching the PK
274 13 my ($pk) = $dbic->$rel->result_source->primary_columns;
275  
276 next
277 unless grep {
278 15 ▶ $pk eq
Branch: 1 branch
BranchTrueFalseTotal
defined $_->original_name ? : 3 12 15
279 ( defined $_->original_name ? $_->original_name : $_->name )
280 13 ▶ } @{ $block->get_fields( { type => 'Hidden' } ) };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$pk eq (defined $_->original_name ? $_->original_name : $_->name);} @{$block->get_fields({"type", "Hidden"});} 0 13 13
281  
282 13 my @rows = $dbic->$rel->all;
283  
284 my $count
285 = $config->{empty_rows}
286 ? scalar @rows + $config->{empty_rows}
287 13 ▶ : scalar @rows;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'empty_rows'} ? : 3 10 13
288  
289 13 my $blocks = $block->repeat($count);
290  
291 13 $block->process;
292  
293 13 for my $rep ( 0 .. $#rows ) {
294 26 default_values( $self, $rows[$rep],
295 { base => $blocks->[$rep] } );
296 }
297  
298 # set the counter field to the number of rows
299  
300 13 ▶ if ( defined( my $param_name = $block->counter_name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $param_name = $block->counter_name)) 13 0 13
301 my ($field) = grep {
302 124 ▶ $param_name eq (
Branch: 1 branch
BranchTrueFalseTotal
defined $_->original_name ? : 82 42 124
303 defined $_->original_name
304 ? $_->original_name
305 : $_->name )
306 13 } @{ $base->get_fields };
307  
308 13 ▶ $field->default($count)
Branch: 1 branch
BranchTrueFalseTotal
if defined $field 13 0 13
309 if defined $field;
310 }
311  
312 # remove 'delete' checkbox from the last repetition ?
313  
314 13 ▶ if ( $config->{empty_rows} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"empty_rows"}) 3 10 13
315  
316 my $new_row_count
317 = $config->{empty_rows}
318 ? $config->{empty_rows}
319 3 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'empty_rows'} ? : 3 0 3
320  
321 3 my @reps = reverse @{ $block->get_elements };
322  
323 3 for my $i ( 0 .. ( $new_row_count - 1 ) ) {
324  
325 5 my $rep = $reps[$i];
326  
327 my ($del_field)
328 12 = grep { $_->model_config->{delete_if_true} }
329 5 @{ $rep->get_fields };
330  
331 5 ▶ if ( defined $del_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $del_field) 2 3 5
332 2 $del_field->parent->remove_element($del_field);
333 }
334 }
335 }
336 }
337 else {
338 7 ▶ if ( defined( my $row = $dbic->$rel ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $row = $dbic->$rel)) 6 1 7
339 6 default_values( $self, $row, { base => $block } );
340 }
341 }
342 }
343 65 return;
344 }
345  
346 sub create {
347 3 ▶ my ( $self, $attrs ) = @_;
Subroutine create: called CC=7 SCAR=22.0
Pod documented
348  
349 3 ▶ croak "invalid arguments" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 3 3
350  
351 3 my $form = $self->form;
352 3 ▶ my $base = defined $attrs->{base} ? delete $attrs->{base} : $form;
Branch: 1 branch
BranchTrueFalseTotal
defined $attrs->{'base'} ? : 0 3 3
353  
354 my $schema = $form->stash->{schema}
355 3 ▶ or croak 'schema required on form stash, if no row object provided';
Branch: 1 branch
BranchTrueFalseTotal
my $schema = $form->stash->{"schema"} or croak("schema required on form stash, if no row object provided") 0 3 3
356  
357 my $resultset
358 = $attrs->{resultset}
359 || $base->model_config->{resultset}
360 || $form->model_config->{resultset}
361 3 ▶ or croak 'could not find resultset name';
Branch: 1 branch
BranchTrueFalseTotal
my $resultset = $attrs->{"resultset"} || $base->model_config->{"resultset"} || $form->model_config->{"resultset"} or croak("could not find resultset name") 0 3 3
Condition33%2/6
$attrs->{'resultset'} || $base->model_config->{'resultset'} || $form->model_config->{'resultset'}
l!l&&r!l&&!r
300
$attrs->{"resultset"} or $base->model_config->{"resultset"}
l!l&&r!l&&!r
030
MC/DC0%0/3
$attrs->{'resultset'} || $base->model_config->{'resultset'} || $form->model_config->{'resultset'}
$attrs->{"resultset"} $base->model_config->{"resultset"} $form->model_config->{'resultset'}
Truth table25%1/4
A or B || C
A= $attrs->{"resultset"}B= $base->model_config->{"resultset"}C= $form->model_config->{'resultset'}
ABCresult
1XX1
01X1
0011
0000
362  
363 3 $resultset = $schema->resultset($resultset);
364  
365 3 my $dbic = $resultset->new_result( {} );
366  
367 3 return $self->update( $dbic, { %$attrs, base => $base } );
368 }
369  
370 sub update {
371 107 ▶ my ( $self, $dbic, $attrs ) = @_;
Subroutine update: called CC=19 SCAR=29.5
Pod documented
372  
373 107 ▶ croak "row object missing" if !defined $dbic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbic 0 107 107
374  
375 107 my $form = $self->form;
376 107 ▶ my $base = defined $attrs->{base} ? delete $attrs->{base} : $form;
Branch: 1 branch
BranchTrueFalseTotal
defined $attrs->{'base'} ? : 47 60 107
377  
378 $base = $form->get_all_element( { nested_name => $attrs->{nested_base} } )
379 if defined $attrs->{nested_base}
380 && ( !defined $base->nested_name
381 107 ▶ || $base->nested_name ne $attrs->{nested_base} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"nested_base"} and !defined($base->nested_name) || $base->nested_name ne $attrs->{"nested_base"} 3 104 107
Condition66%4/6
defined $attrs->{"nested_base"} and !defined($base->nested_name) || $base->nested_name ne $attrs->{"nested_base"}
!ll&&!rl&&r
96110
not defined $base->nested_name or $base->nested_name ne $attrs->{"nested_base"}
l!l&&r!l&&!r
308
MC/DC0%0/3
defined $attrs->{"nested_base"} and !defined($base->nested_name) || $base->nested_name ne $attrs->{"nested_base"}
defined $attrs->{"nested_base"} not defined $base->nested_name $base->nested_name ne $attrs->{"nested_base"}
Truth table50%2/4
A and not(B) or C
A= defined $attrs->{"nested_base"}B= not defined $base->nested_nameC= $base->nested_name ne $attrs->{"nested_base"}
ABCresult
0XX0
1000
11X1
1011
382  
383 107 my $rs = $dbic->result_source;
384 107 my @rels = $rs->relationships;
385 107 my @cols = $rs->columns;
386  
387 # check for belongs_to relationships with a required foreign key
388 107 my (@belongs_to_rels, @other_rels);
389  
390 107 foreach my $rel (@rels) {
391 # 'fk_columns' is set for belong_to rels in DBIx::Class::Relationship::BelongsTo
392 333 my @fk_columns = keys %{ $dbic->relationship_info($rel)->{attrs}{fk_columns} };
393  
394 333 ▶ if (@fk_columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fk_columns) { } 55 278 333
395 55 push @belongs_to_rels, $rel;
396 } else {
397 278 push @other_rels, $rel;
398 }
399 }
400  
401 # add belongs_to rels before insert
402 107 ▶ if (@belongs_to_rels) {
Branch: 1 branch
BranchTrueFalseTotal
if (@belongs_to_rels) 28 79 107
403 # tell _save_relationships not to update $dbic yet, just add the rels
404 28 my %attrs = ( %$attrs, no_update => 1 );
405 28 _save_relationships( $self, $base, $dbic, $form, $rs, \%attrs, \@belongs_to_rels );
406 }
407  
408  
409 107 ▶ _save_columns( $base, $dbic, $form ) or return;
Branch: 1 branch
BranchTrueFalseTotal
_save_columns($base, $dbic, $form) or return 1 106 107
410  
411 106 $dbic->update_or_insert;
412  
413 106 _save_relationships( $self, $base, $dbic, $form, $rs, $attrs, \@other_rels );
414  
415 106 _save_multi_value_fields_many_to_many( $base, $dbic, $form, $attrs, \@rels,
416 \@cols );
417  
418 106 _save_repeatable_many_to_many( $self, $base, $dbic, $form, $attrs, \@rels,
419 \@cols );
420  
421 # handle non-rel, non-column, nested_base accessors.
422 # - this highlights a failing of the approach of iterating over
423 # db cols + rels - we should maybe refactor to iterate over
424 # form blocks and fields instead ?
425  
426 106 for my $block ( @{ $base->get_all_elements } ) {
427 626 ▶ next if $block->is_field;
Branch: 1 branch
BranchTrueFalseTotal
if $block->is_field 505 121 626
428 121 ▶ next if !$block->can('nested_name');
Branch: 1 branch
BranchTrueFalseTotal
unless $block->can("nested_name") 0 121 121
429  
430 121 my $rel = $block->nested_name;
431 121 ▶ next if !defined $rel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rel 41 80 121
432  
433 80 ▶ next unless $dbic->can($rel);
Branch: 1 branch
BranchTrueFalseTotal
unless $dbic->can($rel) 53 27 80
434  
435 27 ▶ next if grep { $rel eq $_ } @cols;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$rel eq $_;} @cols 1 26 27
436 26 ▶ next if grep { $rel eq $_ } @rels;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$rel eq $_;} @rels 22 4 26
437  
438 4 ▶ next if $dbic->can( "add_to_" . $rel ); # many-to-many
Branch: 1 branch
BranchTrueFalseTotal
if $dbic->can('add_to_' . $rel) 3 1 4
439  
440 1 ▶ if ( defined( my $row = $dbic->$rel ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $row = $dbic->$rel)) 1 0 1
441 1 update( $self, $row, { base => $block } );
442 }
443 }
444  
445 106 return $dbic;
446 }
447  
448 sub _save_relationships {
449 134 ▶ my ( $self, $base, $dbic, $form, $rs, $attrs, $rels ) = @_;
Subroutine _save_relationships: called CC=27 SCAR=33.3
450  
451 134 ▶ return if $attrs->{no_follow};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"no_follow"} 0 134 134
452  
453 134 for my $rel (@$rels) {
454  
455 # don't follow rels to where we came from
456 next
457 if defined $attrs->{from}
458 332 ▶ && $attrs->{from} eq $rs->related_source($rel)->result_class;
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"from"} and $attrs->{"from"} eq $rs->related_source($rel)->result_class 34 298 332
Condition100%3/3
defined $attrs->{"from"} and $attrs->{"from"} eq $rs->related_source($rel)->result_class
!ll&&!rl&&r
2683034
MC/DC100%2/2
defined $attrs->{"from"} and $attrs->{"from"} eq $rs->related_source($rel)->result_class
defined $attrs->{"from"} $attrs->{"from"} eq $rs->related_source($rel)->result_class
Truth table100%3/3
A and B
A= defined $attrs->{"from"}B= $attrs->{"from"} eq $rs->related_source($rel)->result_class
ABresult
0X0
100
111
459  
460 298 my @elements = @{ $base->get_all_elements( { nested_name => $rel } ) };
461  
462 298 my ($block) = grep { !$_->is_field } @elements;
463 298 ▶ my ($multi_value) = grep { $_->is_field && $_->multi_value } @elements;
Branch: 1 branch
BranchTrueFalseTotal
$_->is_field and $_->multi_value 9 23 32
464 298 my ($combo) = grep { $_->isa('HTML::FormFu::Element::ComboBox') } @elements;
465  
466 298 ▶ next if !defined $block && !defined $multi_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block or defined $multi_value 268 30 298
Condition100%3/3
defined $block or defined $multi_value
l!l&&r!l&&!r
237268
MC/DC100%2/2
defined $block or defined $multi_value
defined $block defined $multi_value
Truth table100%3/3
A or B
A= defined $blockB= defined $multi_value
ABresult
1X1
011
000
467 30 ▶ next if !$form->valid($rel);
Branch: 1 branch
BranchTrueFalseTotal
unless $form->valid($rel) 0 30 30
468  
469 30 my $params = $form->param($rel);
470  
471 30 ▶ if ( defined $block && $block->is_repeatable ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $block and $block->is_repeatable) { } 15 15 30
elsif (defined $combo) { } 3 12 15
elsif (defined $block and ref $params eq 'HASH') { } 8 4 12
elsif (defined $multi_value) { } 4 0 4
Condition100%3/3
defined $block and $block->is_repeatable
!ll&&!rl&&r
7815
MC/DC100%2/2
defined $block and $block->is_repeatable
defined $block $block->is_repeatable
Truth table100%3/3
A and B
A= defined $blockB= $block->is_repeatable
ABresult
0X0
100
111
472  
473 # Handle has_many
474  
475 15 _save_has_many( $self, $dbic, $form, $rs, $block, $rel, $attrs );
476  
477 }
478 elsif ( defined $combo ) {
479  
480 3 _save_combobox( $self, $base, $dbic, $form, $rs, $combo, $rel, $attrs );
481 }
482 ▶ elsif ( defined $block && ref $params eq 'HASH' ) {
Condition66%2/3
defined $block and ref $params eq "HASH"
!ll&&!rl&&r
408
MC/DC50%1/2
defined $block and ref $params eq "HASH"
defined $block ref $params eq "HASH"
Truth table66%2/3
A and B
A= defined $blockB= ref $params eq "HASH"
ABresult
0X0
100
111
483 # It seems that $dbic->$rel must be called otherwise the following
484 # find_related() can fail.
485 # However, this can die - so we're just wrapping it in an eval
486 8 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dbic->$rel } } or $dbic->discard_changes 6 2 8
487 8 $dbic->$rel;
488 } or $dbic->discard_changes;
489  
490 8 my $target = $dbic->find_related( $rel, {} );
491  
492 8 ▶ if ( !defined $target && grep { length $_ } values %$params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $target and grep {length $_;} values %$params) 6 2 8
Condition66%2/3
not defined $target and grep {length $_;} values %$params
!ll&&!rl&&r
206
MC/DC50%1/2
not defined $target and grep {length $_;} values %$params
not defined $target grep {length $_;} values %$params
Truth table66%2/3
not(A) and B
A= not defined $targetB= grep {length $_;} values %$params
ABresult
0X0
100
111
493 6 $target = $dbic->new_related( $rel, {} );
494 }
495  
496 8 ▶ next if !defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 8 8
497  
498 8 update(
499 $self, $target,
500 { %$attrs,
501 base => $block,
502 nested_base => $rel,
503 from => $dbic->result_class,
504 } );
505 8 ▶ unless ( $dbic->$rel ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbic->$rel) 2 6 8
506 2 $dbic->$rel($target);
507 2 ▶ $dbic->update unless $attrs->{no_update};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs->{"no_update"} 1 1 2
508 }
509 }
510 elsif ( defined $multi_value ) {
511  
512 # belongs_to, has_one or might_have relationship
513  
514 4 my $info = $dbic->result_source->relationship_info($rel);
515  
516 4 my @fpkey = $dbic->related_resultset($rel)
517 ->result_source->primary_columns;
518  
519 4 my @cond = ( %{ $info->{cond} } );
520  
521 # make sure $rel is a has_one or might_have rel
522 # stolen from SQL/Translator/Parser/DBIx/Class
523  
524 4 my $fk_constraint;
525  
526 # Get the key information, mapping off the foreign/self markers
527 4 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
528 4 my @keys = map { $info->{cond}{$_} =~ /^\w+\.(\w+)$/ }
529 4 grep { exists $info->{cond}{$_} } @cond;
530  
531 #first it can be specified explicitly
532 4 ▶ if ( exists $info->{attrs}{is_foreign_key_constraint} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $info->{'attrs'}{'is_foreign_key_constraint'}) { } 2 2 4
elsif ($info->{'attrs'}{'accessor'} and $info->{'attrs'}{'accessor'} eq 'multi') { } 0 2 2
533 2 $fk_constraint = $info->{attrs}{is_foreign_key_constraint};
534 }
535  
536 # it can not be multi
537 ▶ elsif ($info->{attrs}{accessor}
Condition33%1/3
$info->{"attrs"}{"accessor"} and $info->{"attrs"}{"accessor"} eq "multi"
!ll&&!rl&&r
020
MC/DC0%0/2
$info->{"attrs"}{"accessor"} and $info->{"attrs"}{"accessor"} eq "multi"
$info->{"attrs"}{"accessor"} $info->{"attrs"}{"accessor"} eq "multi"
Truth table33%1/3
A and B
A= $info->{"attrs"}{"accessor"}B= $info->{"attrs"}{"accessor"} eq "multi"
ABresult
0X0
100
111
538 && $info->{attrs}{accessor} eq 'multi' )
539 {
540 0 $fk_constraint = 0;
541 }
542  
543 # if indeed single, check if all self.columns are our primary keys.
544 # this is supposed to indicate a has_one/might_have...
545 # where's the introspection!!?? :)
546 else {
547 2 $fk_constraint
548 = not _compare_relationship_keys( \@keys, \@fpkey );
549 }
550  
551 4 ▶ next if ($fk_constraint);
Branch: 1 branch
BranchTrueFalseTotal
if $fk_constraint 2 2 4
552  
553 2 my $fpkey = shift @fpkey;
554 2 my ( $fkey, $skey ) = @cond;
555 2 $fkey =~ s/^foreign\.//;
556 2 $skey =~ s/^self\.//;
557  
558 2 my $fclass = $info->{class};
559  
560 2 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $fpkey eq $fkey 0 2 2
561 'The primary key and the foreign key may not be the same column in class '
562 . $fclass
563 if $fpkey eq $fkey;
564  
565 2 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if @fpkey > 1 0 2 2
566 'multiple primary keys are not supported for has_one/might_have relationships'
567 if ( @fpkey > 1 );
568  
569 2 my $schema = $dbic->result_source->schema;
570  
571 # use transactions if supported by storage
572 $schema->txn_do(
573 sub {
574  
575 # reset any previous items which were related to $dbic
576 2 ▶ $rs->schema->resultset($fclass)
Subroutine __ANON__: called CC=2 SCAR=6.9
577 ->search( { $fkey => $dbic->$skey } )
578 ->update( { $fkey => undef } );
579  
580 # set new related item
581 2 my $updated
582 = $rs->schema->resultset($fclass)
583 ->search( { $fpkey => $params } )
584 ->update( { $fkey => $dbic->$skey } );
585  
586 2 ▶ $schema->txn_rollback
Branch: 1 branch
BranchTrueFalseTotal
if $updated != 1 1 1 2
587 if $updated != 1;
588  
589 2 } );
590 }
591 }
592 }
593  
594 sub _save_combobox {
595 3 ▶ my ( $self, $base, $dbic, $form, $rs, $combo, $rel, $attrs ) = @_;
Subroutine _save_combobox: called CC=3 SCAR=11.0
596  
597 3 my $select = $combo->get_field({ type => 'Select' });
598 3 my $text = $combo->get_field({ type => 'Text' });
599  
600 3 my $select_value = $form->param( $select->nested_name );
601 3 my $text_value = $form->param( $text->nested_name );
602  
603 3 my $target_rs = $dbic->result_source->related_source( $rel )->resultset;
604 3 my $target;
605  
606 3 ▶ if ( defined $select_value && length $select_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $select_value and length $select_value) { } 2 1 3
Condition66%2/3
defined $select_value and length $select_value
!ll&&!rl&&r
012
MC/DC50%1/2
defined $select_value and length $select_value
defined $select_value length $select_value
Truth table66%2/3
A and B
A= defined $select_valueB= length $select_value
ABresult
0X0
100
111
607 2 my $pk_name = $combo->model_config->{select_column};
608  
609 2 $target = $target_rs->find(
610 {
611 $pk_name => $select_value,
612 },
613 );
614 }
615 else {
616 1 my $column_name = $combo->model_config->{text_column};
617  
618 1 $target = $target_rs->create(
619 {
620 $column_name => $text_value,
621 },
622 );
623 }
624  
625 3 $dbic->set_from_related( $rel, $target );
626 3 $dbic->update;
627 }
628  
629 # Copied from DBIx::Class::ResultSource
630 sub _compare_relationship_keys {
631 2 ▶ my ( $keys1, $keys2 ) = @_;
Subroutine _compare_relationship_keys: called CC=10 SCAR=23.6
632  
633 # Make sure every keys1 is in keys2
634 2 my $found;
635 2 foreach my $key (@$keys1) {
636 2 $found = 0;
637 2 foreach my $prim (@$keys2) {
638 2 ▶ if ( $prim eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prim eq $key) 2 0 2
639 2 $found = 1;
640 2 last;
641 }
642 }
643 2 ▶ last unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 2 2
644 }
645  
646 # Make sure every key2 is in key1
647 2 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 2 0 2
648 2 foreach my $prim (@$keys2) {
649 2 $found = 0;
650 2 foreach my $key (@$keys1) {
651 2 ▶ if ( $prim eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prim eq $key) 2 0 2
652 2 $found = 1;
653 2 last;
654 }
655 }
656 2 ▶ last unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 2 2
657 }
658 }
659  
660 2 return $found;
661 }
662  
663 sub _save_has_many {
664 15 ▶ my ( $self, $dbic, $form, $rs, $block, $rel, $attrs ) = @_;
Subroutine _save_has_many: called CC=16 SCAR=27.9
665  
666 15 ▶ return unless $block->increment_field_names;
Branch: 1 branch
BranchTrueFalseTotal
unless $block->increment_field_names 0 15 15
667  
668 # check there's a field name matching the PK
669  
670 15 my ($pk) = $rs->related_source($rel)->primary_columns;
671  
672 return
673 41 unless grep { $_->original_name eq $pk }
674 15 ▶ @{ $block->get_fields( { type => 'Hidden' } ) };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->original_name eq $pk;} @{$block->get_fields({"type", "Hidden"});} 0 15 15
675  
676 15 my @blocks = @{ $block->get_elements };
677 15 my $max = $#blocks;
678 15 my $config = $block->model_config;
679  
680 15 ▶ my $new_rows_max = $config->{new_rows_max} || $config->{empty_rows} || 0;
Condition100%5/5
$config->{'new_rows_max'} || $config->{'empty_rows'} || 0
l!l
87
$config->{"new_rows_max"} or $config->{"empty_rows"}
l!l&&r!l&&!r
537
MC/DC100%2/2
$config->{'new_rows_max'} || $config->{'empty_rows'} || 0
$config->{"new_rows_max"} $config->{"empty_rows"}
Truth table100%3/3
A or B
A= $config->{"new_rows_max"}B= $config->{"empty_rows"}
ABresult
1X1
011
000
681 15 my $new_rows_counter = 0;
682  
683 # iterate over blocks, not rows
684 # new rows might have been created in the meantime
685  
686 15 for my $i ( 0 .. $max ) {
687 36 my $rep = $blocks[$i];
688  
689 # find PK field
690  
691 my ($pk_field)
692 41 = grep { $_->original_name eq $pk }
693 36 @{ $rep->get_fields( { type => 'Hidden' } ) };
694  
695 36 ▶ next if !defined $pk_field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pk_field 0 36 36
696  
697 36 my $value = $form->param_value( $pk_field->nested_name );
698 36 my $row;
699  
700 36 ▶ if (( !defined $value || $value eq '' )
Branch: 2 branches
BranchTrueFalseTotal
if (!defined($value) || $value eq '' and ($new_rows_max and ++$new_rows_counter <= $new_rows_max)) { } 11 25 36
elsif (not defined $value or $value eq '') { } 3 22 25
Condition88%8/9
!defined($value) || $value eq "" and ($new_rows_max and ++$new_rows_counter <= $new_rows_max)
!ll&&!rl&&r
22311
not defined $value or $value eq ""
l!l&&r!l&&!r
5922
$new_rows_max and ++$new_rows_counter <= $new_rows_max
!ll&&!rl&&r
3011
MC/DC75%3/4
!defined($value) || $value eq "" and ($new_rows_max and ++$new_rows_counter <= $new_rows_max)
not defined $value $value eq "" $new_rows_max ++$new_rows_counter <= $new_rows_max
Truth table57%4/7
not(A) or B and C and D
A= not defined $valueB= $value eq ""C= $new_rows_maxD= ++$new_rows_counter <= $new_rows_max
ABCDresult
00XX0
1X0X0
1X100
010X0
01100
1X111
01111
701 && ( $new_rows_max
702 && ( ++$new_rows_counter <= $new_rows_max ) ) )
703 {
704  
705 # insert a new row
706 11 $row = _insert_has_many( $dbic, $form, $config, $rep, $rel,
707 $pk_field );
708  
709 11 ▶ next if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 3 8 11
710 }
711 ▶ elsif ( !defined $value || $value eq '' ) {
Condition66%2/3
not defined $value or $value eq ""
l!l&&r!l&&!r
3022
MC/DC50%1/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table66%2/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
712 3 next;
713 }
714 else {
715 22 $row = $dbic->find_related( $rel, $value );
716 }
717 30 ▶ next if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 30 30
718  
719 # should we delete the row?
720  
721 30 ▶ next if _delete_has_many( $form, $row, $rep );
Branch: 1 branch
BranchTrueFalseTotal
if _delete_has_many($form, $row, $rep) 2 28 30
722  
723 28 update(
724 $self, $row,
725 { %$attrs,
726 base => $rep,
727 repeat_base => $rel,
728 from => $dbic->result_class,
729 } );
730 }
731 }
732  
733 sub _insert_has_many {
734 11 ▶ my ( $dbic, $form, $config, $repetition, $rel, $pk_field ) = @_;
Subroutine _insert_has_many: called CC=2 SCAR=6.9
735  
736 return
737 11 ▶ if !_can_insert_new_row( $dbic, $form, $config, $repetition, $rel,
Branch: 1 branch
BranchTrueFalseTotal
unless _can_insert_new_row($dbic, $form, $config, $repetition, $rel, $pk_field) 3 8 11
738 $pk_field );
739  
740 8 my $row = $dbic->new_related( $rel, {} );
741  
742 8 return $row;
743 }
744  
745 sub _can_insert_new_row {
746 12 ▶ my ( $dbic, $form, $config, $repetition, $rel, $pk_field ) = @_;
Subroutine _can_insert_new_row: called CC=5 SCAR=16.1
747  
748 12 my @rep_fields = @{ $repetition->get_fields };
749  
750 12 my $pk_name = $pk_field->nested_name;
751  
752 2 my @constraints = grep { $_->when->{field} eq $pk_name }
753 2 grep { defined $_->when }
754 12 map { @{ $_->get_constraints( { type => 'Required' } ) } } @rep_fields;
755  
756 12 my @required_fields;
757  
758 12 ▶ if (@constraints) {
Branch: 1 branch
BranchTrueFalseTotal
if (@constraints) { } 2 10 12
759  
760 # if there are any Required constraints whose 'when' clause points to
761 # the PK field - check that all these fields are filled in - as
762 # the PK value is missing on new reps, so the constraint won't have run
763  
764 return
765 2 ▶ if notall { defined && length }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 1 1 2
766 2 map { $form->param_value( $_->nested_name ) }
767 2 ▶ map { $_->parent } @constraints;
Branch: 1 branch
BranchTrueFalseTotal
if &notall(sub { defined $_ and length $_; } , map({$form->param_value($_->nested_name);} map({$_->parent;} @constraints))) 1 1 2
768 }
769 else {
770  
771 # otherwise, just check at least 1 field that matches either a column
772 # name or an accessor, is filled in
773  
774 10 my $result_source = $dbic->$rel->result_source;
775  
776 # only create a new record if (read from bottom)...
777  
778 return
779 20 ▶ if none { defined && length }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 18 2 20
780 20 map { $form->param_value( $_->nested_name ) }
781 grep {
782 20 ▶ $result_source->has_column( $_->original_name )
Branch: 1 branch
BranchTrueFalseTotal
$result_source->has_column($_->original_name) or $result_source->can($_->original_name) 0 20 20
783 || $result_source->can( $_->original_name )
784 }
785 10 ▶ grep { defined $_->original_name } @rep_fields;
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { defined $_ and length $_; } , map({$form->param_value($_->nested_name);} grep({$result_source->has_column($_->original_name) or $result_source->can($_->original_name);} grep({defined $_->original_name;} @rep_fields)))) 2 8 10
786 }
787  
788 9 return 1;
789 }
790  
791 sub _delete_has_many {
792 30 ▶ my ( $form, $row, $rep ) = @_;
Subroutine _delete_has_many: called CC=5 SCAR=16.2
793  
794 my ($del_field)
795 30 = grep { $_->model_config->{delete_if_true} } @{ $rep->get_fields };
796  
797 30 ▶ return if !defined $del_field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $del_field 27 3 30
798  
799 3 my $nested_name = $del_field->nested_name;
800  
801 return
802 3 ▶ unless $form->valid($nested_name)
Branch: 1 branch
BranchTrueFalseTotal
unless $form->valid($nested_name) and $form->param_value($nested_name) 1 2 3
Condition66%2/3
$form->valid($nested_name) and $form->param_value($nested_name)
!ll&&!rl&&r
102
MC/DC50%1/2
$form->valid($nested_name) and $form->param_value($nested_name)
$form->valid($nested_name) $form->param_value($nested_name)
Truth table66%2/3
A and B
A= $form->valid($nested_name)B= $form->param_value($nested_name)
ABresult
0X0
100
111
803 && $form->param_value($nested_name);
804  
805 2 ▶ $row->delete if ( $row->in_storage );
Branch: 1 branch
BranchTrueFalseTotal
if $row->in_storage 2 0 2
806  
807 2 return 1;
808 }
809  
810 sub _fix_value {
811 249 ▶ my ( $dbic, $col, $value, $field, ) = @_;
Subroutine _fix_value: called CC=13 SCAR=25.7
812  
813 249 my $col_info = $dbic->column_info($col);
814 249 ▶ my $is_nullable = $col_info->{is_nullable} || 0;
Condition100%2/2
$col_info->{'is_nullable'} || 0
l!l
93156
MC/DC100%1/1
$col_info->{'is_nullable'} || 0
$col_info->{'is_nullable'}
Truth table100%2/2
A
A= $col_info->{'is_nullable'}
Aresult
11
00
815 249 ▶ my $data_type = $col_info->{data_type} || '';
Condition50%1/2
$col_info->{'data_type'} || ''
l!l
2490
MC/DC0%0/1
$col_info->{'data_type'} || ''
$col_info->{'data_type'}
Truth table50%1/2
A
A= $col_info->{'data_type'}
Aresult
11
00
816  
817 249 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 222 27 249
818 222 ▶ if ( ( ( $is_nullable
Branch: 1 branch
BranchTrueFalseTotal
if ($is_nullable && $data_type =~ /^timestamp|date|int|float|numeric/i || $field->model_config->{"null_if_empty"} and not ref $value and $value eq "") 1 221 222
Condition91%11/12
$is_nullable && $data_type =~ /^timestamp|date|int|float|numeric/i || $field->model_config->{"null_if_empty"} and not ref $value and $value eq ""
!ll&&!rl&&r
21381
$is_nullable && $data_type =~ /^timestamp|date|int|float|numeric/i || $field->model_config->{"null_if_empty"} and not ref $value
!ll&&!rl&&r
21309
$is_nullable and $data_type =~ /^timestamp|date|int|float|numeric/i or $field->model_config->{"null_if_empty"}
l!l&&r!l&&!r
54213
$is_nullable and $data_type =~ /^timestamp|date|int|float|numeric/i
!ll&&!rl&&r
153645
MC/DC40%2/5
$is_nullable && $data_type =~ /^timestamp|date|int|float|numeric/i || $field->model_config->{"null_if_empty"} and not ref $value and $value eq ""
$is_nullable $data_type =~ /^timestamp|date|int|float|numeric/i $field->model_config->{"null_if_empty"} not ref $value $value eq ""
Truth table45%5/11
A and B or C and not(D) and E
A= $is_nullableB= $data_type =~ /^timestamp|date|int|float|numeric/iC= $field->model_config->{"null_if_empty"}D= not ref $valueE= $value eq ""
ABCDEresult
0X0XX0
100XX0
11X0X0
0X10X0
1010X0
11X100
0X1100
101100
11X111
0X1111
101111
819 && $data_type =~ m/^timestamp|date|int|float|numeric/i
820 ) or $field->model_config->{null_if_empty} )
821  
822 # comparing to '' does not work for inflated objects
823 && !ref $value
824 && $value eq ''
825 )
826 {
827 1 $value = undef;
828 }
829 }
830  
831 249 ▶ if ( !defined $value
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and defined $field and $field->isa("HTML::FormFu::Element::Checkbox") and not $is_nullable) 3 246 249
Condition88%8/9
not defined $value and defined $field and $field->isa("HTML::FormFu::Element::Checkbox") and not $is_nullable
!ll&&!rl&&r
24243
not defined $value and defined $field and $field->isa("HTML::FormFu::Element::Checkbox")
!ll&&!rl&&r
221217
not defined $value and defined $field
!ll&&!rl&&r
221028
MC/DC75%3/4
not defined $value and defined $field and $field->isa("HTML::FormFu::Element::Checkbox") and not $is_nullable
not defined $value defined $field $field->isa("HTML::FormFu::Element::Checkbox") not $is_nullable
Truth table80%4/5
not(A) and B and C and not(D)
A= not defined $valueB= defined $fieldC= $field->isa("HTML::FormFu::Element::Checkbox")D= not $is_nullable
ABCDresult
0XXX0
10XX0
110X0
11100
11111
832 && defined $field
833 && $field->isa('HTML::FormFu::Element::Checkbox')
834 && !$is_nullable )
835 {
836 3 $value = 0;
837 }
838  
839 249 return $value;
840 }
841  
842 sub _save_columns {
843 107 ▶ my ( $base, $dbic, $form ) = @_;
Subroutine _save_columns: called CC=35 SCAR=35.8
844  
845 107 for my $field ( @{ $base->get_fields }, ) {
846 506 ▶ next if not is_direct_child( $base, $field );
Branch: 1 branch
BranchTrueFalseTotal
unless is_direct_child($base, $field) 147 359 506
847  
848 359 my $config = $field->model_config;
849 359 ▶ next if $config->{delete_if_true};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"delete_if_true"} 1 358 359
850 358 ▶ next if $config->{read_only};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"read_only"} 2 356 358
851  
852 356 my $name = $field->name;
853 356 ▶ $name = $field->original_name if $field->original_name;
Branch: 1 branch
BranchTrueFalseTotal
if $field->original_name 74 282 356
854  
855 356 ▶ my $accessor = $config->{accessor} || $name;
Condition66%2/3
$config->{'accessor'} || $name
l!l&&r!l&&!r
23540
MC/DC0%0/2
$config->{'accessor'} || $name
$config->{'accessor'} $name
Truth table66%2/3
A || B
A= $config->{'accessor'}B= $name
ABresult
1X1
011
000
856 356 ▶ next if not defined $accessor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $accessor 0 356 356
857  
858 my $value = ( $dbic->result_source->has_column($accessor)
859 and exists $dbic->result_source->column_info($accessor)->{is_array} )
860 356 ▶ ? $form->param_array( $field->nested_name )
Branch: 1 branch
BranchTrueFalseTotal
$dbic->result_source->has_column($accessor) && exists $dbic->result_source->column_info($accessor)->{'is_array'} ? : 5 351 356
Condition100%3/3
$dbic->result_source->has_column($accessor) and exists $dbic->result_source->column_info($accessor)->{"is_array"}
!ll&&!rl&&r
882635
MC/DC100%2/2
$dbic->result_source->has_column($accessor) and exists $dbic->result_source->column_info($accessor)->{"is_array"}
$dbic->result_source->has_column($accessor) exists $dbic->result_source->column_info($accessor)->{"is_array"}
Truth table100%3/3
A and B
A= $dbic->result_source->has_column($accessor)B= exists $dbic->result_source->column_info($accessor)->{"is_array"}
ABresult
0X0
100
111
861 : $form->param_value( $field->nested_name ) ;
862  
863 next
864 if $config->{ignore_if_empty}
865 356 ▶ && ( !defined $value || $value eq "" );
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"ignore_if_empty"} and !defined($value) || $value eq "" 1 355 356
Condition66%4/6
$config->{"ignore_if_empty"} and !defined($value) || $value eq ""
!ll&&!rl&&r
35240
not defined $value or $value eq ""
l!l&&r!l&&!r
103
MC/DC0%0/3
$config->{"ignore_if_empty"} and !defined($value) || $value eq ""
$config->{"ignore_if_empty"} not defined $value $value eq ""
Truth table50%2/4
A and not(B) or C
A= $config->{"ignore_if_empty"}B= not defined $valueC= $value eq ""
ABCresult
0XX0
1000
11X1
1011
866  
867 355 my ($pk) = $dbic->result_source->primary_columns;
868  
869 # don't set primary key to null or '' - for Pg SERIALs
870 355 ▶ next if ( $name eq $pk ) && !( defined $value && length $value );
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $pk and not defined $value && length $value 17 338 355
Condition100%6/6
$name eq $pk and not defined $value && length $value
!ll&&!rl&&r
2706817
defined $value && length $value
!ll&&!rl&&r
51268
MC/DC100%3/3
$name eq $pk and not defined $value && length $value
$name eq $pk defined $value length $value
Truth table100%4/4
A and not(B && C)
A= $name eq $pkB= defined $valueC= length $value
ABCresult
0XX0
1110
10X1
1101
871  
872 338 ▶ if ( $config->{delete_if_empty}
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"delete_if_empty"} and not defined $value && length $value) 1 337 338
Condition50%3/6
$config->{"delete_if_empty"} and not defined $value && length $value
!ll&&!rl&&r
33701
defined $value && length $value
!ll&&!rl&&r
010
MC/DC33%1/3
$config->{"delete_if_empty"} and not defined $value && length $value
$config->{"delete_if_empty"} defined $value length $value
Truth table50%2/4
A and not(B && C)
A= $config->{"delete_if_empty"}B= defined $valueC= length $value
ABCresult
0XX0
1110
10X1
1101
873 && ( !defined $value || !length $value ) )
874 {
875 1 ▶ $dbic->discard_changes if $dbic->is_changed;
Branch: 1 branch
BranchTrueFalseTotal
if $dbic->is_changed 0 1 1
876 1 ▶ $dbic->delete if $dbic->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
if $dbic->in_storage 1 0 1
877 1 return;
878 }
879 337 ▶ if ( $dbic->result_source->has_column($accessor) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbic->result_source->has_column($accessor)) { } 249 88 337
elsif ($field->isa('HTML::FormFu::Element::Checkbox')) { } 2 86 88
880 249 $value = _fix_value( $dbic, $accessor, $value, $field );
881 }
882 elsif ( $field->isa('HTML::FormFu::Element::Checkbox') ) {
883  
884 # We are a checkbox.
885 2 ▶ unless ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 1 2
886 1 $value = 0;
887 }
888 }
889  
890 337 ▶ if ( !$config->{accessor}
Branch: 2 branches
BranchTrueFalseTotal
if (not $config->{'accessor'} and $dbic->result_source->has_relationship($accessor) and $dbic->result_source->has_column($accessor)) { } 4 333 337
elsif ($dbic->can($accessor) and not $dbic->result_source->relationship_info($accessor) and not $dbic->can('add_to_' . $accessor)) { } 253 80 333
Condition100%6/6
not $config->{"accessor"} and $dbic->result_source->has_relationship($accessor) and $dbic->result_source->has_column($accessor)
!ll&&!rl&&r
32854
not $config->{"accessor"} and $dbic->result_source->has_relationship($accessor)
!ll&&!rl&&r
23269
MC/DC100%3/3
not $config->{"accessor"} and $dbic->result_source->has_relationship($accessor) and $dbic->result_source->has_column($accessor)
not $config->{"accessor"} $dbic->result_source->has_relationship($accessor) $dbic->result_source->has_column($accessor)
Truth table100%4/4
not(A) and B and C
A= not $config->{"accessor"}B= $dbic->result_source->has_relationship($accessor)C= $dbic->result_source->has_column($accessor)
ABCresult
0XX0
10X0
1100
1111
891 and $dbic->result_source->has_relationship($accessor)
892 and $dbic->result_source->has_column($accessor) )
893 {
894 4 $dbic->set_column( $accessor, $value );
895 }
896 ▶ elsif (
Condition100%6/6
$dbic->can($accessor) and not $dbic->result_source->relationship_info($accessor) and not $dbic->can('add_to_' . $accessor)
!ll&&!rl&&r
755253
$dbic->can($accessor) and not $dbic->result_source->relationship_info($accessor)
!ll&&!rl&&r
705258
MC/DC100%3/3
$dbic->can($accessor) and not $dbic->result_source->relationship_info($accessor) and not $dbic->can('add_to_' . $accessor)
$dbic->can($accessor) not $dbic->result_source->relationship_info($accessor) not $dbic->can('add_to_' . $accessor)
Truth table100%4/4
A and not(B) and not(C)
A= $dbic->can($accessor)B= not $dbic->result_source->relationship_info($accessor)C= not $dbic->can('add_to_' . $accessor)
ABCresult
0XX0
10X0
1100
1111
897 $dbic->can($accessor)
898  
899 # and $accessor is not a has_one or might_have rel where the foreign key is on the foreign table
900 and !$dbic->result_source->relationship_info($accessor)
901 and !$dbic->can( 'add_to_' . $accessor ) )
902 {
903 253 $dbic->$accessor($value);
904 }
905 else {
906  
907 # We should just ignore
908 #croak "cannot call $accessor on $dbic";
909 }
910 }
911  
912 # for values inserted by add_valid - and not correlated to any field in the form
913 106 my $parent = $base;
914 106 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not defined $parent or do { do { return 1 if defined $parent->nested_name; $parent = $parent->parent } } 0 60 60
915 106 ▶ return 1 if defined $parent->nested_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parent->nested_name 46 60 106
916 60 $parent = $parent->parent;
917 } until ( !defined $parent );
918  
919 60 for my $valid ( $form->valid ) {
920 273 ▶ next if @{ $base->get_fields( name => $valid ) };
Branch: 1 branch
BranchTrueFalseTotal
if @{$base->get_fields("name", $valid);} 172 101 273
921 101 ▶ next if not $dbic->can($valid);
Branch: 1 branch
BranchTrueFalseTotal
unless $dbic->can($valid) 100 1 101
922  
923 1 my $value = $form->param_value($valid);
924 1 $dbic->$valid($value);
925 }
926  
927 60 return 1;
928 }
929  
930 sub _save_multi_value_fields_many_to_many {
931 106 ▶ my ( $base, $dbic, $form, $attrs, $rels, $cols ) = @_;
Subroutine _save_multi_value_fields_many_to_many: called CC=19 SCAR=30.8
932  
933 my @fields = grep {
934 ( defined $attrs->{nested_base} && defined $_->parent->nested_name )
935 ? $_->parent->nested_name eq $attrs->{nested_base}
936 63 ▶ : !$_->nested
Branch: 1 branch
BranchTrueFalseTotal
defined $attrs->{'nested_base'} && defined $_->parent->nested_name ? : 8 55 63
Condition66%2/3
defined $attrs->{"nested_base"} and defined $_->parent->nested_name
!ll&&!rl&&r
5508
MC/DC50%1/2
defined $attrs->{"nested_base"} and defined $_->parent->nested_name
defined $attrs->{"nested_base"} defined $_->parent->nested_name
Truth table66%2/3
A and B
A= defined $attrs->{"nested_base"}B= defined $_->parent->nested_name
ABresult
0X0
100
111
937 }
938 505 grep { $_->multi_value }
939 106 grep { defined $_->name } @{ $base->get_fields };
940  
941 106 for my $field (@fields) {
942 46 my $name = $field->name;
943  
944 46 ▶ next if grep { $name eq $_ } @$rels, @$cols;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$name eq $_;} @$rels, @$cols 38 8 46
945  
946 8 ▶ if ( $dbic->can($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbic->can($name)) 8 0 8
947 8 my $related = $dbic->$name;
948  
949 8 ▶ next if !blessed($related) || !$related->can('result_source');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($related) and $related->can("result_source") 2 6 8
Condition66%2/3
builtin::blessed($related) and $related->can("result_source")
!ll&&!rl&&r
206
MC/DC50%1/2
builtin::blessed($related) and $related->can("result_source")
builtin::blessed($related) $related->can("result_source")
Truth table66%2/3
A and B
A= builtin::blessed($related)B= $related->can("result_source")
ABresult
0X0
100
111
950  
951 6 my $nested_name = $field->nested_name;
952  
953 6 ▶ next if $form->has_errors($nested_name);
Branch: 1 branch
BranchTrueFalseTotal
if $form->has_errors($nested_name) 0 6 6
954  
955 6 my @values = $form->param_list($nested_name);
956 6 my @rows;
957  
958 6 my $config = $field->model_config;
959  
960 6 ▶ next if $config->{read_only};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"read_only"} 1 5 6
961  
962 my ($pk) = $config->{default_column}
963 5 ▶ || $related->result_source->primary_columns;
Condition33%1/3
$config->{'default_column'} || $related->result_source->primary_columns
l!l&&r!l&&!r
050
MC/DC0%0/2
$config->{'default_column'} || $related->result_source->primary_columns
$config->{'default_column'} $related->result_source->primary_columns
Truth table33%1/3
A || B
A= $config->{'default_column'}B= $related->result_source->primary_columns
ABresult
1X1
011
000
964  
965 5 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 3 2 5
966  
967 3 ▶ $pk = "me.$pk" unless $pk =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk =~ /\./ 3 0 3
968  
969 @rows = $related->result_source->resultset->search( {
970 3 ▶ %{ $config->{condition} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$config->{"condition"} or {} 2 1 3
971 $pk => { -in => \@values } } )->all;
972 }
973  
974 5 ▶ if ( $config->{additive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'additive'}) { } 0 5 5
975 0 $pk =~ s/^.*\.//;
976  
977 0 my $set_method = "add_to_$name";
978 0 my $remove_method = "remove_from_$name";
979  
980 0 foreach my $row (@rows) {
981 0 $dbic->$remove_method($row);
982 0 $dbic->$set_method( $row, $config->{link_values} );
983 }
984 }
985 else {
986  
987 # check if there is a restricting condition on here
988 # if so life is more complex
989 5 my $condition = $config->{'-condition'};
990 5 ▶ if ($condition) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition) { } 1 4 5
991 1 my $set_method = "add_to_$name";
992 1 my $remove_method = "remove_from_$name";
993 1 foreach ( $dbic->$name->search($condition)->all ) {
994 1 $dbic->$remove_method($_);
995 }
996 1 foreach my $row (@rows) {
997 1 $dbic->$set_method( $row, $config->{link_values} );
998 }
999 }
1000 else {
1001 4 my $set_method = "set_$name";
1002 4 $dbic->$set_method( \@rows, $config->{link_values} );
1003 }
1004 }
1005 }
1006 }
1007 }
1008  
1009 sub _save_repeatable_many_to_many {
1010 106 ▶ my ( $self, $base, $dbic, $form, $attrs, $rels, $cols ) = @_;
Subroutine _save_repeatable_many_to_many: called CC=21 SCAR=31.5
1011  
1012 my @blocks
1013 626 ▶ = grep { !$_->is_field && $_->is_repeatable && $_->increment_field_names }
Branch: 1 branch
BranchTrueFalseTotal
not $_->is_field and $_->is_repeatable and $_->increment_field_names 22 604 626
Condition100%3/3
not $_->is_field and $_->is_repeatable
!ll&&!rl&&r
5059922
MC/DC100%2/2
not $_->is_field and $_->is_repeatable
not $_->is_field $_->is_repeatable
Truth table100%3/3
not(A) and B
A= not $_->is_fieldB= $_->is_repeatable
ABresult
0X0
100
111
1014 106 @{ $base->get_all_elements };
1015  
1016 106 for my $block (@blocks) {
1017 22 my $rel = $block->nested_name;
1018  
1019 22 ▶ next if !defined $rel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rel 0 22 22
1020 22 ▶ next if grep { $rel eq $_ } @$rels, @$cols;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$rel eq $_;} @$rels, @$cols 15 7 22
1021  
1022 7 ▶ if ( $dbic->can($rel) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbic->can($rel)) 3 4 7
1023  
1024 # check there's a field name matching the PK
1025  
1026 3 my ($pk) = $dbic->$rel->result_source->primary_columns;
1027  
1028 3 my @blocks = @{ $block->get_elements };
1029 3 my $max = $#blocks;
1030  
1031 # iterate over blocks, not rows
1032 # new rows might have been created in the meantime
1033  
1034 3 for my $i ( 0 .. $max ) {
1035 7 my $rep = $blocks[$i];
1036  
1037 # find PK field
1038  
1039 my ($pk_field)
1040 12 = grep { $_->original_name eq $pk }
1041 7 @{ $rep->get_fields( { type => 'Hidden' } ) };
1042  
1043 7 ▶ next if !defined $pk_field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pk_field 0 7 7
1044  
1045 7 my $value = $form->param_value( $pk_field->nested_name );
1046 7 my $row;
1047 my $is_new;
1048  
1049 7 my $config = $block->model_config;
1050 my $new_rows_max
1051 = $config->{new_rows_max}
1052 || $config->{empty_rows}
1053 7 ▶ || 0;
Condition80%4/5
$config->{'new_rows_max'} || $config->{'empty_rows'} || 0
l!l
34
$config->{"new_rows_max"} or $config->{"empty_rows"}
l!l&&r!l&&!r
304
MC/DC50%1/2
$config->{'new_rows_max'} || $config->{'empty_rows'} || 0
$config->{"new_rows_max"} $config->{"empty_rows"}
Truth table66%2/3
A or B
A= $config->{"new_rows_max"}B= $config->{"empty_rows"}
ABresult
1X1
011
000
1054 7 my $new_rows_counter = 0;
1055  
1056 7 ▶ if (( !defined $value || $value eq '' )
Branch: 2 branches
BranchTrueFalseTotal
if (!defined($value) || $value eq '' and ($new_rows_max and ++$new_rows_counter <= $new_rows_max)) { } 1 6 7
elsif (not defined $value or $value eq '') { } 0 6 6
Condition55%5/9
!defined($value) || $value eq "" and ($new_rows_max and ++$new_rows_counter <= $new_rows_max)
!ll&&!rl&&r
601
not defined $value or $value eq ""
l!l&&r!l&&!r
016
$new_rows_max and ++$new_rows_counter <= $new_rows_max
!ll&&!rl&&r
001
MC/DC25%1/4
!defined($value) || $value eq "" and ($new_rows_max and ++$new_rows_counter <= $new_rows_max)
not defined $value $value eq "" $new_rows_max ++$new_rows_counter <= $new_rows_max
Truth table28%2/7
not(A) or B and C and D
A= not defined $valueB= $value eq ""C= $new_rows_maxD= ++$new_rows_counter <= $new_rows_max
ABCDresult
00XX0
1X0X0
1X100
010X0
01100
1X111
01111
1057 && ( $new_rows_max
1058 && ( ++$new_rows_counter <= $new_rows_max ) ) )
1059 {
1060  
1061 # insert a new row
1062 1 $row = _insert_many_to_many( $dbic, $form, $config, $rep,
1063 $rel, $pk_field );
1064  
1065 1 ▶ next if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 1 1
1066  
1067 1 $is_new = 1;
1068 }
1069 ▶ elsif ( !defined $value || $value eq '' ) {
Condition33%1/3
not defined $value or $value eq ""
l!l&&r!l&&!r
006
MC/DC0%0/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table33%1/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
1070 0 next;
1071 }
1072 else {
1073 6 $row = $dbic->$rel->find($value);
1074 }
1075 7 ▶ next if !defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 7 7
1076  
1077 # should we delete the row?
1078  
1079 7 ▶ next if _delete_many_to_many( $form, $dbic, $row, $rel, $rep );
Branch: 1 branch
BranchTrueFalseTotal
if _delete_many_to_many($form, $dbic, $row, $rel, $rep) 0 7 7
1080  
1081 7 update(
1082 $self, $row,
1083 { %$attrs,
1084 base => $rep,
1085 repeat_base => $rel,
1086 from => $dbic->result_class,
1087 } );
1088  
1089 7 ▶ if ($is_new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_new) 1 6 7
1090  
1091 # new rows need to be related
1092 1 my $add_method = "add_to_$rel";
1093  
1094 1 $dbic->$add_method($row);
1095 }
1096 }
1097 }
1098 }
1099 106 return;
1100 }
1101  
1102 sub _insert_many_to_many {
1103 1 ▶ my ( $dbic, $form, $config, $repetition, $rel, $pk_field ) = @_;
Subroutine _insert_many_to_many: called CC=2 SCAR=7.0
1104  
1105 return
1106 1 ▶ if !_can_insert_new_row( $dbic, $form, $config, $repetition, $rel,
Branch: 1 branch
BranchTrueFalseTotal
unless _can_insert_new_row($dbic, $form, $config, $repetition, $rel, $pk_field) 0 1 1
1107 $pk_field );
1108  
1109 1 my $row = $dbic->$rel->new( {} );
1110  
1111 # add_to_* will be called later, after update is called on this row
1112  
1113 1 return $row;
1114 }
1115  
1116 sub _delete_many_to_many {
1117 7 ▶ my ( $form, $dbic, $row, $rel, $rep ) = @_;
Subroutine _delete_many_to_many: called CC=4 SCAR=21.2
1118  
1119 my ($del_field)
1120 7 = grep { $_->model_config->{delete_if_true} } @{ $rep->get_fields };
1121  
1122 7 ▶ return if !defined $del_field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $del_field 7 0 7
1123  
1124 0 my $nested_name = $del_field->nested_name;
1125  
1126 return
1127 0 ▶ unless $form->valid($nested_name)
Branch: 1 branch
BranchTrueFalseTotal
unless $form->valid($nested_name) and $form->param_value($nested_name) 0 0 0
Condition0%0/3
$form->valid($nested_name) and $form->param_value($nested_name)
!ll&&!rl&&r
000
MC/DC0%0/2
$form->valid($nested_name) and $form->param_value($nested_name)
$form->valid($nested_name) $form->param_value($nested_name)
Truth table0%0/3
A and B
A= $form->valid($nested_name)B= $form->param_value($nested_name)
ABresult
0X0
100
111
1128 && $form->param_value($nested_name);
1129  
1130 0 my $remove = "remove_from_$rel";
1131  
1132 0 $dbic->$remove($row);
1133  
1134 0 return 1;
1135 }
1136  
1137 90 1;
1138  
1139 __END__
1140  
1141 =head1 NAME
1142  
1143 HTML::FormFu::Model::DBIC - Integrate HTML::FormFu with DBIx::Class
1144  
1145 =head1 VERSION
1146  
1147 version 2.02
1148  
1149 =head1 SYNOPSIS
1150  
1151 Example of typical use in a Catalyst controller:
1152  
1153 sub edit : Chained {
1154 my ( $self, $c ) = @_;
1155  
1156 my $form = $c->stash->{form};
1157 my $book = $c->stash->{book};
1158  
1159 if ( $form->submitted_and_valid ) {
1160  
1161 # update dbic row with submitted values from form
1162  
1163 $form->model->update( $book );
1164  
1165 $c->response->redirect( $c->uri_for('view', $book->id) );
1166 return;
1167 }
1168 elsif ( !$form->submitted ) {
1169  
1170 # use dbic row to set form's default values
1171  
1172 $form->model->default_values( $book );
1173 }
1174  
1175 return;
1176 }
1177  
1178 =head1 SETUP
1179  
1180 For the form object to be able to access your L<DBIx::Class> schema, it needs
1181 to be placed on the form stash, with the name C<schema>.
1182  
1183 This is easy if you're using L<Catalyst-Controller-HTML-FormFu>, as you can
1184 set this up to happen in your Catalyst app's config file.
1185  
1186 For example, if your model is named C<MyApp::Model::Corp>, you would set this
1187 (in L<Config::General> format):
1188  
1189 <Controller::HTML::FormFu>
1190 <model_stash>
1191 schema Corp
1192 </model_stash>
1193 </Controller::HTML::FormFu>
1194  
1195 Or if your app's config file is in L<YAML> format:
1196  
1197 'Controller::HTML::FormFu':
1198 model_stash:
1199 schema: Corp
1200  
1201 =head1 METHODS
1202  
1203 =head2 default_values
1204  
1205 Arguments: $dbic_row, [\%config]
1206  
1207 Return Value: $form
1208  
1209 $form->model->default_values( $dbic_row );
1210  
1211 Set a form's default values from the database, to allow a user to edit them.
1212  
1213 =head2 update
1214  
1215 Arguments: [$dbic_row], [\%config]
1216  
1217 Return Value: $dbic_row
1218  
1219 $form->model->update( $dbic_row );
1220  
1221 Update the database with the submitted form values.
1222  
1223 =head2 create
1224  
1225 Arguments: [\%config]
1226  
1227 Return Value: $dbic_row
1228  
1229 my $dbic_row = $form->model->create( {resultset => 'Book'} );
1230  
1231 Like L</update>, but doesn't require a C<$dbic_row> argument.
1232  
1233 You need to ensure the DBIC schema is available on the form stash - see
1234 L</SYNOPSIS> for an example config.
1235  
1236 The C<resultset> must be set either in the method arguments, or the form or
1237 block's C<model_config>.
1238  
1239 An example of setting the ResultSet name on a Form:
1240  
1241 ---
1242 model_config:
1243 resultset: FooTable
1244  
1245 elements:
1246 # [snip]
1247  
1248 =head2 options_from_model
1249  
1250 Populates a multi-valued field with values from the database.
1251  
1252 This method should not be called directly, but is called for you during
1253 C<< $form->process >> by fields that inherit from
1254 L<HTML::FormFu::Element::_Group>. This includes:
1255  
1256 =over
1257  
1258 =item L<HTML::FormFu::Element::Select>
1259  
1260 =item L<HTML::FormFu::Element::Checkboxgroup>
1261  
1262 =item L<HTML::FormFu::Element::Radiogroup>
1263  
1264 =item L<HTML::FormFu::Element::ComboBox>
1265  
1266 =back
1267  
1268 To use you must set the appropriate C<resultset> on the element C<model_config>:
1269  
1270 element:
1271 - type: Select
1272 name: foo
1273 model_config:
1274 resultset: TableClass
1275  
1276 =head1 BUILDING FORMS
1277  
1278 =head2 single table
1279  
1280 To edit the values in a row with no related rows, the field names simply have
1281 to correspond to the database column names.
1282  
1283 For the following DBIx::Class schema:
1284  
1285 package MySchema::Book;
1286 use base 'DBIx::Class';
1287  
1288 __PACKAGE__->load_components(qw/ Core /);
1289  
1290 __PACKAGE__->table("book");
1291  
1292 __PACKAGE__->add_columns(
1293 id => { data_type => "INTEGER" },
1294 title => { data_type => "TEXT" },
1295 author => { data_type => "TEXT" },
1296 blurb => { data_type => "TEXT" },
1297 );
1298  
1299 __PACKAGE__->set_primary_key("id");
1300  
1301 1;
1302  
1303 A suitable form for this might be:
1304  
1305 elements:
1306 - type: Text
1307 name: title
1308  
1309 - type: Text
1310 name: author
1311  
1312 - type: Textarea
1313 name: blurb
1314  
1315 =head2 might_have and has_one relationships
1316  
1317 Set field values from a related row with a C<might_have> or C<has_one>
1318 relationship by placing the fields within a
1319 L<Block|HTML::FormFu::Element::Block> (or any element that inherits from
1320 Block, such as L<Fieldset|HTML::FormFu::Element::Fieldset>) with its
1321 L<HTML::FormFu/nested_name> set to the relationship name.
1322  
1323 For the following DBIx::Class schemas:
1324  
1325 package MySchema::Book;
1326 use base 'DBIx::Class';
1327  
1328 __PACKAGE__->load_components(qw/ Core /);
1329  
1330 __PACKAGE__->table("book");
1331  
1332 __PACKAGE__->add_columns(
1333 id => { data_type => "INTEGER" },
1334 title => { data_type => "TEXT" },
1335 );
1336  
1337 __PACKAGE__->set_primary_key("id");
1338  
1339 __PACKAGE__->might_have( review => 'MySchema::Review', 'book' );
1340  
1341 1;
1342  
1343  
1344 package MySchema::Review;
1345 use base 'DBIx::Class';
1346  
1347 __PACKAGE__->load_components(qw/ Core /);
1348  
1349 __PACKAGE__->table("review");
1350  
1351 __PACKAGE__->add_columns(
1352 id => { data_type => "INTEGER" },
1353 book => { data_type => "INTEGER", is_nullable => 1 },
1354 review_text => { data_type => "TEXT" },
1355 );
1356  
1357 __PACKAGE__->set_primary_key("book");
1358  
1359 __PACKAGE__->belongs_to( book => 'MySchema::Book' );
1360  
1361 1;
1362  
1363 A suitable form for this would be:
1364  
1365 elements:
1366 - type: Text
1367 name: title
1368  
1369 - type: Block
1370 nested_name: review
1371 elements:
1372 - type: Textarea
1373 name: review_text
1374  
1375 For C<might_have> and C<has_one> relationships, you generally shouldn't need
1376 to have a field for the related table's primary key, as DBIx::Class will
1377 handle retrieving the correct row automatically.
1378  
1379 You can also set a C<has_one> or C<might_have> relationship using a multi value
1380 field like L<Select|HTML::FormFu::Element::Select>.
1381  
1382 elements:
1383 - type: Text
1384 name: title
1385  
1386 - type: Select
1387 nested: review
1388 model_config:
1389 resultset: Review
1390  
1391 This will load all reviews into the select field. If you select a review from
1392 that list, a current relationship to a review is removed and the new one is
1393 added. This requires that the primary key of the C<Review> table and the
1394 foreign key do not match.
1395  
1396 =head2 has_many and many_to_many relationships
1397  
1398 The general principle is the same as for C<might_have> and C<has_one> above,
1399 except you should use a L<Repeatable|HTML::FormFu::Element::Repeatable>
1400 element instead of a Block, and it needs to contain a
1401 L<Hidden|HTML::FormFu::Element::Hidden> field corresponding to the primary key
1402 of the related table.
1403  
1404 The Repeatable block's
1405 L<nested_name|HTML::FormFu::Element::Repeatable/nested_name> must be set to the
1406 name of the relationship.
1407  
1408 The Repeable block's
1409 L<increment_field_names|HTML::FormFu::Element::Repeatable/increment_field_names>
1410 must be true (which is the default value).
1411  
1412 The Repeable block's
1413 L<counter_name|HTML::FormFu::Element::Repeatable/counter_name> must be set to
1414 the name of a L<Hidden|HTML::FormFu::Element::Hidden> field, which is placed
1415 outside of the Repeatable block.
1416 This field is used to store a count of the number of repetitions of the
1417 Repeatable block were created.
1418 When the form is submitted, this value is used during C<< $form->process >>
1419 to ensure the form is rebuilt with the correct number of repetitions.
1420  
1421 To allow the user to add new related rows, either C<empty_rows> or
1422 C<new_rows_max> must be set - see L</"Config options for Repeatable blocks">
1423 below.
1424  
1425 For the following DBIx::Class schemas:
1426  
1427 package MySchema::Book;
1428 use base 'DBIx::Class';
1429  
1430 __PACKAGE__->load_components(qw/ Core /);
1431  
1432 __PACKAGE__->table("book");
1433  
1434 __PACKAGE__->add_columns(
1435 id => { data_type => "INTEGER" },
1436 title => { data_type => "TEXT" },
1437 );
1438  
1439 __PACKAGE__->set_primary_key("id");
1440  
1441 __PACKAGE__->has_many( review => 'MySchema::Review', 'book' );
1442  
1443 1;
1444  
1445  
1446 package MySchema::Review;
1447 use base 'DBIx::Class';
1448  
1449 __PACKAGE__->load_components(qw/ Core /);
1450  
1451 __PACKAGE__->table("review");
1452  
1453 __PACKAGE__->add_columns(
1454 book => { data_type => "INTEGER" },
1455 review_text => { data_type => "TEXT" },
1456 );
1457  
1458 __PACKAGE__->set_primary_key("book");
1459  
1460 __PACKAGE__->belongs_to( book => 'MySchema::Book' );
1461  
1462 1;
1463  
1464 A suitable form for this might be:
1465  
1466 elements:
1467 - type: Text
1468 name: title
1469  
1470 - type: Hidden
1471 name: review_count
1472  
1473 - type: Repeatable
1474 nested_name: review
1475 counter_name: review_count
1476 model_config:
1477 empty_rows: 1
1478 elements:
1479 - type: Hidden
1480 name: book
1481  
1482 - type: Textarea
1483 name: review_text
1484  
1485 =head2 belongs_to relationships
1486  
1487 Belongs-to relationships can be edited / created with a ComboBox element.
1488 If the user selects a value with the Select field, the belongs-to will be set
1489 to an already-existing row in the related table.
1490 If the user enters a value into the Text field, the belongs-to will be set
1491 using a newly-created row in the related table.
1492  
1493 elements:
1494 - type: ComboBox
1495 name: author
1496 model_config:
1497 resultset: Author
1498 select_column: id
1499 text_column: name
1500  
1501 The element name should match the relationship name.
1502 C<< $field->model_config->{select_column} >> should match the related primary
1503 column.
1504 C<< $field->model_config->{text_column} >> should match the related text
1505 column.
1506  
1507 =head2 many_to_many selection
1508  
1509 To select / deselect rows from a C<many_to_many> relationship, you must use
1510 a multi-valued element, such as a
1511 L<Checkboxgroup|HTML::FormFu::Element::Checkboxgroup> or a
1512 L<Select|HTML::FormFu::Element::Select> with
1513 L<multiple|HTML::FormFu::Element::Select/multiple> set.
1514  
1515 The field's L<name|HTML::FormFu::Element::_Field/name> must be set to the
1516 name of the C<many_to_many> relationship.
1517  
1518 =head3 default_column
1519  
1520 If you want to search / associate the related table by a column other it's
1521 primary key, set C<< $field->model_config->{default_column} >>.
1522  
1523 ---
1524 element:
1525 - type: Checkboxgroup
1526 name: authors
1527 model_config:
1528 default_column: foo
1529  
1530 =head3 link_values
1531  
1532 If you want to set columns on the link table you can do so if you add a
1533 C<link_values> attribute to C<model_config>:
1534  
1535 ---
1536 element:
1537 - type: Checkboxgroup
1538 name: authors
1539 model_config:
1540 link_values:
1541 foo: bar
1542  
1543 =head3 additive
1544  
1545 The default implementation will first remove all related objects and set the
1546 new ones (see L<http://search.cpan.org/perldoc?DBIx::Class::Relationship::Base#set_$rel>).
1547 If you want to add the selected objects to the current set of objects
1548 set C<additive> in the C<model_config>.
1549  
1550 ---
1551 element:
1552 - type: Checkboxgroup
1553 name: authors
1554 model_config:
1555 additive: 1
1556 options_from_model: 0
1557  
1558 L</options_from_model> is set to C<0> because it will try to fetch
1559 all objects from the result class C<Authors> if C<model_config> is specified
1560 without a C<resultset> attribute.)
1561  
1562 =head1 COMMON ARGUMENTS
1563  
1564 The following items are supported in the optional C<config> hash-ref argument
1565 to the methods L<default_values>, L<update> and L<create>.
1566  
1567 =over
1568  
1569 =item base
1570  
1571 If you want the method to process a particular Block element, rather than the
1572 whole form, you can pass the element as a C<base> argument.
1573  
1574 $form->default_values(
1575 $row,
1576 {
1577 base => $formfu_element,
1578 },
1579 );
1580  
1581 =item nested_base
1582  
1583 If you want the method to process a particular Block element by
1584 L<name|HTML::FormFu::Element/name>, you can pass the name as an argument.
1585  
1586 $form->default_values(
1587 $row,
1588 {
1589 nested_base => 'foo',
1590 }'
1591 );
1592  
1593 =back
1594  
1595 =head1 CONFIGURATION
1596  
1597 =head2 Config options for fields
1598  
1599 The following items are supported as C<model_config> options on form fields.
1600  
1601 =over
1602  
1603 =item accessor
1604  
1605 If set, C<accessor> will be used as a method-name accessor on the
1606 C<DBIx::Class> row object, instead of using the field name.
1607  
1608 =item ignore_if_empty
1609  
1610 If the submitted value is blank, no attempt will be made to save it to the database.
1611  
1612 =item null_if_empty
1613  
1614 If the submitted value is blank, save it as NULL to the database. Normally an empty string is saved as NULL when its corresponding field is numeric, and as an empty string when its corresponding field is a text field. This option is useful for changing the default behavior for text fields.
1615  
1616 =item delete_if_empty
1617  
1618 Useful for editing a "might_have" related row containing only one field.
1619  
1620 If the submitted value is blank, the related row is deleted.
1621  
1622 For the following DBIx::Class schemas:
1623  
1624 package MySchema::Book;
1625 use base 'DBIx::Class';
1626  
1627 __PACKAGE__->load_components(qw/ Core /);
1628  
1629 __PACKAGE__->table("book");
1630  
1631 __PACKAGE__->add_columns(
1632 id => { data_type => "INTEGER" },
1633 title => { data_type => "TEXT" },
1634 );
1635  
1636 __PACKAGE__->set_primary_key("id");
1637  
1638 __PACKAGE__->might_have( review => 'MySchema::Review', 'book' );
1639  
1640 1;
1641  
1642  
1643 package MySchema::Review;
1644 use base 'DBIx::Class';
1645  
1646 __PACKAGE__->load_components(qw/ Core /);
1647  
1648 __PACKAGE__->table("review");
1649  
1650 __PACKAGE__->add_columns(
1651 book => { data_type => "INTEGER" },
1652 review_text => { data_type => "TEXT" },
1653 );
1654  
1655 __PACKAGE__->set_primary_key("book");
1656  
1657 __PACKAGE__->belongs_to( book => 'MySchema::Book' );
1658  
1659 1;
1660  
1661 A suitable form for this would be:
1662  
1663 elements:
1664 - type: Text
1665 name: title
1666  
1667 - type: Block
1668 nested_name: review
1669 elements:
1670 - type: Text
1671 name: review_text
1672 model_config:
1673 delete_if_empty: 1
1674  
1675 =item label
1676  
1677 To use a column value for a form field's
1678 L<label|HTML::FormFu::Element::_Field/label>.
1679  
1680 =back
1681  
1682 =head2 Config options for fields within a Repeatable block
1683  
1684 =over
1685  
1686 =item delete_if_true
1687  
1688 Intended for use on a L<Checkbox|HTML::FormFu::Element::Checkbox> field.
1689  
1690 If the checkbox is checked, the following occurs: for a has-many relationship,
1691 the related row is deleted; for a many-to-many relationship, the relationship
1692 link is removed.
1693  
1694 An example of use might be:
1695  
1696 elements:
1697 - type: Text
1698 name: title
1699  
1700 - type: Hidden
1701 name: review_count
1702  
1703 - type: Repeatable
1704 nested_name: review
1705 counter_name: review_count
1706 elements:
1707 - type: Hidden
1708 name: book
1709  
1710 - type: Textarea
1711 name: review_text
1712  
1713 - type: Checkbox
1714 name: delete_review
1715 label: 'Delete Review?'
1716 model_config:
1717 delete_if_true: 1
1718  
1719 Note: make sure the name of this field does not clash with one of your
1720 L<DBIx::Class::Row> method names (e.g. "delete") - see L</CAVEATS>.
1721  
1722 =back
1723  
1724 =head2 Config options for Repeatable blocks
1725  
1726 =over
1727  
1728 =item empty_rows
1729  
1730 For a Repeatable block corresponding to a has-many or many-to-many
1731 relationship, to allow the user to insert new rows, set C<empty_rows> to
1732 the number of extra repetitions you wish added to the end of the Repeatable
1733 block.
1734  
1735 =item new_rows_max
1736  
1737 Set to the maximum number of new rows that a Repeatable block is allowed to
1738 add.
1739  
1740 If not set, it will fallback to the value of C<empty_rows>.
1741  
1742 =back
1743  
1744 =head2 Config options for options_from_model
1745  
1746 The column used for the element values is set with the C<model_config>
1747 value C<id_column> - or if not set, the table's primary column is used.
1748  
1749 element:
1750 - type: Select
1751 name: foo
1752 model_config:
1753 resultset: TableClass
1754 id_column: pk_col
1755  
1756 The column used for the element labels is set with the C<model_config>
1757 value C<label_column> - or if not set, the first text/varchar column found
1758 in the table is used - or if one is not found, the C<id_column> is used
1759 instead.
1760  
1761 element:
1762 - type: Select
1763 name: foo
1764 model_config:
1765 resultset: TableClass
1766 label_column: label_col
1767  
1768 To pass the database label values via the form's localization object, set
1769 C<localize_label>
1770  
1771 element:
1772 - type: Select
1773 name: foo
1774 model_config:
1775 localize_label: 1
1776  
1777 You can set a C<condition>, which will be passed as the 1st argument to
1778 L<DBIx::Class::ResultSet/search>.
1779  
1780 element:
1781 - type: Select
1782 name: foo
1783 model_config:
1784 resultset: TableClass
1785 condition:
1786 type: is_foo
1787  
1788 You can set a C<condition_from_stash>, which will be passed as the 1st argument to
1789 L<DBIx::Class::ResultSet/search>.
1790  
1791 C<key> is the column-name to be passed to
1792 L<search|DBIx::Class::ResultSet/search>,
1793 and C<stash_key> is the name of a key on the form L<stash|HTML::FormFu/stash>
1794 from which the value to be passed to L<search|DBIx::Class::ResultSet/search>
1795 is found.
1796  
1797 element:
1798 - type: Select
1799 name: foo
1800 model_config:
1801 resultset: TableClass
1802 condition_from_stash:
1803 key: stash_key
1804  
1805 Is comparable to:
1806  
1807 $form->element({
1808 type => 'Select',
1809 name => 'foo',
1810 model_config => {
1811 resultset => 'TableClass',
1812 condition => {
1813 key => $form->stash->{stash_key}
1814 }
1815 }
1816 })
1817  
1818 You can set C<attributes>, which will be passed as the 2nd argument to
1819 L<DBIx::Class::ResultSet/search>.
1820  
1821 =head3 ENUM Column Type
1822  
1823 If the field name matches (case-insensitive) a column name with type 'ENUM'
1824 and the Schema contains enum values in
1825 C<< $resultset->column_info($name)->{extra}{list} >>, the field's options
1826 will be populated with the enum values.
1827  
1828 =head1 FAQ
1829  
1830 =head2 Add extra values not in the form
1831  
1832 To update values to the database which weren't submitted to the form,
1833 you can first add them to the form with L<add_valid|HTML::FormFu/add_valid>.
1834  
1835 my $passwd = generate_passwd();
1836  
1837 $form->add_valid( passwd => $passwd );
1838  
1839 $form->model->update( $row );
1840  
1841 C<add_valid> works for fieldnames that don't exist in the form.
1842  
1843 =head2 Set a field read only
1844  
1845 You can make a field read only. The value of such fields cannot be changed by
1846 the user even if they submit a value for it.
1847  
1848 $field->model_config->{read_only} = 1;
1849  
1850 - Name: field
1851 model_config:
1852 read_only: 1
1853  
1854 See L<HTML::FormFu::Element::Label>.
1855  
1856 =head1 CAVEATS
1857  
1858 To ensure your column's inflators and deflators are called, we have to
1859 get / set values using their named methods, and not with C<get_column> /
1860 C<set_column>.
1861  
1862 Because of this, beware of having column names which clash with DBIx::Class
1863 built-in method-names, such as C<delete>. - It will have obviously
1864 undesirable results!
1865  
1866 =head1 REMOVED METHODS
1867  
1868 =head2 new_empty_row
1869  
1870 See C<empty_rows> in L</"Config options for Repeatable blocks"> instead.
1871  
1872 =head2 new_empty_row_multi
1873  
1874 See C<new_rows_max> in L</"Config options for Repeatable blocks"> instead.
1875  
1876 =head2 Range constraint
1877  
1878 See C<empty_rows> in L</"Config options for Repeatable blocks"> instead.
1879  
1880 =head1 SUPPORT
1881  
1882 Project Page:
1883  
1884 L<http://code.google.com/p/html-formfu/>
1885  
1886 Mailing list:
1887  
1888 L<http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu>
1889  
1890 Mailing list archives:
1891  
1892 L<http://lists.scsys.co.uk/pipermail/html-formfu/>
1893  
1894 =head1 BUGS
1895  
1896 Please submit bugs / feature requests to
1897 L<http://code.google.com/p/html-formfu/issues/list> (preferred) or
1898 L<http://rt.perl.org>.
1899  
1900 =head1 GITHUB REPOSITORY
1901  
1902 This module's sourcecode is maintained in a git repository at
1903 L<git://github.com/fireartist/HTML-FormFu-Model-DBIC.git>
1904  
1905 The project page is L<https://github.com/fireartist/HTML-FormFu-Model-DBIC>
1906  
1907 =head1 SEE ALSO
1908  
1909 L<HTML::FormFu>, L<DBIx::Class>, L<Catalyst::Controller::HTML::FormFu>
1910  
1911 =head1 AUTHOR
1912  
1913 Carl Franks
1914  
1915 =head1 CONTRIBUTORS
1916  
1917 Based on the code of C<DBIx::Class::HTML::FormFu>, which was contributed to
1918 by:
1919  
1920 Adam Herzog
1921  
1922 Daisuke Maki
1923  
1924 Mario Minati
1925  
1926 =head1 COPYRIGHT AND LICENSE
1927  
1928 Copyright (C) 2007 by Carl Franks
1929  
1930 Based on the original source code of L<DBIx::Class::HTMLWidget>, copyright
1931 Thomas Klausner.
1932  
1933 This library is free software; you can redistribute it and/or modify
1934 it under the same terms as Perl itself, either Perl version 5.8.8 or,
1935 at your option, any later version of Perl 5 you may have available.
1936  
1937  
1938 =cut
↑ Summary Constraint/DBIC/Unique.pm »