File Coverage

blib/lib/HTML/FormHandler/InitResult.pm
Criterion Covered Total %
statement 86 89 96.6
branch 52 72 72.2
condition 39 57 68.4
subroutine 7 7 100.0
pod 0 1 0.0
total 184 226 81.4


line stmt bran cond sub pod time code
1             package HTML::FormHandler::InitResult;
2             # ABSTRACT: internal code
3             $HTML::FormHandler::InitResult::VERSION = '0.40067';
4 141     141   98900 use Moose::Role;
  141         247  
  141         1157  
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 445     445   754 my ( $self, $self_result ) = @_;
12              
13             # defaults for compounds, etc.
14 445 100       2342 if ( my @values = $self->get_default_value ) {
15 7 50       591 my $value = @values > 1 ? \@values : shift @values;
16 7 50 66     33 if( ref $value eq 'HASH' || blessed $value ) {
17 7         36 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 438         604 my $my_value;
23 438         4157 for my $field ( $self->sorted_fields ) {
24 1284 50 66     30408 next if ($field->inactive && !$field->_active);
25 1284         28961 my $result = HTML::FormHandler::Field::Result->new(
26             name => $field->name,
27             parent => $self_result
28             );
29 1284         5366 $result = $field->_result_from_fields($result);
30 1284 100       35773 $my_value->{ $field->name } = $result->value if $result->has_value;
31 1284 50       40319 $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 438 100       3345 $self_result->_set_value($my_value) if ( keys %$my_value );
37 438         12969 $self->_set_result($self_result);
38 438 100       8719 $self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
39 438         195573 return $self_result;
40             }
41              
42             # building fields from input (params)
43             # formerly done in validate_field
44             sub _result_from_input {
45 262     262   474 my ( $self, $self_result, $input, $exists ) = @_;
46              
47             # transfer the input values to the input attributes of the
48             # subfields
49 262 0 33     887 return unless ( defined $input || $exists || $self->has_fields );
      33        
50 262         8259 $self_result->_set_input($input);
51 262 50       809 if ( ref $input eq 'HASH' ) {
52 262         1036 foreach my $field ( $self->sorted_fields ) {
53 821 50 66     19315 next if ($field->inactive && !$field->_active);
54 821         18500 my $field_name = $field->name;
55 821         20559 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 821   66     22103 exists $input->{$field->input_param || $field_name} );
      66        
62 821 50       26177 $self_result->add_result($result) if $result;
63             }
64             }
65 262         7058 $self->_set_result($self_result);
66 262 100       4434 $self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
67 262         98305 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   190 my ( $self, $self_result, $item ) = @_;
74              
75 115 50 33     437 return unless ( $item || $self->has_fields ); # empty fields for compounds
76 115         135 my $my_value;
77 115         1422 my $init_obj = $self->form->init_object;
78 115         513 for my $field ( $self->sorted_fields ) {
79 333 50 33     7675 next if ( $field->inactive && !$field->_active );
80 333         7447 my $result = HTML::FormHandler::Field::Result->new(
81             name => $field->name,
82             parent => $self_result
83             );
84 333 100 100     9427 if ( (ref $item eq 'HASH' && !exists $item->{ $field->accessor } ) ||
      100        
      66        
85             ( blessed($item) && !$item->can($field->accessor) ) ) {
86 58         65 my $found = 0;
87 58 100       1374 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         59 my @names = split( /\./, $field->full_name );
91 6         27 my $init_obj_value = $self->find_sub_item( $init_obj, \@names );
92 6 100       15 if ( defined $init_obj_value ) {
93 4         4 $found = 1;
94 4         21 $result = $field->_result_from_object( $result, $init_obj_value );
95             }
96             }
97 58 100       227 $result = $field->_result_from_fields($result) unless $found;
98             }
99             else {
100 275 50       7037 my $value = $self->_get_value( $field, $item ) unless $field->writeonly;
101 275         837 $result = $field->_result_from_object( $result, $value );
102             }
103 333 50       10658 $self_result->add_result($result) if $result;
104 333         1031 $my_value->{ $field->name } = $field->value;
105             }
106 115         3408 $self_result->_set_value($my_value);
107 115         2958 $self->_set_result($self_result);
108 115 100       1965 $self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
109 115         40802 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 10 my ( $self, $item, $field_name_array ) = @_;
117 7         15 my $this_fname = shift @$field_name_array;;
118 7         33 my $field = $self->field($this_fname);
119 7         25 my $new_item = $self->_get_value( $field, $item );
120 7 50       23 if ( scalar @$field_name_array ) {
121 0         0 $new_item = $field->find_sub_item( $new_item, $field_name_array );
122             }
123 7         15 return $new_item;
124             }
125              
126             sub _get_value {
127 282     282   339 my ( $self, $field, $item ) = @_;
128              
129 282         6331 my $accessor = $field->accessor;
130 282         294 my @values;
131 282 100 66     7304 if( defined $field->default_over_obj ) {
    100 100        
    100 66        
    100          
    50          
132 3         68 @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         305 @values = $item->$accessor;
139             # for non-DBIC blessed object where access returns arrayref
140 17 100 66     186 if ( scalar @values == 1 && ref $values[0] eq 'ARRAY' && $field->has_flag('multiple') ) {
      66        
141 1         1 @values = @{$values[0]};
  1         3  
142             }
143             }
144             elsif ( exists $item->{$accessor} ) {
145 258         373 my $v = $item->{$accessor};
146 258 100 100     667 if($field->has_flag('multiple') && ref($v) eq 'ARRAY'){
147 3         11 @values = @$v;
148             } else {
149 255         433 @values = $v;
150             }
151             }
152             elsif ( @values = $field->get_default_value ) {
153             }
154             else {
155 2         5 return;
156             }
157 280 100       8901 if( $field->has_inflate_default_method ) {
158 9         355 @values = $field->inflate_default(@values);
159             }
160 280         333 my $value;
161 280 100       537 if( $field->has_flag('multiple')) {
162 7 100 100     33 $value = scalar @values == 1 && ! defined $values[0] ? [] : \@values;
163             }
164             else {
165 273 50       602 $value = @values > 1 ? \@values : shift @values;
166             }
167 280         477 return $value;
168             }
169              
170 141     141   631677 use namespace::autoclean;
  141         250  
  141         1383  
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.40067
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) 2016 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