| 1 | package DBIx::Class::WebForm; | ||||||||||||||||||||||||||||
| 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 HTML::Element; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | DBIx::Class::WebForm - CRUD Methods For DBIx::Class | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use base 'DBIx::Class::WebForm'; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $results = Data::FormValidator->check( ... ); | ||||||||||||||||||||||||||||
| 18 | my $film = Film->retrieve('Fahrenheit 911'); | ||||||||||||||||||||||||||||
| 19 | $film->update_from_form($results); | ||||||||||||||||||||||||||||
| 20 | my $new_film = Film->create_from_form($results); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | CRUD Methods For DBIx::Class. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 METHODS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =item $class->create_from_form($form) | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub create_from_form { | ||||||||||||||||||||||||||
|
Subroutine
create_from_form: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | die "create_from_form can only be called as a class method" if ref $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | __PACKAGE__->_run_create( $class, @_ ); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item $self->update_from_form($form) | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub update_from_form { | ||||||||||||||||||||||||||
|
Subroutine
update_from_form: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | die "update_from_form cannot be called as a class method" unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | __PACKAGE__->_run_update( $self, @_ ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub _run_create { | ||||||||||||||||||||||||||
|
Subroutine
_run_create: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 51 | 0 | my ( $me, $class, $results ) = @_; | |||||||||||||||||||||||||||
| 52 | 0 | my $them = bless {}, $class; | |||||||||||||||||||||||||||
| 53 | 0 | my $cols = {}; | |||||||||||||||||||||||||||
| 54 | 0 | foreach my $col ( $them->columns ) { | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if(defined($results->valid($col))) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 0 | $cols->{$col} = $results->valid($col); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | return $class->create($cols); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub _run_update { | ||||||||||||||||||||||||||
|
Subroutine
_run_update: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 64 | 0 | my ( $me, $them, $results ) = @_; | |||||||||||||||||||||||||||
| 65 | 0 | my %pk; | |||||||||||||||||||||||||||
| 66 | 0 | $pk{$_} = 1 for $them->primary_columns; | |||||||||||||||||||||||||||
| 67 | 0 | foreach my $col ( keys %{ $results->valid } ) { | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( $them->can($col) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | next if $pk{$col}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | my $val = $results->valid($col); | |||||||||||||||||||||||||||
| 71 | 0 | $them->$col($val); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 0 | $them->update; | |||||||||||||||||||||||||||
| 75 | 0 | return 1; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item $class->to_cgi | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub to_cgi { | ||||||||||||||||||||||||||
|
Subroutine
to_cgi: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 84 | 0 | map { $_ => $class->to_field($_) } $class->columns; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =item $self->to_field( $field, $how ) | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub to_field { | ||||||||||||||||||||||||||
|
Subroutine
to_field: not called CC=8 SCAR=40.1
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 0 | my ( $self, $field, $how ) = @_; | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( $how and $how =~ /^(text(area|field)|select)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$how and $how =~ /^(text(area|field)|select)$/
MC/DC0%0/2
$how and $how =~ /^(text(area|field)|select)$/
$how
$how =~ /^(text(area|field)|select)$/
Truth table0%0/3
A and B
A= $howB= $how =~ /^(text(area|field)|select)$/
| |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 0 | my $meth = "_to_$how"; | |||||||||||||||||||||||||||
| 97 | 0 | return $class->$meth($field); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 0 | my $hasa = $class->_relationships->{$field}; | |||||||||||||||||||||||||||
| 100 | return $self->_to_select($field) | ||||||||||||||||||||||||||||
| 101 | if defined $hasa | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | and $class->resolve_class( $hasa->{class} )->isa("DBIx::Class"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $hasa and $class->resolve_class($hasa->{"class"})->isa("DBIx::Class")
MC/DC0%0/2
defined $hasa and $class->resolve_class($hasa->{"class"})->isa("DBIx::Class")
defined $hasa
$class->resolve_class($hasa->{"class"})->isa("DBIx::Class")
Truth table0%0/3
A and B
A= defined $hasaB= $class->resolve_class($hasa->{"class"})->isa("DBIx::Class")
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my $type = $class->column_type($field); | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | return $self->_to_textarea($field) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$type and $type =~ /^(TEXT|BLOB)$/i
MC/DC0%0/2
$type and $type =~ /^(TEXT|BLOB)$/i
$type
$type =~ /^(TEXT|BLOB)$/i
Truth table0%0/3
A and B
A= $typeB= $type =~ /^(TEXT|BLOB)$/i
| |||||||||||||||||||||||||||||
| 106 | if $type | ||||||||||||||||||||||||||||
| 107 | and $type =~ /^(TEXT|BLOB)$/i; | ||||||||||||||||||||||||||||
| 108 | 0 | return $self->_to_textfield($field); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub _to_textarea { | ||||||||||||||||||||||||||
|
Subroutine
_to_textarea: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 112 | 0 | my ( $self, $col ) = @_; | |||||||||||||||||||||||||||
| 113 | 0 | my $a = HTML::Element->new( | |||||||||||||||||||||||||||
| 114 | "textarea", | ||||||||||||||||||||||||||||
| 115 | name => $col, | ||||||||||||||||||||||||||||
| 116 | rows => "3", | ||||||||||||||||||||||||||||
| 117 | cols => "22" | ||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ( ref $self ) { $a->push_content( $self->$col ) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $a; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub _to_textfield { | ||||||||||||||||||||||||||
|
Subroutine
_to_textfield: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 124 | 0 | my ( $self, $col ) = @_; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $value = ref $self && $self->$col; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $self && $self->$col
MC/DC0%0/2
ref $self && $self->$col
ref $self
$self->$col
Truth table0%0/3
A && B
A= ref $selfB= $self->$col
| |||||||||||||||||||||||||||||
| 126 | 0 | my $a = HTML::Element->new( "input", type => "text", name => $col ); | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | $a->attr( "value" => $value ) if $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $a; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub _to_select { | ||||||||||||||||||||||||||
|
Subroutine
_to_select: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 132 | 0 | my ( $self, $col, $hint ) = @_; | |||||||||||||||||||||||||||
| 133 | my $has_a_class = $hint | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | || $self->resolve_class( $self->_relationships->{$col}->{class} ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$hint || $self->resolve_class($self->_relationships->{$col}{'class'})
MC/DC0%0/2
$hint || $self->resolve_class($self->_relationships->{$col}{'class'})
$hint
$self->resolve_class($self->_relationships->{$col}{'class'})
Truth table0%0/3
A || B
A= $hintB= $self->resolve_class($self->_relationships->{$col}{'class'})
| |||||||||||||||||||||||||||||
| 135 | 0 | my @objs = $has_a_class->search; | |||||||||||||||||||||||||||
| 136 | 0 | my $a = HTML::Element->new( "select", name => $col ); | |||||||||||||||||||||||||||
| 137 | 0 | for (@objs) { | |||||||||||||||||||||||||||
| 138 | 0 | my $sel = HTML::Element->new( "option", value => $_->id ); | |||||||||||||||||||||||||||
| 139 | $sel->attr( "selected" => "selected" ) | ||||||||||||||||||||||||||||
| 140 | if ref $self | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | and eval { $_->id eq $self->$col->id }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $self and eval {
do {
$_->id eq $self->$col->id
}
}
MC/DC0%0/2
ref $self and eval {
do {
$_->id eq $self->$col->id
}
}
ref $self
eval {
do {
$_->id eq $self->$col->id
}
}
Truth table0%0/3
A and B
A= ref $selfB= eval {
do {
$_->id eq $self->$col->id
}
}
| |||||||||||||||||||||||||||||
| 142 | 0 | $sel->push_content( $_ . '' ); | |||||||||||||||||||||||||||
| 143 | 0 | $a->push_content($sel); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 0 | $a; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item $class->column_type($col) | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub column_type { | ||||||||||||||||||||||||||
|
Subroutine
column_type: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 0 | my ( $class, $col ) = @_; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | return if(!$class->has_column($col)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | return $class->column_info($col)->{data_type}; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =back | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Matt S. Trout | ||||||||||||||||||||||||||||
| 165 | Sebastian Riedel, C<sri@oook.de> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | This library is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 170 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1 | 1; | |||||||||||||||||||||||||||