Catalyst/Controller/HTML/FormFu.pm

filter: Statementstmt 90.9% 111 / 122 Branchbran 56.8% 25 / 44 Conditioncond 41.6% 5 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 18 / 18 Podpod 50.0% 1 / 2 totaltotal 78.9% 161 / 204 CC 30 SCAR 19.6
CC
30
Cov
79%
_common_construction
16.7
__ANON__
8.7
__ANON__
5.9
CRAP 7.1
« Catalyst/Controller/HTML/FormFu/Action/MultiFormConfig.pm ↑ Summary Catalyst/Controller/HTML/FormFu/Action/FormConfig.pm »
1 package Catalyst::Controller::HTML::FormFu;
2  
3 # ABSTRACT: Catalyst integration for HTML::FormFu
4  
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 our $VERSION = '2.04'; # VERSION
8 9 our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY
9  
10 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use HTML::FormFu;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Regexp::Assemble;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Scalar::Util qw/ isweak weaken /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # see https://rt.cpan.org/Ticket/Display.html?id=55780
21 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 extends 'Catalyst::Controller';
23 }
24  
25 9 with 'Catalyst::Component::InstancePerContext';
26  
27 9 has _html_formfu_config => ( is => 'rw' );
28  
29 124 ▶sub build_per_context_instance {
Subroutine build_per_context_instance: called CC=3 SCAR=11.0
Pod undocumented
30 124 my ( $self, $c ) = @_;
31 124 ▶ return $self unless ( ref $c );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c 6 118 124
32 118 $self->{c} = $c;
33  
34 weaken( $self->{c} )
35 118 ▶ if !isweak( $self->{c} );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::is_weak($self->{"c"}) 118 0 118
36  
37 118 return $self;
38 }
39  
40 54 ▶sub BUILD { }
Subroutine BUILD: called CC=1 SCAR=0.0
41  
42 54 ▶after BUILD => sub {
Subroutine __ANON__: called CC=8 SCAR=21.7
43 54 my ($self) = @_;
44  
45 54 my $app = $self->_app;
46 54 ▶ my $self_config = $self->config->{'Controller::HTML::FormFu'} || {};
Condition100%2/2
$self->config->{'Controller::HTML::FormFu'} || {}
l!l
2727
MC/DC100%1/1
$self->config->{'Controller::HTML::FormFu'} || {}
$self->config->{'Controller::HTML::FormFu'}
Truth table100%2/2
A
A= $self->config->{'Controller::HTML::FormFu'}
Aresult
11
00
47 54 ▶ my $parent_config = $app->config->{'Controller::HTML::FormFu'} || {};
Condition50%1/2
$app->config->{'Controller::HTML::FormFu'} || {}
l!l
540
MC/DC0%0/1
$app->config->{'Controller::HTML::FormFu'} || {}
$app->config->{'Controller::HTML::FormFu'}
Truth table50%1/2
A
A= $app->config->{'Controller::HTML::FormFu'}
Aresult
11
00
48  
49 54 my %defaults = (
50 request_token_enable => 0,
51 request_token_field_name => '_token',
52 request_token_session_key => '__token',
53 request_token_expiration_time => 3600,
54 form_method => 'form',
55 form_stash => 'form',
56 form_attr => 'Form',
57 config_attr => 'FormConfig',
58 method_attr => 'FormMethod',
59 form_action => "Catalyst::Controller::HTML::FormFu::Action::Form",
60 config_action => "Catalyst::Controller::HTML::FormFu::Action::FormConfig",
61 method_action => "Catalyst::Controller::HTML::FormFu::Action::FormMethod",
62  
63 multiform_method => 'multiform',
64 multiform_stash => 'multiform',
65 multiform_attr => 'MultiForm',
66 multiform_config_attr => 'MultiFormConfig',
67 multiform_method_attr => 'MultiFormMethod',
68 multiform_action => "Catalyst::Controller::HTML::FormFu::Action::MultiForm",
69 multiform_config_action => "Catalyst::Controller::HTML::FormFu::Action::MultiFormConfig",
70 multiform_method_action => "Catalyst::Controller::HTML::FormFu::Action::MultiFormMethod",
71  
72 context_stash => 'context',
73  
74 model_stash => {},
75  
76 constructor => {},
77 multiform_constructor => {},
78  
79 config_callback => 1,
80 );
81  
82 54 my %args = ( %defaults, %$parent_config, %$self_config );
83  
84 54 my $local_path = $app->path_to( 'root', 'formfu' );
85  
86 54 ▶ if ( !exists $args{constructor}{tt_args}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{"constructor"}{"tt_args"} or not exists $args{"constructor"}{"tt_args"}{"INCLUDE_PATH"} and -d $local_path) 54 0 54
Condition16%1/6
not exists $args{"constructor"}{"tt_args"} or not exists $args{"constructor"}{"tt_args"}{"INCLUDE_PATH"} and -d $local_path
l!l&&r!l&&!r
5400
not exists $args{"constructor"}{"tt_args"}{"INCLUDE_PATH"} and -d $local_path
!ll&&!rl&&r
000
MC/DC0%0/3
not exists $args{"constructor"}{"tt_args"} or not exists $args{"constructor"}{"tt_args"}{"INCLUDE_PATH"} and -d $local_path
not exists $args{"constructor"}{"tt_args"} not exists $args{"constructor"}{"tt_args"}{"INCLUDE_PATH"} -d $local_path
Truth table25%1/4
not(A) or not(B) and C
A= not exists $args{"constructor"}{"tt_args"}B= not exists $args{"constructor"}{"tt_args"}{"INCLUDE_PATH"}C= -d $local_path
ABCresult
1XX1
0111
00X0
0100
87 || !exists $args{constructor}{tt_args}{INCLUDE_PATH} && -d $local_path ) {
88 54 $args{constructor}{tt_args}{INCLUDE_PATH} = [$local_path];
89 }
90  
91 54 ▶ $args{constructor}{query_type} ||= 'Catalyst';
Condition50%1/2
$args{"constructor"}{"query_type"} ||= "Catalyst"
l!l
054
MC/DC0%0/1
$args{"constructor"}{"query_type"} ||= "Catalyst"
$args{"constructor"}{"query_type"}
Truth table50%1/2
A
A= $args{"constructor"}{"query_type"}
Aresult
11
00
92  
93 54 ▶ if ( !exists $args{constructor}{config_file_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"constructor"}{"config_file_path"}) 54 0 54
94 54 $args{constructor}{config_file_path} = $app->path_to( 'root', 'forms' );
95 }
96  
97 # build regexp of file extensions
98 54 my $regex_builder = Regexp::Assemble->new;
99  
100 54 map { $regex_builder->add($_) } Config::Any->extensions;
101  
102 54 $args{_file_ext_regex} = $regex_builder->re;
103  
104 # save config for use by action classes
105 54 $self->_html_formfu_config( \%args );
106  
107 # add controller methods
108 9 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict);
Subroutine BEGIN: called CC=1 SCAR=0.0
109 54 *{"$args{form_method}"} = \&_form;
110 54 *{"$args{multiform_method}"} = \&_multiform;
111 9 };
112  
113 34 ▶sub _form {
Subroutine _form: called CC=3 SCAR=11.1
114 34 my $self = shift;
115 34 my $config = $self->_html_formfu_config;
116 0 ▶ my $form = HTML::FormFu->new( { %{ $self->_html_formfu_config->{constructor} }, ( @_ ? %{ $_[0] } : () ), } );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 34 34
117  
118 34 $self->_common_construction($form);
119  
120 34 ▶ if ( $config->{request_token_enable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"request_token_enable"}) 26 8 34
121 $form->plugins(
122 { type => 'RequestToken',
123 context => $config->{context_stash},
124 field_name => $config->{request_token_field_name},
125 session_key => $config->{request_token_session_key},
126 expiration_time => $config->{request_token_expiration_time}
127 }
128 26 );
129 }
130  
131 34 return $form;
132 }
133  
134 6 ▶sub _multiform {
Subroutine _multiform: called CC=2 SCAR=7.1
135 6 my $self = shift;
136  
137 6 require HTML::FormFu::MultiForm;
138  
139 my $multi = HTML::FormFu::MultiForm->new(
140 6 { %{ $self->_html_formfu_config->{constructor} },
141 6 ▶ %{ $self->_html_formfu_config->{multiform_constructor} },
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 6 6
142 0 ( @_ ? %{ $_[0] } : () ),
143 }
144 );
145  
146 6 $self->_common_construction($multi);
147  
148 6 return $multi;
149 }
150  
151 40 ▶sub _common_construction {
Subroutine _common_construction: called CC=11 SCAR=28.2
152 40 my ( $self, $form ) = @_;
153  
154 40 ▶ croak "form or multi arg required" if !defined $form;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $form 0 40 40
155  
156 40 $form->query( $self->{c}->request );
157  
158 40 my $config = $self->_html_formfu_config;
159  
160 40 ▶ if ( $config->{config_callback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"config_callback"}) 40 0 40
161 $form->config_callback(
162 100 ▶ { plain_value => sub {
Subroutine __ANON__: called CC=5 SCAR=17.8
163 100 ▶ return if !defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 100 100
164 100 ▶ s{__uri_for\((.+?)\)__}
Branch: 1 branch
BranchTrueFalseTotal
if /__uri_for\(/ 5 95 100
165 5 { $self->{c}->uri_for( split( '\s*,\s*', $1 ) ) }eg
166 if /__uri_for\(/;
167  
168 100 ▶ s{__path_to\(\s*(.+?)\s*\)__}
Branch: 1 branch
BranchTrueFalseTotal
if /__path_to\(/ 0 100 100
169 0 { $self->{c}->path_to( split( '\s*,\s*', $1 ) ) }eg
170 if /__path_to\(/;
171  
172 100 ▶ s{__config\((.+?)\)__}
Branch: 1 branch
BranchTrueFalseTotal
if /__config\(/ 0 100 100
173 0 { $self->{c}->config->{$1} }eg
174 if /__config\(/;
175 }
176 }
177 40 );
178  
179 weaken( $self->{c} )
180 40 ▶ if !isweak( $self->{c} );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::is_weak($self->{"c"}) 0 40 40
181 }
182  
183 40 ▶ if ( $config->{languages_from_context} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"languages_from_context"}) 0 40 40
184 0 $form->languages( $self->{c}->languages );
185 }
186  
187 40 ▶ if ( $config->{localize_from_context} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"localize_from_context"}) 0 40 40
188 0 $form->add_localize_object( $self->{c} );
189 }
190  
191 40 ▶ if ( $config->{default_action_use_name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config->{'default_action_use_name'}) { } 0 40 40
elsif ($config->{'default_action_use_path'}) { } 40 0 40
192 0 my $action = $self->{c}->uri_for( $self->{c}->{action}->name );
193  
194 $self->{c}->log->debug("FormFu - Setting default action by name: $action")
195 0 ▶ if $self->{c}->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"c"}->debug 0 0 0
196  
197 0 $form->action($action);
198 }
199 elsif ( $config->{default_action_use_path} ) {
200 40 my $action = $self->{c}->{request}->base . $self->{c}->{request}->path;
201  
202 $self->{c}->log->debug("FormFu - Setting default action by path: $action")
203 40 ▶ if $self->{c}->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"c"}->debug 0 40 40
204  
205 40 $form->action($action);
206 }
207  
208 40 my $context_stash = $config->{context_stash};
209 40 $form->stash->{$context_stash} = $self->{c};
210 40 weaken( $form->stash->{$context_stash} );
211  
212 40 my $model_stash = $config->{model_stash};
213  
214 40 for my $model ( keys %$model_stash ) {
215 0 $form->stash->{$model} = $self->{c}->model( $model_stash->{$model} );
216 }
217  
218 40 return;
219 }
220  
221 459 ▶sub create_action {
Subroutine create_action: called CC=4 SCAR=13.9
Pod documented
222 459 my $self = shift;
223 459 my %args = @_;
224  
225 459 my $config = $self->_html_formfu_config;
226  
227 459 for my $type (
228 qw/
229 form
230 config
231 method
232 multiform
233 multiform_config
234 multiform_method /
235 ) {
236 2484 my $attr = $config->{"${type}_attr"};
237  
238 2484 ▶ if ( exists $args{attributes}{$attr} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'attributes'}{$attr}) { } 99 2385 2484
elsif (exists $args{'attributes'}{"$attr()"}) { } 0 2385 2385
239 99 $args{_attr_params} = delete $args{attributes}{$attr};
240 }
241 elsif ( exists $args{attributes}{"$attr()"} ) {
242 0 $args{_attr_params} = delete $args{attributes}{"$attr()"};
243 }
244 else {
245 2385 next;
246 }
247  
248 99 push @{ $args{attributes}{ActionClass} }, $config->{"${type}_action"};
249 99 last;
250 }
251  
252 459 $self->SUPER::create_action(%args);
253 }
254  
255 9 1;
256  
257 __END__
« Catalyst/Controller/HTML/FormFu/Action/MultiFormConfig.pm ↑ Summary Catalyst/Controller/HTML/FormFu/Action/FormConfig.pm »