File Coverage

blib/lib/Data/MuForm/Field/Display.pm
Criterion Covered Total %
statement 19 20 95.0
branch 7 8 87.5
condition n/a
subroutine 4 5 80.0
pod 1 4 25.0
total 31 37 83.7


line stmt bran cond sub pod time code
1             package Data::MuForm::Field::Display;
2             # ABSTRACT: display only field
3              
4 1     1   562 use Moo;
  1         2  
  1         4  
5             extends 'Data::MuForm::Field';
6              
7              
8             has '+no_update' => ( default => 1 );
9              
10 0     0 0 0 sub no_fif {1}
11 5     5 1 564 sub fif { shift->value }
12              
13             sub base_render_args {
14 5     5 0 85 my $self = shift;
15 5         24 my $args = $self->next::method(@_);
16 5         7 $args->{layout_type} = 'display';
17 5         7 $args->{layout} = 'span';
18 5         4 $args->{no_label} = 1;
19 5         14 return $args;
20             }
21              
22             has 'html' => ( is => 'rw', predicate => 1 );
23              
24             has 'render_method' => ( is => 'rw', predicate => 1 );
25              
26             sub render {
27 6     6 0 25 my $self = shift;
28              
29 6 100       48 return $self->html if $self->has_html;
30 4         20 my $rargs = $self->get_render_args;
31 4 100       21 return $self->render_method->($self, $rargs) if $self->has_render_method;
32 2 50       38 if ( $self->form ) {
33 2         17 my $form_meth = "html_" . $self->convert_full_name($self->full_name);
34 2 100       27 if ( $self->form->can($form_meth) ) {
35 1         21 return $self->form->$form_meth($rargs);
36             }
37             }
38 1         17 return $self->next::method($rargs);
39             }
40              
41             1;
42              
43             __END__
44              
45             =pod
46              
47             =encoding UTF-8
48              
49             =head1 NAME
50              
51             Data::MuForm::Field::Display - display only field
52              
53             =head1 VERSION
54              
55             version 0.04
56              
57             =head1 SYNOPSIS
58              
59             This class can be used for fields that are display only. It will
60             render the value returned by a form's 'html_<field_name>' method,
61             or the field's 'html' attribute.
62              
63             has_field 'explanation' => ( type => 'Display',
64             html => '<p>This is an explanation...</p>' );
65              
66             or in a form:
67              
68             has_field 'explanation' => ( type => 'Display' );
69             sub html_explanation {
70             my ( $self, $field ) = @_;
71             if( $self->something ) {
72             return '<p>This type of explanation...</p>';
73             }
74             else {
75             return '<p>Another type of explanation...</p>';
76             }
77             }
78             #----
79             has_field 'username' => ( type => 'Display' );
80             sub html_username {
81             my ( $self, $field ) = @_;
82             return '<div><b>User:&nbsp;</b>' . $field->value . '</div>';
83             }
84              
85             or set the name of the rendering method:
86              
87             has_field 'explanation' => ( type => 'Display', set_html => 'my_explanation' );
88             sub my_explanation {
89             ....
90             }
91              
92             or provide a 'render_method':
93              
94             has_field 'my_button' => ( type => 'Display', render_method => \&render_my_button );
95             sub render_my_button {
96             my $self = shift;
97             ....
98             return '...';
99             }
100              
101             =head1 AUTHOR
102              
103             Gerda Shank
104              
105             =head1 COPYRIGHT AND LICENSE
106              
107             This software is copyright (c) 2017 by Gerda Shank.
108              
109             This is free software; you can redistribute it and/or modify it under
110             the same terms as the Perl 5 programming language system itself.
111              
112             =cut