blib/lib/Form/Processor/Model/RDBO.pm

filter: Statementstmt 89.9% 116 / 129 Branchbran 66.2% 49 / 74 Conditioncond 62.1% 23 / 37 MC/DCmcdc 27.2% 6 / 22 Subroutinesub 100% 14 / 14 Podpod 100% 11 / 11 totaltotal 76.3% 219 / 287 CC 53 SCAR 23.7
CC
53
Cov
78%
init_item
26.3
lookup_options
16.5
init_value
10.7
CRAP 10.7
↑ Summary
1 package Form::Processor::Model::RDBO;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use base 'Form::Processor';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 require UNIVERSAL::require;
6  
7 16 our $VERSION = '0.02';
8  
9 =head1 NAME
10  
11 Form::Processor::Model::RDBO - Model class for Form Processor using
12 Rose::DB::Object
13  
14 =head1 SYNOPSIS
15  
16 Same API as in L<Form::Processor::Model::DBIC>. For more documentation and
17 examples see it. Here are written only L<Rose::DB::Object> special cases.
18  
19 =head1 METHODS
20  
21 =head2 manager_module
22  
23 Defines the default name for Rose::DB::Object managers. You are free to override
24 this property if the name of your Manager module differs.
25  
26 =cut
27  
28 34 ▶sub manager_module { 'Manager' }
Subroutine manager_module: called CC=1 SCAR=0.0
Pod documented
29  
30 =head2 init_item
31  
32 This is called on the first call of $form->item. It also validates that the item
33 id matches /^\d+$/. Override this method in your form class (or form base class)
34 if your ids do not match that pattern.
35  
36 =cut
37  
38 21 ▶sub init_item {
Subroutine init_item: called CC=6 SCAR=32.7
Pod documented
39 21 my $self = shift;
40 21 my $item_id = $self->item_id;
41  
42 21 ▶ return unless defined $item_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item_id 21 0 21
43 0 ▶ return unless $item_id =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $item_id =~ /^\d+$/ 0 0 0
44  
45 0 my $object = $self->object_class;
46  
47 0 ▶ return unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 0 0
48 0 ▶ return unless $object->isa('Rose::DB::Object');
Branch: 1 branch
BranchTrueFalseTotal
unless $object->isa("Rose::DB::Object") 0 0 0
49
50 0 my ( $primary_key ) = ( $object->meta->primary_key_columns );
51 0 ▶ return unless $primary_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $primary_key 0 0 0
52  
53 0 my $item = $object->new( $primary_key => $item_id );
54 0 $item->load;
55  
56 0 $self->reset_params;
57  
58 0 return $item;
59 }
60  
61 =head2 init_value
62  
63 Populate $field->value with object ids from the Rose::DB::Object object. If the
64 column expands to more than one object then an array ref is set.
65  
66 =cut
67  
68 21 ▶sub init_value {
Subroutine init_value: called CC=8 SCAR=23.7
Pod documented
69 21 my ( $self, $field, $item ) = @_;
70  
71 21 my $column = $field->name;
72  
73 21 ▶ $item ||= $self->item;
Condition33%1/3
$item ||= $self->item
l!l&&r!l&&!r
2100
MC/DC0%0/2
$item ||= $self->item
$item $self->item
Truth table33%1/3
A ||= B
A= $itemB= $self->item
ABresult
1X1
011
000
74  
75 21 ▶ return $item->{$column} if ref($item) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $item eq "HASH" 0 21 21
76  
77 # Use "can" instead of "find_column" because could be a related column
78 21 ▶ return unless $item && $item->isa('Rose::DB::Object') && $item->can( $column );
Branch: 1 branch
BranchTrueFalseTotal
unless $item and $item->isa("Rose::DB::Object") and $item->can($column) 0 21 21
Condition33%2/6
$item and $item->isa("Rose::DB::Object") and $item->can($column)
!ll&&!rl&&r
0021
$item and $item->isa("Rose::DB::Object")
!ll&&!rl&&r
0021
MC/DC0%0/3
$item and $item->isa("Rose::DB::Object") and $item->can($column)
$item $item->isa("Rose::DB::Object") $item->can($column)
Truth table25%1/4
A and B and C
A= $itemB= $item->isa("Rose::DB::Object")C= $item->can($column)
ABCresult
0XX0
10X0
1100
1111
79  
80 # @options can be a collection of RDBO objects (has_many) or a
81 # RDBO objects get turned into IDs. Should also check that it's not a compound
82 # primary key.
83  
84 my @values =
85 21 ▶ map { ref $_ && $_->isa( 'Rose::DB::Object' ) ? $_->id : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_->isa('Rose::DB::Object') ? : 6 7 13
Condition66%2/3
ref $_ and $_->isa("Rose::DB::Object")
!ll&&!rl&&r
706
MC/DC50%1/2
ref $_ and $_->isa("Rose::DB::Object")
ref $_ $_->isa("Rose::DB::Object")
Truth table66%2/3
A and B
A= ref $_B= $_->isa("Rose::DB::Object")
ABresult
0X0
100
111
86 $item->$column;
87  
88 21 return @values;
89  
90 }
91  
92 =head2 guess_field_type
93  
94 Tries to guess field type of the passed column. Currently only looks at RDBO
95 relationships. And returns the type 'Select' for 'one to one' or 'many to one'
96 relations and 'Text' in other cases.
97  
98 =cut
99  
100 sub guess_field_type
101 3 ▶{
Subroutine guess_field_type: called CC=5 SCAR=16.1
Pod documented
102 3 my ( $self, $column ) = @_;
103 3 my $object_meta = $self->object_class->meta;
104 3 my $foreign_item ;
105 my $r_type;
106  
107 3 ▶ if ( $foreign_item = $object_meta->foreign_key( $column ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($foreign_item = $object_meta->foreign_key($column)) { } 1 2 3
elsif ($foreign_item = $object_meta->relationship($column)) { } 1 1 2
108 1 $r_type = $foreign_item->relationship_type;
109 }
110 elsif ( $foreign_item = $object_meta->relationship( $column ) ) {
111 1 $r_type = $foreign_item->type;
112 }
113 else {
114 1 $r_type = '';
115 }
116  
117 3 my $guessed_field_type;
118 3 ▶ if ( $r_type eq 'one to one' || $r_type eq 'many to one' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r_type eq 'one to one' or $r_type eq 'many to one') { } 2 1 3
Condition66%2/3
$r_type eq "one to one" or $r_type eq "many to one"
l!l&&r!l&&!r
021
MC/DC50%1/2
$r_type eq "one to one" or $r_type eq "many to one"
$r_type eq "one to one" $r_type eq "many to one"
Truth table66%2/3
A or B
A= $r_type eq "one to one"B= $r_type eq "many to one"
ABresult
1X1
011
000
119 2 $guessed_field_type = 'Select';
120 }
121 1 else { $guessed_field_type = 'Text'; }
122  
123 3 return $guessed_field_type;
124 }
125  
126 =head2 lookup_options
127  
128 Tries to get options for the filed, derived from
129 L<Form::Processor::Field::Select> (e.g. Select and Multiple) from the database.
130 First, it asks foreign RDBO class if it can 'active_column'. Active columns is
131 used for labeling select fields.
132  
133 package Schema::RDBO::Current;
134 use strict;
135 use base qw(DB::Object);
136 __PACKAGE__->meta->setup(
137 foreign_keys =>
138 [
139 foreign_fk => {
140 class => 'RDBO::ForeignTable'
141 }
142 ]
143 );
144 1;
145  
146 package Schema::RDBO::ForeignTable;
147 use strict;
148 use base qw(DB::Object);
149 __PACKAGE__->meta->setup(
150 columns => [
151 qw/ id title /
152 ],
153 );
154  
155 sub active_column { 'title' }
156  
157 1;
158  
159 Value returned by active_column method is used in building sql. If not defined
160 it will be set to the default 'name'. If you want to have freedom how to set up
161 label and active_column is not enough, you can declare active_column_method
162 method that is called when labeling value as a method of retreived object.
163  
164 sub active_column_method {
165 ucfirst $_[0]->title;
166 }
167  
168 If no active_column method was found it gets default 'name' value. If active
169 column exists in the foreign RDBO schema class, reference to array of key/value
170 pairs will be returned and the options for field will be filled automatically.
171 Otherwise it will return the empty array.
172  
173 =cut
174  
175 34 ▶sub lookup_options {
Subroutine lookup_options: called CC=15 SCAR=28.0
Pod documented
176 34 my ( $self, $field ) = @_;
177 34 my $object_class = $self->object_class;
178 34 my $prefix = $self->name_prefix;
179  
180 34 my $field_name = $field->name;
181 34 ▶ $field_name =~ s/^$prefix\.//g if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 34 34
182  
183 34 my $field_type = $field->type;
184 # NOTE: "Multiple" is derived class from "Select".
185 # This code will work for all classes derived from "Select".
186 34 ▶ return unless $field->isa( "Form::Processor::Field::Select" );
Branch: 1 branch
BranchTrueFalseTotal
unless $field->isa("Form::Processor::Field::Select") 0 34 34
187 34 ▶ return unless $object_class->isa('Rose::DB::Object');
Branch: 1 branch
BranchTrueFalseTotal
unless $object_class->isa("Rose::DB::Object") 0 34 34
188  
189 34 my $object_meta = $object_class->meta;
190 34 my $foreign_item;
191 my $foreign_class;
192 34 my $foreign_manager;
193  
194 34 $foreign_item = $object_meta->foreign_key($field_name);
195 34 ▶ if ( !$foreign_item ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($foreign_item) 30 4 34
196 30 $foreign_item = $object_meta->relationship($field_name);
197 }
198 34 ▶ return unless $foreign_item;
Branch: 1 branch
BranchTrueFalseTotal
unless $foreign_item 0 34 34
199  
200 34 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($foreign_item->isa('Rose::DB::Object::Metadata::Relationship::ManyToMany')) { } 13 21 34
201 $foreign_item->isa(
202 'Rose::DB::Object::Metadata::Relationship::ManyToMany'
203 )
204 )
205 {
206 13 $foreign_class = $foreign_item->foreign_class;
207 } else {
208 21 $foreign_class = $foreign_item->class;
209 }
210  
211 34 $foreign_manager = $foreign_class . '::' . $self->manager_module;
212  
213 # Trying to require Manager class
214 34 $foreign_manager->require;
215 34 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 34 0 34
216 34 $foreign_manager = 'Rose::DB::Object::Manager';
217 34 ▶ $foreign_manager->require or die "Unable to require $foreign_manager";
Branch: 1 branch
BranchTrueFalseTotal
$foreign_manager->require or die "Unable to require $foreign_manager" 34 0 34
218 }
219  
220 # Trying to determinate the active column the default value for
221 # active_column is "name"
222 34 my $active_column;
223 34 ▶ if ( $foreign_class->can('active_column') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($foreign_class->can('active_column')) { } 13 21 34
elsif ($self->can('active_column')) { } 0 21 21
224 13 $active_column = $foreign_class->active_column;
225 } elsif ( $self->can('active_column') ) {
226 0 $active_column = $self->active_column;
227 }
228  
229 # Check that active_column exists in foreign class columns list
230 # If the active column does not exist in foreign class columns list
231 # set it to '' (boolean false)
232 #my @foreign_columns = $foreign_class->meta->column_names;
233 #$active_column = '' unless grep { $_ eq $active_column } @foreign_columns;
234 # FIXME: can't simply check if column exists because relative column can be
235 # specified: 't2.title'
236  
237 34 ▶ $active_column = "name" unless $active_column;
Branch: 1 branch
BranchTrueFalseTotal
unless $active_column 21 13 34
238  
239 34 my ( $primary_key ) = ( $foreign_class->meta->primary_key_column_names );
240  
241 34 my @options;
242 # Fetch options only if active_column exists
243  
244 34 ▶ if ( $active_column ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active_column) { } 34 0 34
245 34 my %m_params = (
246 'object_class' => $foreign_class,
247 'select' => [ $primary_key, $active_column ],
248 );
249 34 my $records = $foreign_manager->get_objects( %m_params);
250  
251 34 foreach my $r ( @$records ) {
252 29 ▶ push @options,
Branch: 1 branch
BranchTrueFalseTotal
$r->can('active_column_method') ? : 0 29 29
253 (
254 $r->$primary_key => $r->can( 'active_column_method' )
255 ? $r->active_column_method()
256 : $r->$active_column
257 );
258 }
259 } else {
260 0 warn 'active_column is undefined';
261 0 @options = ();
262 }
263
264 34 return \@options;
265 }
266  
267 =head2 update_from_form
268  
269 Makes form validation and returns the filled RDBO item, filled by update_model.
270 It does not save the current item automatically. You should do this in your
271 code, after the call update_from_form.
272  
273 =cut
274  
275 11 ▶sub update_from_form {
Subroutine update_from_form: called CC=2 SCAR=7.1
Pod documented
276 11 my ( $self, $params ) = @_;
277 11 ▶ return unless $self->validate( $params );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate($params) 0 11 11
278 11 return $self->update_model( $params );
279 }
280  
281 =head2 update_model
282  
283 Returns the new RDBO item, filled with passed form params. It does not save any
284 data to the database, just creates and fills new RDBO object.
285  
286 =cut
287  
288 11 ▶sub update_model {
Subroutine update_model: called CC=9 SCAR=22.0
Pod documented
289 11 my ( $self, $params ) = @_;
290  
291 11 my $item = $self->item;
292 11 ▶ my $class = ref( $item ) || $self->object_class;
Condition66%2/3
ref $item || $self->object_class
l!l&&r!l&&!r
650
MC/DC0%0/2
ref $item || $self->object_class
ref $item $self->object_class
Truth table66%2/3
A || B
A= ref $itemB= $self->object_class
ABresult
1X1
011
000
293  
294 11 my %fields = map { $_->name, $_ } grep { !$_->noupdate } $self->fields;
295  
296 11 ▶ $item = $class->new() unless $item;
Branch: 1 branch
BranchTrueFalseTotal
unless $item 5 6 11
297 11 ▶ while ( my ( $name, $field ) = each %fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %fields) 33 11 44
298 33 ▶ if ( $item->can( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->can($name)) 33 0 33
299 33 ▶ if ( !$field->isa( 'Form::Processor::Field::Select' )
Branch: 1 branch
BranchTrueFalseTotal
if (not $field->isa("Form::Processor::Field::Select") and defined $item->$name and defined $field->value) 6 27 33
Condition83%5/6
not $field->isa("Form::Processor::Field::Select") and defined $item->$name and defined $field->value
!ll&&!rl&&r
2706
not $field->isa("Form::Processor::Field::Select") and defined $item->$name
!ll&&!rl&&r
2256
MC/DC66%2/3
not $field->isa("Form::Processor::Field::Select") and defined $item->$name and defined $field->value
not $field->isa("Form::Processor::Field::Select") defined $item->$name defined $field->value
Truth table75%3/4
not(A) and B and C
A= not $field->isa("Form::Processor::Field::Select")B= defined $item->$nameC= defined $field->value
ABCresult
0XX0
10X0
1100
1111
300 && defined $item->$name
301 && defined $field->value )
302 {
303 6 ▶ next if $item->$name eq $field->value;
Branch: 1 branch
BranchTrueFalseTotal
if $item->$name eq $field->value 2 4 6
304 }
305  
306 31 $item->$name( $field->value );
307 }
308 }
309  
310 11 return $item;
311 }
312  
313 =head2 model_validate
314  
315 Does simple validation of fields, that you specified as unique in your form
316 profile. You are free to override this method if you want to implement much
317 serious model validation.
318  
319 =cut
320  
321 sub model_validate
322 14 ▶{
Subroutine model_validate: called CC=2 SCAR=6.9
Pod documented
323 14 my ($self) = @_;
324 14 ▶ return unless $self->validate_unique;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_unique 1 13 14
325 13 return 1;
326 }
327  
328 =head2 validate_unique
329  
330 Internal method. Does simple validation of fields, that you specified as unique
331 in your form profile.
332  
333 =cut
334  
335 14 ▶sub validate_unique {
Subroutine validate_unique: called CC=8 SCAR=20.9
Pod documented
336 14 my ( $self ) = @_;
337  
338 14 ▶ my $unique = $self->profile->{ unique } || return 1;
Condition100%2/2
$self->profile->{'unique'} || (return 1)
l!l
122
MC/DC100%1/1
$self->profile->{'unique'} || (return 1)
$self->profile->{'unique'}
Truth table100%2/2
A
A= $self->profile->{'unique'}
Aresult
11
00
339  
340 12 my $item = $self->item;
341  
342 12 ▶ my $class = ref( $item ) || $self->object_class;
Condition66%2/3
ref $item || $self->object_class
l!l&&r!l&&!r
660
MC/DC0%0/2
ref $item || $self->object_class
ref $item $self->object_class
Truth table66%2/3
A || B
A= ref $itemB= $self->object_class
ABresult
1X1
011
000
343  
344 12 my $found_error = 0;
345  
346 12 for my $field ( map { $self->field( $_ ) } @$unique ) {
347 12 ▶ next if $field->errors;
Branch: 1 branch
BranchTrueFalseTotal
if $field->errors 0 12 12
348  
349 12 my $value = $field->value;
350 12 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 12 12
351  
352 12 my $name = $field->name;
353  
354 # unique means there can only be on in the database like it.
355 12 my $match = $class->new( $name => $value );
356 12 $match->load( speculative => 1 );
357 12 ▶ next if $match->not_found;
Branch: 1 branch
BranchTrueFalseTotal
if $match->not_found 8 4 12
358  
359 4 ▶ next if $self->items_same( $item, $match );
Branch: 1 branch
BranchTrueFalseTotal
if $self->items_same($item, $match) 3 1 4
360  
361 1 $field->add_error( 'Value must be unique in the database' );
362 1 $found_error++;
363 }
364  
365 12 return !$found_error;
366 }
367  
368 =head2 items_same
369  
370 Internal method. Compares two RDBO items. Returns true, if the items are same.
371  
372 =cut
373  
374 4 ▶sub items_same {
Subroutine items_same: called CC=5 SCAR=16.6
Pod documented
375 4 my ( $self, $item1, $item2 ) = @_;
376  
377 # returns true if both are undefined
378 4 ▶ return 1 if not defined $item1 and not defined $item2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item1 or defined $item2 0 4 4
Condition66%2/3
defined $item1 or defined $item2
l!l&&r!l&&!r
310
MC/DC0%0/2
defined $item1 or defined $item2
defined $item1 defined $item2
Truth table66%2/3
A or B
A= defined $item1B= defined $item2
ABresult
1X1
011
000
379  
380 # return false if either undefined
381 4 ▶ return unless defined $item1 and defined $item2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item1 and defined $item2 1 3 4
Condition66%2/3
defined $item1 and defined $item2
!ll&&!rl&&r
103
MC/DC50%1/2
defined $item1 and defined $item2
defined $item1 defined $item2
Truth table66%2/3
A and B
A= defined $item1B= defined $item2
ABresult
0X0
100
111
382  
383 3 return $self->primary_key( $item1 ) eq $self->primary_key( $item2 );
384 }
385  
386 =head2 primary_key
387  
388 Internal method. Returns the primary key of items. Joins compound primary key in
389 one string value. Used in method items_same.
390  
391 =cut
392  
393 6 ▶sub primary_key {
Subroutine primary_key: called CC=2 SCAR=7.1
Pod documented
394 6 my ( $self, $item ) = @_;
395 return join '|',
396 $item->meta->table,
397 6 ▶ map { $_ . '=' . ( $item->$_ || '.' ) } $item->meta->primary_key_columns;
Condition50%1/2
$item->$_ || '.'
l!l
60
MC/DC0%0/1
$item->$_ || '.'
$item->$_
Truth table50%1/2
A
A= $item->$_
Aresult
11
00
398 }
399  
400 16 1;
401  
402 =head1 AUTHORS
403  
404 vti <vti@cpan.org>;
405 dzhariy <dzhariy@cpan.org>
406  
407 =head1 CONTRIBUTORS
408  
409 L<Form::Processor::Model::CDBI> written by Bill Moseley;
410 L<Form::Processor::Model::DBIC> ( Gerda Shank )
411  
412 A big part of code and documentation from those two modules is adapted and
413 used for current Form::Processor::Model::RDBO.
414  
415 =head1 LICENSE
416  
417 This library is free software, you can redistribute it and/or modify it under
418 the same terms as Perl itself.
419  
420 =head1 SEE ALSO
421  
422 L<Form::Processor>
423 L<Form::Processor::Model::CDBI>
424 L<Form::Processor::Model::DBIC>
425 L<Catalyst::Plugin::Form::Processor>
426 L<Rose::Object>
427 L<Rose::DB::Object>
428 L<Rose::DB::Object::Tutorial>
429  
430 =cut
431  
432 16 1;
↑ Summary