DBIC/FormGenerator.pm

filter: Statementstmt 96.6% 115 / 119 Branchbran 79.5% 35 / 44 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 6 totaltotal 87.2% 171 / 196 CC 33 SCAR 21.9
CC
33
Cov
91%
get_elements
17.0
get_foreign_cols
7.1
get_self_cols
7.1
CRAP 9.0
« DBIC.pm ↑ Summary
1 package Rose::HTMLx::Form::DBIC::FormGenerator;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DBIx::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use version; our $VERSION = qv('0.0.3');
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 has 'schema' => (
11 is => 'rw',
12 isa => 'DBIx::Class::Schema',
13 );
14  
15 has 'tt' => (
16 is => 'ro',
17 3 ▶ default => sub { Template->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 );
19  
20 1 has 'class_prefix' => (
21 is => 'ro',
22 );
23  
24 1 has 'style' => (
25 is => 'ro'
26 );
27  
28 1 has 'm2m' => (
29 is => 'ro'
30 );
31  
32 1 my $form_template = <<'END';
33 [% FOR form = forms %]
34 {
35 package [% form.class %]Form;
36 use base "Rose::HTML::Form";
37
38 sub build_form {
39 my($self) = shift;
40 $self->method( 'POST' );
41
42 $self->add_fields (
43 [% FOR field = form.fields -%]
44 [%- SET field_name = field.name; field.delete( 'name' ); -%]
45 [% field_name %] => { [% FOREACH attr IN field.pairs %] [% attr.key %] => '[% attr.value %]', [% END %] },
46 [% END %]
47 );
48 $self->add_forms (
49 [% FOR sub_form = form.sub_forms -%]
50 [% IF single %]#[% END %] [% sub_form.name %] => [% sub_form.class %]Form->new,
51 [% END %]
52 );
53
54
55 }
56  
57 }
58 [% END %]
59  
60 END
61  
62 5 ▶sub generate_form {
Subroutine generate_form: called CC=4 SCAR=14.4
Pod undocumented
63 5 my ( $self, $rsname ) = @_;
64 5 my $config = $self->get_config( $rsname );
65 5 my $output;
66 5 my %sub_forms = map { $_->{class} => $_ } @{$config->{sub_forms}};
67 5 my $tmpl_params = {
68 forms => [ $config, values %sub_forms ],
69 };
70 5 ▶ $tmpl_params->{single} = 1 if defined $self->style && $self->style eq 'single';
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->style and $self->style eq "single" 0 5 5
Condition33%1/3
defined $self->style and $self->style eq "single"
!ll&&!rl&&r
500
MC/DC0%0/2
defined $self->style and $self->style eq "single"
defined $self->style $self->style eq "single"
Truth table33%1/3
A and B
A= defined $self->styleB= $self->style eq "single"
ABresult
0X0
100
111
71 5 ▶ $self->tt->process( \$form_template, $tmpl_params, \$output )
Branch: 1 branch
BranchTrueFalseTotal
$self->tt->process(\$form_template, $tmpl_params, \$output) or die $self->tt->error, "\n" 5 0 5
72 || die $self->tt->error(), "\n";
73 5 return $output;
74 }
75  
76 31 ▶sub _strip_class {
Subroutine _strip_class: called CC=1 SCAR=0.0
77 31 my $fullclass = shift;
78 31 my @parts = split /::/, $fullclass;
79 31 my $class = pop @parts;
80 31 return $class;
81 }
82  
83 5 ▶sub get_config {
Subroutine get_config: called CC=2 SCAR=6.9
Pod undocumented
84 5 my( $self, $class ) = @_;
85 5 my $config = $self->get_elements ( $class, 0, );
86 5 push @{$config->{fields}}, {
87 type => 'submit',
88 name => 'foo',
89 };
90 5 my $target_class = $class;
91 5 ▶ $target_class = $self->class_prefix . '::' . $class if $self->class_prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $self->class_prefix 1 4 5
92 5 $config->{class} = $target_class;
93 5 return $config;
94 }
95  
96 1 my %types = (
97 text => 'textarea',
98 'int' => 'int',
99 integer => 'integer',
100 num => 'num',
101 number => 'number',
102 numeric => 'numeric',
103 );
104
105
106 43 ▶sub m2m_for_class {
Subroutine m2m_for_class: called CC=3 SCAR=11.0
Pod undocumented
107 43 my( $self, $class ) = @_;
108 43 ▶ return if not $self->m2m;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->m2m 33 10 43
109 10 ▶ return if not $self->m2m->{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->m2m->{$class} 5 5 10
110 5 return @{$self->m2m->{$class}};
111 }
112  
113 11 ▶sub get_elements {
Subroutine get_elements: called CC=17 SCAR=28.3
Pod undocumented
114 11 my( $self, $class, $level, @exclude ) = @_;
115 11 my $source = $self->schema->source( $class );
116 11 my %primary_columns = map {$_ => 1} $source->primary_columns;
117 11 my @fields;
118 my @sub_forms;
119 11 my @fieldsets;
120 11 for my $rel( $source->relationships ) {
121 32 ▶ next if grep { $_ eq $rel } @exclude;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $rel;} @exclude 0 32 32
122 32 ▶ next if grep { $_->[1] eq $rel } $self->m2m_for_class($class);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->[1] eq $rel;} $self->m2m_for_class($class) 1 31 32
123 31 my $info = $source->relationship_info($rel);
124 31 my @self_cols = get_self_cols( $info->{cond} );
125 31 push @exclude, @self_cols;
126 31 my $rel_class = _strip_class( $info->{class} );
127 31 my $elem_conf;
128 31 ▶ if ( ! ( $info->{attrs}{accessor} eq 'multi' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $info->{'attrs'}{'accessor'} eq 'multi') { } 23 8 31
elsif ($level < 1) { } 6 2 8
129 23 my $new_element = {
130 name => $rel,
131 type => 'selectbox'
132 };
133 23 my $col_info = $source->column_info($self_cols[0]);
134 23 ▶ $new_element->{required} = 1 if not $col_info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_info->{"is_nullable"} 18 5 23
135 23 push @fields, $new_element;
136 }
137 elsif( $level < 1 ) {
138 6 my @new_exclude = get_foreign_cols ( $info->{cond} );
139 6 my $config = $self->get_elements ( $rel_class, 1, );
140 6 my $target_class = $rel_class;
141 6 ▶ $target_class = $self->class_prefix . '::' . $rel_class if $self->class_prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $self->class_prefix 1 5 6
142 6 $config->{class} = $target_class;
143 6 $config->{name} = $rel;
144 6 push @sub_forms, $config;
145 }
146 }
147 11 for my $col ( $source->columns ) {
148 50 my $new_element = { name => $col };
149 50 my $info = $source->column_info($col);
150 50 ▶ if( $primary_columns{$col} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($primary_columns{$col}) { } 15 35 50
151 # - generated schemas have not is_auto_increment set so
152 # so the below needs to be commented out
153 # and $info->{is_auto_increment} ){
154 15 $new_element->{type} = 'hidden';
155 }
156 else{
157 35 ▶ next if grep { $_ eq $col } @exclude;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $col;} @exclude 10 25 35
158 25 ▶ my $type = $types{ $info->{data_type} } || 'text';
Condition100%2/2
$types{$info->{'data_type'}} || 'text'
l!l
124
MC/DC100%1/1
$types{$info->{'data_type'}} || 'text'
$types{$info->{'data_type'}}
Truth table100%2/2
A
A= $types{$info->{'data_type'}}
Aresult
11
00
159 25 ▶ $type = 'textarea' if defined($info->{size}) && $info->{size} > 60;
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"size"} and $info->{"size"} > 60 14 11 25
Condition66%2/3
defined $info->{"size"} and $info->{"size"} > 60
!ll&&!rl&&r
11014
MC/DC50%1/2
defined $info->{"size"} and $info->{"size"} > 60
defined $info->{"size"} $info->{"size"} > 60
Truth table66%2/3
A and B
A= defined $info->{"size"}B= $info->{"size"} > 60
ABresult
0X0
100
111
160 25 $new_element->{type} = $type;
161 25 ▶ $new_element->{size} = $info->{size} if $type eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "text" 10 15 25
162 25 ▶ $new_element->{required} = 1 if not $info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"is_nullable"} 3 22 25
163 }
164 40 unshift @fields, $new_element;
165 }
166 11 for my $many( $self->m2m_for_class($class) ){
167 1 unshift @fields, {
168 name => $many->[0],
169 type => 'select',
170 multiple => 1
171 };
172 }
173 11 return { fields => \@fields, sub_forms => \@sub_forms };
174 }
175  
176 6 ▶sub get_foreign_cols{
Subroutine get_foreign_cols: called CC=6 SCAR=19.6
Pod undocumented
177 6 my $cond = shift;
178 6 my @cols;
179 6 ▶ if ( ref $cond eq 'ARRAY' ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cond eq 'ARRAY') { } 0 6 6
elsif (ref $cond eq 'HASH') { } 6 0 6
180 0 for my $c1 ( @$cond ){
181 0 push @cols, get_foreign_cols( $c1 );
182 }
183 }
184 elsif ( ref $cond eq 'HASH' ){
185 6 for my $key ( keys %{$cond} ){
186 6 ▶ if( $key =~ /foreign\.(.*)/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /foreign\.(.*)/) 6 0 6
187 6 push @cols, $1;
188 }
189 }
190 }
191 6 return @cols;
192 }
193  
194 31 ▶sub get_self_cols{
Subroutine get_self_cols: called CC=6 SCAR=19.6
Pod undocumented
195 31 my $cond = shift;
196 31 my @cols;
197 31 ▶ if ( ref $cond eq 'ARRAY' ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cond eq 'ARRAY') { } 0 31 31
elsif (ref $cond eq 'HASH') { } 31 0 31
198 0 for my $c1 ( @$cond ){
199 0 push @cols, get_self_cols( $c1 );
200 }
201 }
202 elsif ( ref $cond eq 'HASH' ){
203 31 for my $key ( values %{$cond} ){
204 31 ▶ if( $key =~ /self\.(.*)/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /self\.(.*)/) 31 0 31
205 31 push @cols, $1;
206 }
207 }
208 }
209 31 return @cols;
210 }
211  
212  
213 #################### main pod documentation begin ###################
214 ## Below is the stub of documentation for your module.
215 ## You better edit it!
216  
217  
218 =head1 NAME
219  
220 Rose::HTMLx::Form::DBIC::FormGenerator - generates Rose::HTML forms from database schema
221  
222 =head1 SYNOPSIS
223  
224 use Rose::HTMLx::Form::DBIC::FormGenerator;
225 my $generator = Rose::HTMLx::Form::DBIC::FormGenerator->new( schema => $schema );
226 my $output = $generator->generate_form( 'User' );
227  
228  
229 =head1 DESCRIPTION
230  
231  
232 =head1 USAGE
233  
234  
235  
236 =head1 BUGS
237  
238  
239  
240 =head1 SUPPORT
241  
242  
243  
244 =head1 AUTHOR
245  
246 Zbigniew Lukasiak
247 CPAN ID: zby
248 http://perlalchemy.blogspot.com
249  
250 =head1 COPYRIGHT
251  
252 This program is free software; you can redistribute
253 it and/or modify it under the same terms as Perl itself.
254  
255 The full text of the license can be found in the
256 LICENSE file included with this module.
257  
258  
259 =head1 SEE ALSO
260  
261 perl(1).
262  
263 =cut
264  
265 #################### main pod documentation end ###################
266  
267  
268 1 1;
269 # The preceding line will help the module return a true value
270  
271  
272  
« DBIC.pm ↑ Summary