lib/Form/Sensible/Renderer/HTML/RenderedForm.pm

filter: Statementstmt 78.8% 138 / 175 Branchbran 55.0% 22 / 40 Conditioncond 40.7% 11 / 27 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 81.4% 22 / 27 Podpod 83.3% 10 / 12 totaltotal 68.9% 204 / 296 CC 39 SCAR 25.9
CC
39
Cov
71%
add_errors_from_validator_result
42.0
render_field
28.5
process_first_template
10.1
CRAP 13.3
« blib/lib/Form/Sensible/Reflector.pm ↑ Summary lib/Form/Sensible/Field/Number.pm »
1 package Form::Sensible::Renderer::HTML::RenderedForm;
2  
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Form::Sensible::DelegateConnection;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 has 'form' => (
11 is => 'rw',
12 isa => 'Form::Sensible::Form',
13 required => 1,
14 #weak_ref => 1,
15 );
16  
17 3 has 'template' => (
18 is => 'rw',
19 isa => 'Template',
20 required => 1,
21 );
22  
23 has 'template_fallback_order' => (
24 is => 'rw',
25 isa => 'ArrayRef[Str]',
26 required => 1,
27 10 ▶ default => sub { return [ shift->form->name ]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 3 lazy => 1,
29 );
30  
31 has 'stash' => (
32 is => 'rw',
33 isa => 'HashRef',
34 required => 1,
35 0 ▶ default => sub { return {}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 3 lazy => 1,
37 );
38  
39 3 has 'css_prefix' => (
40 is => 'rw',
41 isa => 'Str',
42 required => 1,
43 default => 'fs_',
44 );
45  
46 3 has 'form_template_prefix' => (
47 is => 'rw',
48 isa => 'Str',
49 required => 1,
50 builder => '_default_form_template_prefix',
51 lazy => 1,
52 );
53  
54 has 'subform_renderers' => (
55 is => 'rw',
56 isa => 'HashRef',
57 required => 1,
58 1 ▶ default => sub { return {}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 3 lazy => 1
60 );
61  
62  
63 has 'status_messages' => (
64 is => 'rw',
65 isa => 'HashRef[ArrayRef]',
66 required => 1,
67 10 ▶ default => sub { return {}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 3 lazy => 1,
69 );
70  
71 has 'error_messages' => (
72 is => 'rw',
73 isa => 'HashRef[ArrayRef]',
74 required => 1,
75 10 ▶ default => sub { return {}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 3 lazy => 1,
77 );
78  
79 has 'render_hints' => (
80 is => 'rw',
81 isa => 'HashRef',
82 required => 1,
83 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 10 my $self = shift;
85 10 return { %{$self->render_hints_for('HTML', $self->form)} };
86 },
87 3 lazy => 1,
88 );
89  
90 has 'display_name_delegate' => (
91 is => 'rw',
92 isa => 'Form::Sensible::DelegateConnection',
93 required => 1,
94 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 34 ▶ return FSConnector( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 34 my $caller = shift;
97 34 my $display_name = shift;
98 34 my $origin_object = shift;
99
100 ## by default we simply return what we were given
101 34 return $display_name;
102 10 });
103 },
104 3 lazy => 1,
105 coerce => 1,
106 );
107  
108 10 ▶sub _default_form_template_prefix {
Subroutine _default_form_template_prefix: called CC=2 SCAR=6.9
109 10 my $self = shift;
110
111 10 my $hints = $self->render_hints_for('HTML', $self->form);
112 10 ▶ if (exists($hints->{form_template_prefix})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hints->{'form_template_prefix'}) { } 1 9 10
113 1 return $hints->{form_template_prefix};
114 } else {
115 9 return 'form';
116 }
117 }
118  
119 54 ▶sub render_hints_for {
Subroutine render_hints_for: called CC=1 SCAR=0.0
Pod undocumented
120 54 my $self = shift;
121
122 54 return Form::Sensible::Renderer->render_hints_for(@_);
123 }
124  
125 0 ▶sub add_status_message {
Subroutine add_status_message: not called CC=1 SCAR=6.9
Pod documented
126 0 my ($self, $message) = @_;
127
128 0 push @{$self->status_messages}, $message;
129 }
130  
131 0 ▶sub add_error_message {
Subroutine add_error_message: not called CC=2 SCAR=17.9
Pod documented
132 0 my ($self, $fieldname, $message) = @_;
133 #print "in add_error_message for $message\n";
134 0 ▶ if (!exists($self->error_messages->{$fieldname})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->error_messages->{$fieldname}) 0 0 0
135 0 $self->error_messages->{$fieldname} = [];
136 }
137 0 push @{$self->error_messages->{$fieldname}}, $message;
138 }
139  
140 0 ▶sub add_errors_from_validator_result {
Subroutine add_errors_from_validator_result: not called CC=6 SCAR=37.4
Pod documented
141 0 my ($self, $validator_result) = @_;
142
143 0 foreach my $field ($self->form->fieldnames) {
144 0 ▶ if (exists($validator_result->error_fields->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $validator_result->error_fields->{$field}) 0 0 0
145 0 foreach my $message (@{$validator_result->error_fields->{$field}}) {
146 0 $self->add_error_message($field, $message);
147 }
148 }
149 0 ▶ if (exists($validator_result->missing_fields->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $validator_result->missing_fields->{$field}) 0 0 0
150 0 foreach my $message (@{$validator_result->missing_fields->{$field}}) {
151 0 $self->add_error_message($field, $message);
152 }
153 }
154 }
155 }
156  
157 ## render form start using $action as our form action
158 10 ▶sub start {
Subroutine start: called CC=3 SCAR=11.1
Pod documented
159 10 my ($self, $action, $method) = @_;
160
161 10 ▶ if (!$method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 9 1 10
162 9 $method = 'post';
163 }
164 10 my $vars = {
165 'form' => $self->form,
166 'method' => $method,
167 'action' => $action,
168 };
169  
170 10 ▶ if ($self->form->render_hints->{'display_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form->render_hints->{"display_name"}) 0 10 10
171 0 $vars->{'form_display_name'} = $self->display_name_delegate->($self, $self->form->render_hints->{'display_name'}, $self->form);
172 }
173 10 my $output;
174 10 $self->process_first_template($vars, \$output, $self->form_template_prefix . '_start');
175  
176 10 return $output;
177 }
178  
179 ## render all messages - mostly just passes status/error messages to the messages template.
180 10 ▶sub messages {
Subroutine messages: called CC=3 SCAR=12.0
Pod documented
181 10 my ($self, $additionalmessages) = @_;
182
183 ## if we haven't already added error_messages and we have a validator_result in the form
184 ## then we add the errors immediately before processing.
185 10 ▶ if ((!scalar keys %{$self->error_messages}) && defined($self->form->validator_result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar keys %{$self->error_messages;} and defined $self->form->validator_result) 0 10 10
Condition33%1/3
not scalar keys %{$self->error_messages;} and defined $self->form->validator_result
!ll&&!rl&&r
0100
MC/DC0%0/2
not scalar keys %{$self->error_messages;} and defined $self->form->validator_result
not scalar keys %{$self->error_messages;} defined $self->form->validator_result
Truth table33%1/3
not(A) and B
A= not scalar keys %{$self->error_messages;}B= defined $self->form->validator_result
ABresult
0X0
100
111
186 0 $self->add_errors_from_validator_result($self->form->validator_result);
187 }
188
189 10 my $output;
190 10 $self->process_first_template({}, \$output, $self->form_template_prefix . '_messages');
191
192 10 return $output;
193 }
194  
195 ## return the form field names.
196 0 ▶sub fieldnames {
Subroutine fieldnames: not called CC=1 SCAR=6.9
Pod documented
197 0 my ($self) = @_;
198
199 0 return @{$self->form->fieldnames};
200 }
201  
202 ## render all the form fields in the order provided by the form object.
203 10 ▶sub fields {
Subroutine fields: called CC=5 SCAR=17.1
Pod documented
204 10 my ($self, $manual_hints) = @_;
205  
206 10 my @rendered_fields;
207 10 foreach my $field (@{$self->form->field_order}) {
208 35 ▶ my $new_hints = { %{$manual_hints || {}} };
Condition50%1/2
$manual_hints || {}
l!l
035
MC/DC0%0/1
$manual_hints || {}
$manual_hints
Truth table50%1/2
A
A= $manual_hints
Aresult
11
00
209 35 ▶ if ($manual_hints && exists($manual_hints->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($manual_hints and exists $manual_hints->{$field}) 0 35 35
Condition33%1/3
$manual_hints and exists $manual_hints->{$field}
!ll&&!rl&&r
3500
MC/DC0%0/2
$manual_hints and exists $manual_hints->{$field}
$manual_hints exists $manual_hints->{$field}
Truth table33%1/3
A and B
A= $manual_hintsB= exists $manual_hints->{$field}
ABresult
0X0
100
111
210 0 $new_hints = $manual_hints->{$field};
211 }
212 35 my $rendered_field = $self->render_field($field, $new_hints);
213 35 push @rendered_fields, $rendered_field;
214 }
215 10 return join("\n",@rendered_fields);
216 }
217  
218 35 ▶sub render_field {
Subroutine render_field: called CC=15 SCAR=33.5
Pod documented
219 35 my ($self, $fieldname, $manual_hints) = @_;
220  
221 35 my $field = $self->form->field($fieldname);
222 35 my $fieldtype = $field->field_type;
223
224 35 ▶ if (exists($self->subform_renderers->{$fieldname})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->subform_renderers->{$fieldname}) { } 1 34 35
225 ## handle fields that are subforms.
226 1 return $self->subform_renderers->{$fieldname}->complete(undef, $manual_hints);
227 } else {
228 34 ▶ my %custom_vars = %{$manual_hints->{stash_vars} || {}};
Condition50%1/2
$manual_hints->{'stash_vars'} || {}
l!l
034
MC/DC0%0/1
$manual_hints->{'stash_vars'} || {}
$manual_hints->{'stash_vars'}
Truth table50%1/2
A
A= $manual_hints->{'stash_vars'}
Aresult
11
00
229 34 my $vars = {
230 'form' => $self->form,
231 'field' => $field,
232 'field_name' => $fieldname,
233 'field_display_name' => $self->display_name_delegate->($self, $field->display_name, $field),
234 %custom_vars,
235 };
236
237 ## if we have field-specific render_hints, we have to add them
238 ## ourselves. First we check any already-set render_hints,
239 ## Then, check render_hints for this renderer
240 ## Then, any manual hints added in the template.
241 34 ▶ $vars->{render_hints} = { %{$self->render_hints}, %{$self->render_hints_for('HTML', $field)}, %{$manual_hints||{}} };
Condition50%1/2
$manual_hints || {}
l!l
340
MC/DC0%0/1
$manual_hints || {}
$manual_hints
Truth table50%1/2
A
A= $manual_hints
Aresult
11
00
242  
243 34 ▶ if (exists($self->error_messages->{$fieldname}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->error_messages->{$fieldname} and ref $self->error_messages->{$fieldname} eq "ARRAY" and $#{$self->error_messages->{$fieldname};} > -1) 0 34 34
Condition33%2/6
exists $self->error_messages->{$fieldname} and ref $self->error_messages->{$fieldname} eq "ARRAY" and $#{$self->error_messages->{$fieldname};} > -1
!ll&&!rl&&r
3400
exists $self->error_messages->{$fieldname} and ref $self->error_messages->{$fieldname} eq "ARRAY"
!ll&&!rl&&r
3400
MC/DC0%0/3
exists $self->error_messages->{$fieldname} and ref $self->error_messages->{$fieldname} eq "ARRAY" and $#{$self->error_messages->{$fieldname};} > -1
exists $self->error_messages->{$fieldname} ref $self->error_messages->{$fieldname} eq "ARRAY" $#{$self->error_messages->{$fieldname};} > -1
Truth table25%1/4
A and B and C
A= exists $self->error_messages->{$fieldname}B= ref $self->error_messages->{$fieldname} eq "ARRAY"C= $#{$self->error_messages->{$fieldname};} > -1
ABCresult
0XX0
10X0
1100
1111
244 ref($self->error_messages->{$fieldname}) eq 'ARRAY' &&
245 0 $#{$self->error_messages->{$fieldname}} > -1) {
246 0 $vars->{has_errors} = 1;
247 }
248
249 34 ▶ if (exists($self->status_messages->{$fieldname}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->status_messages->{$fieldname} and ref $self->status_messages->{$fieldname} eq "ARRAY" and $#{$self->status_messages->{$fieldname};} > -1) 0 34 34
Condition33%2/6
exists $self->status_messages->{$fieldname} and ref $self->status_messages->{$fieldname} eq "ARRAY" and $#{$self->status_messages->{$fieldname};} > -1
!ll&&!rl&&r
3400
exists $self->status_messages->{$fieldname} and ref $self->status_messages->{$fieldname} eq "ARRAY"
!ll&&!rl&&r
3400
MC/DC0%0/3
exists $self->status_messages->{$fieldname} and ref $self->status_messages->{$fieldname} eq "ARRAY" and $#{$self->status_messages->{$fieldname};} > -1
exists $self->status_messages->{$fieldname} ref $self->status_messages->{$fieldname} eq "ARRAY" $#{$self->status_messages->{$fieldname};} > -1
Truth table25%1/4
A and B and C
A= exists $self->status_messages->{$fieldname}B= ref $self->status_messages->{$fieldname} eq "ARRAY"C= $#{$self->status_messages->{$fieldname};} > -1
ABCresult
0XX0
10X0
1100
1111
250 ref($self->status_messages->{$fieldname}) eq 'ARRAY' &&
251 0 $#{$self->status_messages->{$fieldname}} > -1) {
252 0 $vars->{has_status_messages} = 1;
253 }
254
255 ## allow render_hints to override field type - allowing a number to be rendered
256 ## as a select with a range, etc. also allows text to be rendered as 'hidden'
257 34 ▶ if (exists($vars->{'render_hints'}) && exists($vars->{'render_hints'}{'field_type'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $vars->{"render_hints"} and exists $vars->{"render_hints"}{"field_type"}) 10 24 34
Condition66%2/3
exists $vars->{"render_hints"} and exists $vars->{"render_hints"}{"field_type"}
!ll&&!rl&&r
02410
MC/DC50%1/2
exists $vars->{"render_hints"} and exists $vars->{"render_hints"}{"field_type"}
exists $vars->{"render_hints"} exists $vars->{"render_hints"}{"field_type"}
Truth table66%2/3
A and B
A= exists $vars->{"render_hints"}B= exists $vars->{"render_hints"}{"field_type"}
ABresult
0X0
100
111
258 10 $fieldtype = $vars->{'render_hints'}{'field_type'};
259 }
260
261 34 $vars->{'field_type'} = $fieldtype;
262
263 ## Order for trying templates should be:
264 ## formname/fieldname_field
265 ## formname/fieldtype
266 ## fieldname_field
267 ## fieldtype
268  
269 34 my $output;
270
271 34 my $wrapper = "field_wrapper.tt";
272 34 ▶ if (exists($vars->{'render_hints'}->{'field_wrapper'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $vars->{"render_hints"}{"field_wrapper"}) 0 34 34
273 0 $wrapper = $vars->{'render_hints'}->{'field_wrapper'};
274 }
275 34 ▶ if ($wrapper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrapper) 34 0 34
276 34 $vars->{'FS_wrapper'} = $wrapper;
277 }
278
279 34 $self->process_first_template($vars, \$output, $fieldname . "_field", $fieldtype );
280
281 # this code exists to retain compatibility with the old 'wrap_fields_with' field wrapping.
282 # The old 'wrap_fields_with' wrapped the entire rendered field with label. This is inappropriate
283 # in most cases and thus it has been replaced with real Template::Toolkit WRAPPER functionality.
284 # This code exists ONLY for backwards compatibility and will be removed.
285 34 ▶ if (exists($vars->{'render_hints'}->{'wrap_fields_with'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $vars->{"render_hints"}{"wrap_fields_with"}) 0 34 34
286 # Have to prevent re-wrapping of the wrapper provided.
287 0 $vars->{'FS_wrapper'} = undef;
288 0 my $wrapper_output;
289 0 my %wrapper_vars = ( %{$vars}, field_output => $output );
290 0 $self->process_first_template(\%wrapper_vars, \$wrapper_output, $vars->{'render_hints'}->{'wrap_fields_with'} );
291 0 return $wrapper_output;
292 }
293
294 34 return $output;
295
296 }
297 }
298  
299 ## pass in the vars / output / template_names to use. This method handles automatic fallback
300 ## of templates from most specific to least specific.
301  
302 64 ▶sub process_first_template {
Subroutine process_first_template: called CC=10 SCAR=23.2
Pod undocumented
303 64 my $self = shift;
304 64 my $vars = shift;
305 64 my $output = shift;
306 64 my @template_names = @_;
307
308 ## prefill anything provided already into the stash
309 64 my $stash_vars = { %{$self->stash } };
310
311 64 ▶ if (!exists($stash_vars->{'render_hints'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $stash_vars->{"render_hints"}) 64 0 64
312 64 $stash_vars->{'render_hints'} = $self->render_hints;
313 }
314
315 64 $stash_vars->{'form'} = $self->form;
316 64 $stash_vars->{'error_messages'} = $self->error_messages;
317 64 $stash_vars->{'status_messages'} = $self->status_messages;
318 64 $stash_vars->{'css_prefix'} = $self->css_prefix;
319
320 ## copy the vars array into the stash_vars
321 64 foreach my $key (keys %{$vars}) {
322 268 $stash_vars->{$key} = $vars->{$key};
323 }
324
325 64 my @templates_to_try;
326
327 64 foreach my $path (@{$self->template_fallback_order}) {
328 64 foreach my $template_name (@template_names) {
329 98 push @templates_to_try, $path . '/' . $template_name;
330 }
331 }
332
333 64 push @templates_to_try, @template_names;
334
335 64 my $template_found = 0;
336 64 foreach my $template_name (@templates_to_try) {
337 196 ▶ if ($template_name !~ /.tt$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template_name =~ /.tt$/) 196 0 196
338 196 $template_name .= '.tt';
339 }
340 196 my $res = $self->template->process($template_name, $stash_vars, $output);
341 196 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 64 132 196
342 64 $template_found = 1;
343 64 last;
344 } else {
345 132 my $error = $self->template->error();
346 132 ▶ if ($error->info =~ /parse error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error->info =~ /parse error/) 0 132 132
347 0 croak 'Error processing ' . $template_name . ': ' . $error;
348 }
349 }
350 }
351
352 64 ▶ if (!$template_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template_found) 0 64 64
353 ## crap. throw an error or something, we couldn't find ANY matching template.
354 # croak "Unable to find any template for processing, tried: " . join(", ", @templates_to_try) . " in " . join(':',@{$self->template});
355 }
356 64 return $output;
357 }
358  
359 ## render end of form. Probably just </form> most of the time.
360 10 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod documented
361 10 my ($self) = @_;
362
363 10 my $output;
364
365 10 $self->process_first_template({}, \$output, $self->form_template_prefix . '_end');
366  
367 10 return $output;
368 }
369  
370 7 ▶sub complete {
Subroutine complete: called CC=1 SCAR=0.0
Pod documented
371 7 my ($self, $action, $method, $manual_hints) = @_;
372
373 7 return join('', $self->start($action, $method), $self->messages, $self->fields($manual_hints), $self->end);
374 }
375  
376 3 __PACKAGE__->meta->make_immutable;
377 3 1;
378  
379 __END__
« blib/lib/Form/Sensible/Reflector.pm ↑ Summary lib/Form/Sensible/Field/Number.pm »