DBIC.pm

filter: Statementstmt 81.0% 107 / 132 Branchbran 56.8% 25 / 44 Conditioncond 47.6% 10 / 21 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 84.2% 16 / 19 Podpod 16.6% 1 / 6 totaltotal 69.6% 163 / 234 CC 44 SCAR 28.5
CC
44
Cov
72%
_master_relation_cond
72.0
_delete_empty_auto_increment
30.0
init_with_dbic
13.4
CRAP 17.3
↑ Summary DBIC/FormGenerator.pm »
1 package Rose::HTMLx::Form::DBIC;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Rose::HTML::Form;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 $VERSION = '0.08';
13 # @ISA = qw(Exporter);
14 #Give a hoot don't pollute, do not export more than needed by default
15 # @EXPORT = qw( );
16 # @EXPORT_OK = qw( options_from_resultset init_from_dbic dbic_from_form values_hash );
17 # %EXPORT_TAGS = ();
18 }
19  
20 3 has 'rs' => (
21 is => 'ro',
22 isa => 'DBIx::Class::ResultSet',
23 );
24  
25 3 has 'form' => (
26 is => 'ro',
27 isa => 'Rose::HTML::Form',
28 );
29  
30 7 ▶sub options_from_resultset {
Subroutine options_from_resultset: called CC=1 SCAR=0.0
Pod documented
31 7 my( $self ) = @_;
32 7 _options_from_resultset( $self->form, $self->rs );
33 }
34  
35 0 ▶sub init_params {
Subroutine init_params: not called CC=1 SCAR=6.9
Pod undocumented
36 0 my( $self, $params ) = @_;
37 0 $self->options_from_resultset();
38 0 $self->form->params( $params );
39 0 $self->form->init_fields();
40 }
41  
42 14 ▶sub _options_from_resultset {
Subroutine _options_from_resultset: called CC=9 SCAR=22.0
43 14 my( $form, $rs ) = @_;
44 14 for my $field ( $form->fields ){
45 63 ▶ if ( $field->isa( 'Rose::HTML::Form::Field::SelectBox' ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($field->isa("Rose::HTML::Form::Field::SelectBox")) 16 47 63
46 16 my $name = $field->local_name;
47 16 my $related_source = _get_related_source( $rs, $name );
48 16 ▶ if( $related_source ){
Branch: 1 branch
BranchTrueFalseTotal
if ($related_source) 12 4 16
49 12 my ( $pk ) = $related_source->primary_columns;
50 12 my $related_rs = $related_source->resultset;
51 12 ▶ while( my $related_row = $related_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $related_row = $related_rs->next) 36 12 48
52 36 $field->add_option( $related_row->$pk => $related_row->$pk );
53 }
54 }
55 }
56 }
57 14 for my $sub_form ( $form->forms ){
58 6 my $name = $sub_form->name;
59 6 my $related_source = _get_related_source( $rs, $name );
60 6 ▶ if( $related_source ){
Branch: 1 branch
BranchTrueFalseTotal
if ($related_source) 6 0 6
61 6 my $related_rs = $related_source->resultset;
62 6 ▶ if( $sub_form->isa( 'Rose::HTML::Form::Repeatable' ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_form->isa('Rose::HTML::Form::Repeatable')) { } 3 3 6
63 3 for my $sub_sub_form ( $sub_form->forms ){
64 4 _options_from_resultset( $sub_sub_form, $related_rs );
65 }
66 }
67 else {
68 3 _options_from_resultset( $sub_form, $related_rs );
69 }
70 }
71 }
72 }
73  
74 25 ▶sub _get_related_source {
Subroutine _get_related_source: called CC=5 SCAR=16.2
75 25 my ( $rs, $name ) = @_;
76 25 ▶ if( $rs->result_source->has_relationship( $name ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($rs->result_source->has_relationship($name)) 10 15 25
77 10 return $rs->result_source->related_source( $name );
78 }
79 # many to many case
80 15 my $row = $rs->new({});
81 15 ▶ if ( $row->can( $name ) and $row->can( 'add_to_' . $name ) and $row->can( 'set_' . $name ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($row->can($name) and $row->can('add_to_' . $name) and $row->can('set_' . $name)) 11 4 15
Condition66%4/6
$row->can($name) and $row->can('add_to_' . $name) and $row->can('set_' . $name)
!ll&&!rl&&r
4011
$row->can($name) and $row->can('add_to_' . $name)
!ll&&!rl&&r
4011
MC/DC33%1/3
$row->can($name) and $row->can('add_to_' . $name) and $row->can('set_' . $name)
$row->can($name) $row->can('add_to_' . $name) $row->can('set_' . $name)
Truth table50%2/4
A and B and C
A= $row->can($name)B= $row->can('add_to_' . $name)C= $row->can('set_' . $name)
ABCresult
0XX0
10X0
1100
1111
82 11 return $row->$name->result_source;
83 }
84 4 return;
85 }
86  
87 2 ▶sub init_from_dbic {
Subroutine init_from_dbic: called CC=1 SCAR=0.0
Pod undocumented
88 2 my( $self, @pks ) = @_;
89 2 my $form = $self->form;
90 2 my $rs = $self->rs;
91 2 return init_with_dbic( $form, $rs->find( @pks, { key => 'primary' } ) );
92 }
93  
94 5 ▶sub init_with_dbic {
Subroutine init_with_dbic: called CC=13 SCAR=25.9
Pod undocumented
95 5 my($form, $object) = @_;
96  
97 5 ▶ croak "Missing required object argument" unless($object);
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 5 5
98  
99 5 $form->clear();
100  
101 5 foreach my $field ($form->local_fields) {
102 15 my $name = $field->local_name;
103  
104 15 ▶ if($object->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can($name)) 15 0 15
105 # many to many case
106 15 ▶ if( $object->can( 'add_to_' . $name ) and $object->can( 'set_' . $name ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can('add_to_' . $name) and $object->can('set_' . $name)) { } 3 12 15
Condition66%2/3
$object->can('add_to_' . $name) and $object->can('set_' . $name)
!ll&&!rl&&r
1203
MC/DC50%1/2
$object->can('add_to_' . $name) and $object->can('set_' . $name)
$object->can('add_to_' . $name) $object->can('set_' . $name)
Truth table66%2/3
A and B
A= $object->can('add_to_' . $name)B= $object->can('set_' . $name)
ABresult
0X0
100
111
107 3 my ( $pk ) = _get_pk_for_related( $object, $name );
108 3 $field->add_values( map{ $_->$pk } $object->$name());
109 }
110 else{
111 12 my $value = scalar $object->$name();
112 12 ▶ if( blessed( $value ) && $value->isa( 'DBIx::Class::Row' ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("DBIx::Class::Row")) 1 11 12
Condition66%2/3
builtin::blessed($value) and $value->isa("DBIx::Class::Row")
!ll&&!rl&&r
1101
MC/DC50%1/2
builtin::blessed($value) and $value->isa("DBIx::Class::Row")
builtin::blessed($value) $value->isa("DBIx::Class::Row")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("DBIx::Class::Row")
ABresult
0X0
100
111
113 1 ( $value ) = $value->id;
114 }
115 12 $field->input_value( $value );
116 }
117 }
118 }
119 5 foreach my $sub_form ($form->forms ) {
120 2 my $name = $sub_form->form_name;
121 2 my $info = $object->result_source->relationship_info( $name );
122 2 ▶ if( $info->{attrs}{accessor} eq 'multi' and $sub_form->isa( 'Rose::HTML::Form::Repeatable' ) ){
Branch: 2 branches
BranchTrueFalseTotal
if ($info->{'attrs'}{'accessor'} eq 'multi' and $sub_form->isa('Rose::HTML::Form::Repeatable')) { } 1 1 2
elsif ($info) { } 1 0 1
Condition66%2/3
$info->{"attrs"}{"accessor"} eq "multi" and $sub_form->isa("Rose::HTML::Form::Repeatable")
!ll&&!rl&&r
101
MC/DC50%1/2
$info->{"attrs"}{"accessor"} eq "multi" and $sub_form->isa("Rose::HTML::Form::Repeatable")
$info->{"attrs"}{"accessor"} eq "multi" $sub_form->isa("Rose::HTML::Form::Repeatable")
Truth table66%2/3
A and B
A= $info->{"attrs"}{"accessor"} eq "multi"B= $sub_form->isa("Rose::HTML::Form::Repeatable")
ABresult
0X0
100
111
123 1 my @sub_objects = $object->$name;
124 1 my $i = 1;
125 1 for my $sub_object ( @sub_objects ){
126 2 my $sub_sub_form = $sub_form->make_form( $i++ );
127 2 init_with_dbic( $sub_sub_form, $sub_object );
128 }
129 }
130 elsif( $info ){
131 1 my $sub_object = $object->$name;
132 1 init_with_dbic( $sub_form, $sub_object );
133 }
134 }
135 }
136 3 ▶sub _get_pk_for_related {
Subroutine _get_pk_for_related: called CC=1 SCAR=0.0
137 3 my ( $object, $relation ) = @_;
138  
139 3 my $rs = $object->result_source->resultset;
140 3 my $result_source = _get_related_source( $rs, $relation );
141 3 return $result_source->primary_columns;
142 }
143  
144 0 ▶sub _delete_empty_auto_increment {
Subroutine _delete_empty_auto_increment: not called CC=5 SCAR=34.0
145 0 my ( $object ) = @_;
146 0 for my $col ( keys %{$object->{_column_data}}){
147 0 ▶ if( $object->result_source->column_info( $col )->{is_auto_increment}
Branch: 1 branch
BranchTrueFalseTotal
if ($object->result_source->column_info($col)->{"is_auto_increment"} and !defined($object->{"_column_data"}{$col}) || $object->{"_column_data"}{$col} eq "") 0 0 0
Condition0%0/6
$object->result_source->column_info($col)->{"is_auto_increment"} and !defined($object->{"_column_data"}{$col}) || $object->{"_column_data"}{$col} eq ""
!ll&&!rl&&r
000
not defined $object->{"_column_data"}{$col} or $object->{"_column_data"}{$col} eq ""
l!l&&r!l&&!r
000
MC/DC0%0/3
$object->result_source->column_info($col)->{"is_auto_increment"} and !defined($object->{"_column_data"}{$col}) || $object->{"_column_data"}{$col} eq ""
$object->result_source->column_info($col)->{"is_auto_increment"} not defined $object->{"_column_data"}{$col} $object->{"_column_data"}{$col} eq ""
Truth table0%0/4
A and not(B) or C
A= $object->result_source->column_info($col)->{"is_auto_increment"}B= not defined $object->{"_column_data"}{$col}C= $object->{"_column_data"}{$col} eq ""
ABCresult
0XX0
1000
11X1
1011
148 and
149 ( ! defined $object->{_column_data}{$col} or $object->{_column_data}{$col} eq '' )
150 ){
151 0 delete $object->{_column_data}{$col}
152 }
153 }
154 }
155  
156 8 ▶sub values_hash {
Subroutine values_hash: called CC=5 SCAR=16.1
Pod undocumented
157 8 my( $form ) = @_;
158
159 8 my %hash;
160 8 foreach my $field ($form->local_fields) {
161 22 $hash{$field->local_name} = $field->internal_value;
162 }
163 8 foreach my $sub_form ($form->forms ) {
164 3 ▶ if( $sub_form->isa( 'Rose::HTML::Form::Repeatable' ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_form->isa('Rose::HTML::Form::Repeatable')) { } 1 2 3
165 1 for my $sub_sub_form ( $sub_form->forms ) {
166 2 push @{$hash{$sub_form->form_name}}, values_hash( $sub_sub_form );
167 }
168 }
169 else{
170 2 $hash{$sub_form->form_name} = values_hash( $sub_form );
171 }
172 }
173 8 return \%hash;
174 }
175  
176 1 ▶sub dbic_from_form {
Subroutine dbic_from_form: called CC=5 SCAR=17.5
Pod undocumented
177 1 my( $self, @pks ) = @_;
178 1 my $form = $self->form;
179 1 my $rs = $self->rs;
180 1 ▶ if ( $form->validate ){
Branch: 1 branch
BranchTrueFalseTotal
if ($form->validate) { } 1 0 1
181 1 my $updates = values_hash( $form );
182 1 my @primary_columns = $rs->result_source->primary_columns;
183 1 for my $value ( @pks ){
184 1 $updates->{shift @primary_columns} = $value;
185 }
186 1 for my $key ( @primary_columns ){
187 0 ▶ $updates->{$key} = undef if not length( $updates->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless length $updates->{$key} 0 0 0
188 }
189 1 return $rs->recursive_update( $updates );
190 }
191 else {
192 0 return;
193 }
194 }
195  
196 0 ▶sub _master_relation_cond {
Subroutine _master_relation_cond: not called CC=8 SCAR=42.8
197 0 my ( $object, $cond, @foreign_ids ) = @_;
198 0 my $foreign_ids_re = join '|', @foreign_ids;
199 0 ▶ if ( ref $cond eq 'HASH' ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cond eq 'HASH') { } 0 0 0
elsif (ref $cond eq 'ARRAY') { } 0 0 0
200 0 for my $f_key ( keys %{$cond} ) {
201 # might_have is not master
202 0 my $col = $cond->{$f_key};
203 0 $col =~ s/self\.//;
204 0 ▶ if( $object->column_info( $col )->{is_auto_increment} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($object->column_info($col)->{"is_auto_increment"}) 0 0 0
205 0 return 0;
206 }
207 0 ▶ if( $f_key =~ /^foreign\.$foreign_ids_re/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($f_key =~ /^foreign\.$foreign_ids_re/) 0 0 0
208 0 return 1;
209 }
210 }
211 }elsif ( ref $cond eq 'ARRAY' ){
212 0 for my $new_cond ( @$cond ) {
213 0 ▶ return 1 if _master_relation_cond( $object, $new_cond, @foreign_ids );
Branch: 1 branch
BranchTrueFalseTotal
if _master_relation_cond($object, $new_cond, @foreign_ids) 0 0 0
214 }
215 }
216 0 return;
217 }
218  
219  
220  
221 #################### main pod documentation begin ###################
222 ## Below is the stub of documentation for your module.
223 ## You better edit it!
224  
225  
226 =head1 NAME
227  
228 Rose::HTMLx::Form::DBIC - Filling Forms with data from DBIC and saving data from Forms
229 to DBIC records.
230  
231 =head1 SYNOPSIS
232  
233 use Rose::HTML::Form::DBIC qw(options_from_resultset init_with_dbic dbic_from_form );
234 use DvdForm;
235 use DBSchema;
236 .
237 .
238 .
239 $form = DvdForm->new;
240 options_from_resultset( $form, $schema->resultset( 'Dvd' ) );
241 $form->params( { ... } );
242 $form->init_fields();
243 if( $form->was_submitted ){
244 if ( $form->validate ){
245 dbic_from_form($form, $schema->resultset( 'Dvd' )->find(1));
246 }
247 }
248 else {
249 init_with_dbic($form, $schema->resultset( 'Dvd' )->find(1));
250 }
251  
252 =head1 DESCRIPTION
253  
254 This module exports functions integrating Rose::HTML::Form with DBIx::Class.
255  
256 =head1 USAGE
257  
258 =head2 options_from_resultset
259  
260 Usage : options_from_resultset( $form, $result_set )
261 Purpose : loads options for SELECT boxes from database tables
262 Returns :
263 Argument : $form - Rose::HTML::Form, $result_set - DBIx::Class::ResultSet
264 Throws :
265 Comment :
266 :
267  
268  
269 =head1 BUGS
270  
271  
272 =head1 SUPPORT
273 #rdbo at irc.perl.org
274  
275  
276 =head1 AUTHOR
277  
278 Zbigniew Lukasiak
279 CPAN ID: ZBY
280 http://perlalchemy.blogspot.com
281  
282 =head1 COPYRIGHT
283  
284 This program is free software; you can redistribute
285 it and/or modify it under the same terms as Perl itself.
286  
287 The full text of the license can be found in the
288 LICENSE file included with this module.
289  
290  
291 =head1 SEE ALSO
292  
293 perl(1).
294  
295 =cut
296  
297 #################### main pod documentation end ###################
298  
299  
300 3 1;
301 # The preceding line will help the module return a true value
302  
↑ Summary DBIC/FormGenerator.pm »