File Coverage

blib/lib/HTML/FormHandler/InitResult.pm
Criterion Covered Total %
statement 86 89 96.6
branch 52 72 72.2
condition 40 57 70.1
subroutine 7 7 100.0
pod 0 1 0.0
total 185 226 81.8


line stmt bran cond sub pod time code
1             package HTML::FormHandler::InitResult;
2             # ABSTRACT: internal code
3             $HTML::FormHandler::InitResult::VERSION = '0.40068';
4 143     143   132642 use Moose::Role;
  143         472  
  143         1468  
5              
6              
7             # _init is for building fields when
8             # there is no initial object and no params
9             # formerly _init
10             sub _result_from_fields {
11 448     448   1659 my ( $self, $self_result ) = @_;
12              
13             # defaults for compounds, etc.
14 448 100       3188 if ( my @values = $self->get_default_value ) {
15 7 50       855 my $value = @values > 1 ? \@values : shift @values;
16 7 50 66     43 if( ref $value eq 'HASH' || blessed $value ) {
17 7         57 return $self->_result_from_object( $self_result, $value );
18             }
19 0 0       0 $self->init_value($value) if defined $value;
20 0 0       0 $self_result->_set_value($value) if defined $value;
21             }
22 441         1163 my $my_value;
23 441         5833 for my $field ( $self->sorted_fields ) {
24 1292 50 66     34560 next if ($field->inactive && !$field->_active);
25 1292         33582 my $result = HTML::FormHandler::Field::Result->new(
26             name => $field->name,
27             parent => $self_result
28             );
29 1292         7651 $result = $field->_result_from_fields($result);
30 1292 100       39960 $my_value->{ $field->name } = $result->value if $result->has_value;
31 1292 50       45096 $self_result->add_result($result) if $result;
32             }
33             # setting value here to handle disabled compound fields, where we want to
34             # preserve the 'value' because the fields aren't submitted...except for the
35             # form. Not sure it's the best idea to skip for form, but it maintains previous behavior
36 441 100       4255 $self_result->_set_value($my_value) if ( keys %$my_value );
37 441         15196 $self->_set_result($self_result);
38 441 100       12639 $self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
39 441         285506 return $self_result;
40             }
41              
42             # building fields from input (params)
43             # formerly done in validate_field
44             sub _result_from_input {
45 264     264   939 my ( $self, $self_result, $input, $exists ) = @_;
46              
47             # transfer the input values to the input attributes of the
48             # subfields
49 264 0 33     1314 return unless ( defined $input || $exists || $self->has_fields );
      33        
50 264         9196 $self_result->_set_input($input);
51 264 50       1120 if ( ref $input eq 'HASH' ) {
52 264         1492 foreach my $field ( $self->sorted_fields ) {
53 827 50 66     21932 next if ($field->inactive && !$field->_active);
54 827         20751 my $field_name = $field->name;
55 827         23019 my $result = HTML::FormHandler::Field::Result->new(
56             name => $field_name,
57             parent => $self_result
58             );
59             $result =
60             $field->_result_from_input( $result, $input->{$field->input_param || $field_name},
61 827   66     24660 exists $input->{$field->input_param || $field_name} );
      66        
62 827 50       28833 $self_result->add_result($result) if $result;
63             }
64             }
65 264         7995 $self->_set_result($self_result);
66 264 100       5222 $self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
67 264         142668 return $self_result;
68             }
69              
70             # building fields from model object or init_obj hash
71             # formerly _init_from_object
72             sub _result_from_object {
73 115     115   838 my ( $self, $self_result, $item ) = @_;
74              
75 115 50 33     535 return unless ( $item || $self->has_fields ); # empty fields for compounds
76 115         273 my $my_value;
77 115         1682 my $init_obj = $self->form->init_object;
78 115         754 for my $field ( $self->sorted_fields ) {
79 333 50 33     8806 next if ( $field->inactive && !$field->_active );
80 333         8150 my $result = HTML::FormHandler::Field::Result->new(
81             name => $field->name,
82             parent => $self_result
83             );
84 333 100 100     10167 if ( (ref $item eq 'HASH' && !exists $item->{ $field->accessor } ) ||
      100        
      100        
85             ( blessed($item) && !$item->can($field->accessor) ) ) {
86 58         133 my $found = 0;
87 58 100       1543 if ($field->form->use_init_obj_when_no_accessor_in_item) {
88             # if we're using an item, look for accessor not found in item
89             # in the init_object
90 6         87 my @names = split( /\./, $field->full_name );
91 6         32 my $init_obj_value = $self->find_sub_item( $init_obj, \@names );
92 6 100       40 if ( defined $init_obj_value ) {
93 4         12 $found = 1;
94 4         31 $result = $field->_result_from_object( $result, $init_obj_value );
95             }
96             }
97 58 100       352 $result = $field->_result_from_fields($result) unless $found;
98             }
99             else {
100 275 50       7753 my $value = $self->_get_value( $field, $item ) unless $field->writeonly;
101 275         1221 $result = $field->_result_from_object( $result, $value );
102             }
103 333 50       11729 $self_result->add_result($result) if $result;
104 333         1472 $my_value->{ $field->name } = $field->value;
105             }
106 115         3594 $self_result->_set_value($my_value);
107 115         3323 $self->_set_result($self_result);
108 115 100       2347 $self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
109 115         56983 return $self_result;
110             }
111              
112             # this is used for reloading repeatables form the database if they've changed and
113             # for finding field values in the init_object when we have an item and the
114             # 'use_init_obj_when_no_accessor_in_item' flag is set
115             sub find_sub_item {
116 7     7 0 29 my ( $self, $item, $field_name_array ) = @_;
117 7         24 my $this_fname = shift @$field_name_array;;
118 7         51 my $field = $self->field($this_fname);
119 7         34 my $new_item = $self->_get_value( $field, $item );
120 7 50       30 if ( scalar @$field_name_array ) {
121 0         0 $new_item = $field->find_sub_item( $new_item, $field_name_array );
122             }
123 7         23 return $new_item;
124             }
125              
126             sub _get_value {
127 282     282   767 my ( $self, $field, $item ) = @_;
128              
129 282         6958 my $accessor = $field->accessor;
130 282         590 my @values;
131 282 100 66     7739 if( defined $field->default_over_obj ) {
    100 100        
    100 66        
    100          
    50          
132 3         73 @values = $field->default_over_obj;
133             }
134             elsif( $field->form && $field->form->use_defaults_over_obj && ( @values = $field->get_default_value ) ) {
135             }
136             elsif ( blessed($item) && $item->can($accessor) ) {
137             # this must be an array, so that DBIx::Class relations are arrays not resultsets
138 17         346 @values = $item->$accessor;
139             # for non-DBIC blessed object where access returns arrayref
140 17 100 66     253 if ( scalar @values == 1 && ref $values[0] eq 'ARRAY' && $field->has_flag('multiple') ) {
      66        
141 1         3 @values = @{$values[0]};
  1         4  
142             }
143             }
144             elsif ( exists $item->{$accessor} ) {
145 258         657 my $v = $item->{$accessor};
146 258 100 100     1022 if($field->has_flag('multiple') && ref($v) eq 'ARRAY'){
147 3         16 @values = @$v;
148             } else {
149 255         736 @values = $v;
150             }
151             }
152             elsif ( @values = $field->get_default_value ) {
153             }
154             else {
155 2         8 return;
156             }
157 280 100       9728 if( $field->has_inflate_default_method ) {
158 9         364 @values = $field->inflate_default(@values);
159             }
160 280         660 my $value;
161 280 100       836 if( $field->has_flag('multiple')) {
162 7 100 100     51 $value = scalar @values == 1 && ! defined $values[0] ? [] : \@values;
163             }
164             else {
165 273 50       944 $value = @values > 1 ? \@values : shift @values;
166             }
167 280         801 return $value;
168             }
169              
170 143     143   915576 use namespace::autoclean;
  143         436  
  143         1623  
171             1;
172              
173             __END__
174              
175             =pod
176              
177             =encoding UTF-8
178              
179             =head1 NAME
180              
181             HTML::FormHandler::InitResult - internal code
182              
183             =head1 VERSION
184              
185             version 0.40068
186              
187             =head1 SYNOPSIS
188              
189             Internal role for initializing the result objects.
190              
191             =head1 AUTHOR
192              
193             FormHandler Contributors - see HTML::FormHandler
194              
195             =head1 COPYRIGHT AND LICENSE
196              
197             This software is copyright (c) 2017 by Gerda Shank.
198              
199             This is free software; you can redistribute it and/or modify it under
200             the same terms as the Perl 5 programming language system itself.
201              
202             =cut