blib/lib/Catalyst/Model/HTMLFormhandler.pm

filter: Statementstmt 72.3% 123 / 170 Branchbran 39.7% 31 / 78 Conditioncond 30.0% 6 / 20 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 88.8% 24 / 27 Podpod 70.0% 7 / 10 totaltotal 60.5% 192 / 317 CC 53 SCAR 39.4
CC
53
Cov
60%
ACCEPT_CONTEXT
86.3
ACCEPT_CONTEXT
86.3
build_model_adaptor
10.0
CRAP 51.5
↑ Summary
1 package Catalyst::Model::HTMLFormhandler;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose::Util::TypeConstraints ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 extends 'Catalyst::Model';
9 1 with 'Catalyst::Component::ApplicationAttribute';
10  
11 1 our $VERSION = '0.009';
12  
13 1 has 'roles' => (
14 is=>'ro',
15 isa=>'ArrayRef',
16 predicate=>'has_roles');
17  
18 1 has 'body_method' => (
19 is=>'ro',
20 isa=> Moose::Util::TypeConstraints::enum([qw/body_data body_parameters/]),
21 required=>1,
22 default=>'body_data');
23  
24 1 has 'schema_model_name' => (is=>'ro',
25 isa=>'Str',
26 predicate=>'has_schema_model_name');
27  
28 1 has 'form_namespace' => (
29 is=>'ro',
30 required=>1,
31 lazy=>1,
32 builder=>'_build_form_namespace');
33  
34 1 ▶ sub _default_form_namespace_part { 'Form' }
Subroutine _default_form_namespace_part: called CC=1 SCAR=0.0
35  
36 1 ▶ sub _build_form_namespace {
Subroutine _build_form_namespace: called CC=1 SCAR=0.0
37 1 my $self = shift;
38 1 return $self->_application .'::'. $self->_default_form_namespace_part;
39 }
40  
41 1 has 'form_packages' => (
42 is=>'ro',
43 required=>1,
44 lazy=>1,
45 builder=>'_build_form_packages');
46  
47 1 ▶ sub _build_form_packages {
Subroutine _build_form_packages: called CC=1 SCAR=0.0
48 1 my $self = shift;
49 1 my @forms = Module::Pluggable::Object->new(
50 require => 1,
51 search_path => [ $self->form_namespace ],
52 )->plugins;
53  
54 1 return \@forms;
55 }
56  
57 1 has 'no_auto_process' => (is=>'ro', isa=>'Bool', required=>1, default=>0);
58  
59 2 ▶sub build_model_adaptor {
Subroutine build_model_adaptor: called CC=6 SCAR=23.0
Pod undocumented
60 2 my ($self, $model_package, $form_package, $model_name) = @_;
61 2 ▶ my $roles = join( ',', map { "'$_'"} @{$self->roles||[]}) if $self->has_roles;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_roles 2 0 2
Condition50%1/2
$self->roles || []
l!l
20
MC/DC0%0/1
$self->roles || []
$self->roles
Truth table50%1/2
A
A= $self->roles
Aresult
11
00
62  
63 2 ▶ my $schema_args = $self->has_schema_model_name ?
Branch: 1 branch
BranchTrueFalseTotal
$self->has_schema_model_name ? : 0 2 2
64 '$args{schema} = $c->model("'.$self->schema_model_name.'");' : '';
65  
66 2 ▶ my $package = "package $model_package;\n" . q(
Condition50%1/2
$roles || ''
l!l
20
MC/DC0%0/1
$roles || ''
$roles
Truth table50%1/2
A
A= $roles
Aresult
11
00
67
68 use Moose;
69 use Moose::Util;
70 use ). $form_package . q! ;
71 extends 'Catalyst::Model';
72  
73 sub COMPONENT {
74 my ($class, $app, @args) = @_;
75 # Don't call new, we don't want to merge config now since this is a per-request
76 # model, that way we call for new configuration each time we bless (that way we
77 # can use models that are context sensitive.)
78  
79 return bless +{}, $class;
80 }
81  
82 sub ACCEPT_CONTEXT {
83 my ($self, $c, @args) = @_;
84 my $id = '__'. ref $self;
85  
86 # If there are odd args, that means the first one is either the item object
87 # or item_id (assuming someone is using the DBIC model trait.
88 my %args = ();
89 if(scalar(@args) % 2) {
90 # args are odd, so shift off the first one and figure it out.
91 my $item_proto = shift @args;
92 %args = @args;
93 if(ref $item_proto eq 'HASH') {
94 $args{params} = $item_proto;
95 } elsif(ref $item_proto) {
96 $args{item} = $item_proto;
97 } else {
98 $args{item_id} = $item_proto;
99 }
100 } else {
101 %args = @args;
102 }
103
104 #If an action arg is passed and its a Catalyst::Action, make it a URL
105 if(my $action = delete $args{action_from}) {
106 my @action = ref $action eq 'ARRAY' ? @$action : ($action);
107 $args{action} = ref $action ? $c->uri_for(@action) : $c->uri_for_action(@action);
108 }
109  
110 if(my $form = $c->stash->{$id}) {
111 $form->process( %args ) if keys(%args);
112 return $form;
113 }
114 my $set = 0;
115 unless($args{action}) {
116 foreach my $action ($c->controller->get_action_methods) {
117 my @attrs = map {($_ =~m/^FormModelTarget\((.+)\)$/)[0]} @{$action->attributes||[]};
118 foreach my $attr(@attrs) {
119 my @parts = (@{$c->req->captures}, @{$c->req->args});
120 $set=$c->uri_for($c->controller->action_for($action), (scalar @parts ? \@parts : ())) if ref($self) =~/$attr$/;
121 }
122 }
123 }
124 $args{action} = $set if $set;
125  
126 #If there is a schema model name use it
127 !. $schema_args .q!
128  
129 # If its a POST, set the request params (you can always override
130 # later.
131 if($c->req->method=~m/post/i) {
132 $args{params} = $c->req->! .$self->body_method. q! unless exists $args{params};
133 $args{posted} = 1 unless $args{posts};
134 }
135  
136 my $no_auto_process = exists $args{no_auto_process} ?
137 delete($args{no_auto_process}) : ! .$self->no_auto_process. q!;
138  
139 $c->stash->{$id} ||= do {
140 %args = %{$self->merge_config_hashes($c->config_for($self->catalyst_component_name), \%args)};
141 my $form = $self->_build_per_request_form(%args, ctx=>$c);
142 $form->process() if
143 $c->req->method=~m/post/i && \!$no_auto_process;
144 $form;
145 };
146  
147 return $c->stash->{$id};
148 }
149  
150 sub _build_per_request_form {
151 my ($self, %args) = @_;
152 my $composed = Moose::Util::with_traits( '! .$form_package. q!' , (! .($roles||'').q!));
153 my $form = $composed->new(%args);
154 }
155  
156 __PACKAGE__->meta->make_immutable;
157  
158 package ! .$model_package. q!::IsValid;
159
160 use Moose;
161 extends 'Catalyst::Model';
162  
163 sub ACCEPT_CONTEXT {
164 my ($self, $c, @args) = @_;
165 my $form = $c->model('! .$model_name. q!', @args);
166 return $form->is_valid ? $form : undef;
167 }
168  
169 __PACKAGE__->meta->make_immutable;
170  
171 !;
172  
173 0 ▶ eval $package or die "Trouble creating model: \n\n$@\n\n$package";
Subroutine BEGIN: called CC=1 SCAR=1.1
Subroutine BEGIN: called CC=1 SCAR=1.1
Subroutine BEGIN: called CC=1 SCAR=1.1
Subroutine BEGIN: called CC=1 SCAR=1.1
Subroutine BEGIN: called CC=1 SCAR=1.1
Subroutine BEGIN: called CC=1 SCAR=1.1
Subroutine BEGIN: called CC=1 SCAR=1.1
Subroutine BEGIN: called CC=1 SCAR=1.1
Subroutine ACCEPT_CONTEXT: called CC=23 SCAR=44.6
Subroutine ACCEPT_CONTEXT: called CC=2 SCAR=9.1
Subroutine ACCEPT_CONTEXT: not called CC=23 SCAR=44.6
Subroutine ACCEPT_CONTEXT: not called CC=2 SCAR=9.1
Subroutine COMPONENT: called CC=1 SCAR=1.1
Subroutine COMPONENT: called CC=1 SCAR=1.1
Subroutine _build_per_request_form: called CC=1 SCAR=1.1
Subroutine _build_per_request_form: not called CC=1 SCAR=1.1
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 37 branches
BranchTrueFalseTotal
eval $package or die "Trouble creating model: \n\n$@\n\n$package" 2 0 2
if (scalar(@args) % 2) { } 4 4 8
if (ref $item_proto eq 'HASH') { } 3 1 4
elsif (ref $item_proto) { } 1 0 1
if (my $action = delete $args{"action_from"}) 1 7 8
ref $action eq 'ARRAY' ? : 0 1 1
ref $action ? : 1 0 1
if (my $form = $c->stash->{$id}) 6 2 8
if keys %args 3 3 6
unless ($args{"action"}) 1 1 2
if ref($self) =~ /$attr$/ 1 0 1
scalar @parts ? : 0 1 1
if $set 1 1 2
if ($c->req->method =~ /post/i) 1 1 2
unless exists $args{"params"} 1 0 1
unless $args{"posts"} 1 0 1
exists $args{'no_auto_process'} ? : 0 2 2
if $c->req->method =~ /post/i and not $no_auto_process 1 1 2
$form->is_valid ? : 1 1 2
if (scalar(@args) % 2) { } 0 0 0
if (ref $item_proto eq 'HASH') { } 0 0 0
elsif (ref $item_proto) { } 0 0 0
if (my $action = delete $args{"action_from"}) 0 0 0
ref $action eq 'ARRAY' ? : 0 0 0
ref $action ? : 0 0 0
if (my $form = $c->stash->{$id}) 0 0 0
if keys %args 0 0 0
unless ($args{"action"}) 0 0 0
if ref($self) =~ /$attr$/ 0 0 0
scalar @parts ? : 0 0 0
if $set 0 0 0
if ($c->req->method =~ /post/i) 0 0 0
unless exists $args{"params"} 0 0 0
unless $args{"posts"} 0 0 0
exists $args{'no_auto_process'} ? : 0 0 0
if $c->req->method =~ /post/i and not $no_auto_process 0 0 0
$form->is_valid ? : 0 0 0
Condition25%4/16
$action->attributes || []
l!l
70
$c->stash->{$id} ||= do { %args = %{$self->merge_config_hashes($c->config_for($self->catalyst_component_name), \%args);}; my $form = $self->_build_per_request_form(%args, "ctx", $c); $form->process if $c->req->method =~ /post/i and not $no_auto_process; $form }
l!l&&r!l&&!r
020
$c->req->method =~ /post/i and not $no_auto_process
!ll&&!rl&&r
101
$action->attributes || []
l!l
00
$c->stash->{$id} ||= do { %args = %{$self->merge_config_hashes($c->config_for($self->catalyst_component_name), \%args);}; my $form = $self->_build_per_request_form(%args, "ctx", $c); $form->process if $c->req->method =~ /post/i and not $no_auto_process; $form }
l!l&&r!l&&!r
000
$c->req->method =~ /post/i and not $no_auto_process
!ll&&!rl&&r
000
MC/DC0%0/1
$action->attributes || []
$action->attributes
MC/DC0%0/2
$c->stash->{$id} ||= do { %args = %{$self->merge_config_hashes($c->config_for($self->catalyst_component_name), \%args);}; my $form = $self->_build_per_request_form(%args, "ctx", $c); $form->process if $c->req->method =~ /post/i and not $no_auto_process; $form }
$c->stash->{$id} do { %args = %{$self->merge_config_hashes($c->config_for($self->catalyst_component_name), \%args);}; my $form = $self->_build_per_request_form(%args, "ctx", $c); $form->process if $c->req->method =~ /post/i and not $no_auto_process; $form }
MC/DC50%1/2
$c->req->method =~ /post/i and not $no_auto_process
$c->req->method =~ /post/i not $no_auto_process
MC/DC0%0/1
$action->attributes || []
$action->attributes
MC/DC0%0/2
$c->stash->{$id} ||= do { %args = %{$self->merge_config_hashes($c->config_for($self->catalyst_component_name), \%args);}; my $form = $self->_build_per_request_form(%args, "ctx", $c); $form->process if $c->req->method =~ /post/i and not $no_auto_process; $form }
$c->stash->{$id} do { %args = %{$self->merge_config_hashes($c->config_for($self->catalyst_component_name), \%args);}; my $form = $self->_build_per_request_form(%args, "ctx", $c); $form->process if $c->req->method =~ /post/i and not $no_auto_process; $form }
MC/DC0%0/2
$c->req->method =~ /post/i and not $no_auto_process
$c->req->method =~ /post/i not $no_auto_process
Truth table50%1/2
A
A= $action->attributes
Aresult
11
00
Truth table33%1/3
A ||= B
A= $c->stash->{$id}B= do { %args = %{$self->merge_config_hashes($c->config_for($self->catalyst_component_name), \%args);}; my $form = $self->_build_per_request_form(%args, "ctx", $c); $form->process if $c->req->method =~ /post/i and not $no_auto_process; $form }
ABresult
1X1
011
000
Truth table66%2/3
A and not(B)
A= $c->req->method =~ /post/iB= not $no_auto_process
ABresult
0X0
100
111
Truth table0%0/2
A
A= $action->attributes
Aresult
11
00
Truth table0%0/3
A ||= B
A= $c->stash->{$id}B= do { %args = %{$self->merge_config_hashes($c->config_for($self->catalyst_component_name), \%args);}; my $form = $self->_build_per_request_form(%args, "ctx", $c); $form->process if $c->req->method =~ /post/i and not $no_auto_process; $form }
ABresult
1X1
011
000
Truth table0%0/3
A and not(B)
A= $c->req->method =~ /post/iB= not $no_auto_process
ABresult
0X0
100
111
174 }
175  
176 2 ▶sub construct_model_package {
Subroutine construct_model_package: called CC=1 SCAR=0.0
Pod undocumented
177 2 my ($self, $form_package) = @_;
178 2 return $self->_application .'::Model'. ($form_package=~m/${\$self->_application}(::.+$)/)[0];
179 }
180  
181 2 ▶sub construct_model_name {
Subroutine construct_model_name: called CC=1 SCAR=0.0
Pod undocumented
182 2 my ($self, $form_package) = @_;
183 2 return ($form_package=~m/${\$self->_application}::(.+$)/)[0];
184 }
185  
186 1 ▶sub expand_modules {
Subroutine expand_modules: called CC=2 SCAR=6.9
Pod documented
187 1 my ($self, $config) = @_;
188 1 my @model_packages;
189 1 foreach my $form_package (@{$self->form_packages}) {
190 2 my $model_package = $self->construct_model_package($form_package);
191 2 my $model_name = $self->construct_model_name($form_package);
192 2 $self->build_model_adaptor($model_package, $form_package, $model_name);
193 2 push @model_packages, $model_package;
194 }
195  
196 1 return @model_packages;
197 }
198  
199 1 __PACKAGE__->meta->make_immutable;
200  
201 =head1 NAME
202  
203 Catalyst::Model::HTMLFormhandler - Proxy a directory of HTML::Formhandler forms
204  
205 =head1 SYNOPSIS
206  
207 package MyApp::Model::Form;
208  
209 use Moose;
210 extends 'Catalyst::Model::HTMLFormhandler';
211  
212 __PACKAGE__->config( form_namespace=>'MyApp::Form' );
213  
214 And then using it in a controller:
215  
216 my $form = $c->model("Form::Email"); # Maps to MyApp::Email via MyApp:Model::Email
217  
218 # If the request is a POST, we process parameters automatically
219 if($form->is_valid) {
220 ...
221 } else {
222 ...
223 }
224  
225 =head1 DESCRIPTION
226  
227 Assuming a project namespace 'MyApp::Form' with L<HTML::Formhandler> forms. like
228 the following example:
229  
230 package MyApp::Form::Email;
231  
232 use HTML::FormHandler::Moose;
233  
234 extends 'HTML::FormHandler';
235  
236 has aaa => (is=>'ro', required=>1);
237 has bbb => (is=>'ro', required=>1);
238  
239 has_field 'email' => (
240 type=>'Email',
241 size => 96,
242 required => 1);
243  
244 You create a single L<Catalyst> model like this:
245  
246 package MyApp::Model::Form;
247  
248 use Moose;
249 extends 'Catalyst::Model::HTMLFormhandler';
250  
251 __PACKAGE__->config( form_namespace=>'MyApp::Form' );
252  
253 (Setting 'form_namespace' is optional, it defaults to the application
254 namespace plus "::Form" (in this example case that would be "MyApp::Form").
255  
256 When you start your application it will register one model for each form
257 in the declared namespace. So in the above example you should see a model
258 'MyApp::Model::Form::Email'. This is a 'PerRequest' model since it does
259 ACCEPT_CONTEXT, it will generate a new instance of the form object once
260 per request scope.
261  
262 It will also create one model with the ::IsValid suffix, which is a shortcut
263 to return a form only if its valid and undef otherwise.
264  
265 You can set model configuration in the normal way, in your application general
266 configuration:
267  
268 package MyApp;
269 use Catalyst;
270  
271 MyApp->config(
272 'Model::Form::Email' => { aaa => 1000 }
273 );
274
275 MyApp->setup;
276  
277 And you can pass additional args to the 'new' call of the form when you request
278 the form model:
279  
280 my $email = $c->model('Form::Email', bbb=>2000);
281  
282 Additional args should be in the form of a hash, as in the above example OR you can
283 pass a single argument which is either an object, hashref or id followed by a hash
284 of remaining arguements. These first argument gets set to the item or item_id
285 since its common to need:
286  
287 my $email = $c->model('Form::Email', $dbic_email_row, %args);
288  
289 Or if its a HashRef, these are set to the params for processing.
290  
291 The generated proxy will also add the ctx argument based on the current value of
292 $c, although using this may not be a good way to build well, decoupled applications.
293 It also will add the schema argument if you set a schema_model_name.
294  
295 We offer two additional bit of useful suger:
296  
297 If you pass argument 'action_from' with a value of an action object or an action
298 private name that will set the form action value. If 'action_from' is an arrayref
299 we dereference it when building the url.
300  
301 By default if the request is a POST, we will process the request arguments and
302 return a form object that you can test for validity. If you don't want this
303 behavior you can disable it by passing 'no_auto_process'. For example:
304  
305 my $form = $c->model("Form::XXX", no_auto_process=>1)
306  
307 =head1 ATTRIBUTES
308  
309 This class defines the following attributes you may set via
310 standard L<Catalyst> configuration.
311  
312 =head2 form_namespace
313  
314 This is the target namespace that L<Module::Pluggable> uses to look for forms.
315 It defaults to 'MyApp::Form' (where 'MyApp' is you application namespace).
316  
317 =head2 schema_model_name
318  
319 The name of your DBIC Schema model (if you have one). If you set this, we will
320 automatically instantiate your form classes with as schema => $model argument.
321 Useful if you are using L<HTML::FormHandler::Model::DBIC>.
322  
323 =head2 roles
324  
325 A list of L<Moose::Role>s that get applied automatically to each form model.
326  
327 =head2 post_method
328  
329 This is the name of the method called on L<Catalyst::Request> used to access any
330 POSTed data. Required field, the options are 'body_data' and 'body_parameters.
331 The default is 'body_data'.
332  
333 =head2 no_auto_process
334  
335 By default when createing the perrequest form if the request is a POST we
336 just go ahead and process those args. Setting this to true will disable
337 this behavior globally if you prefer more control.
338  
339 =head1 SPECIAL ARGUMENTS
340  
341 You may pass the following special arguments to $c->model("Form::XXX") to
342 influence how the form object is setup.
343  
344 =head2 no_auto_process
345  
346 Turns off the call to ->process when the request is a POST.
347  
348 =head2 action_from
349  
350 Shortcut to create the action value of the form. If an object, we set 'action'
351 from $c->uri_for($object). If its an arrayref from $c->uri_for( @$action_from).
352  
353 =head1 ACTION ATTRIBUTES.
354  
355 =head2 FormModelTarget( $model)
356  
357 When used on an action, sets that action as the target of the form action. This
358 is a bit experimental. We get any needed captures and arguments from the current
359 request, this this only works if the target action has the same number of needed
360 args and captures.
361  
362 =head1 AUTHOR
363
364 John Napiorkowski L<email:jjnapiork@cpan.org>
365
366 =head1 SEE ALSO
367
368 L<Catalyst>, L<Catalyst::Model>, L<HTML::Formhandler>, L<Module::Pluggable>
369  
370 =head1 COPYRIGHT & LICENSE
371
372 Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org>
373
374 This library is free software; you can redistribute it and/or modify it under
375 the same terms as Perl itself.
376  
377 =cut
↑ Summary