| 1 | package WebNano::Controller::CRUD; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 1 | $WebNano::Controller::CRUD::VERSION = '0.007'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use MooseX::NonMoose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Class::MOP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Spec::Functions 'catdir'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | extends 'WebNano::Controller'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | has form_class => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 13 | 1 | has rs_name => ( is => 'ro', isa => 'Str', lazy_build => 1, ); | |||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub _build_rs_name { | ||||||||||||||||||||||||||
|
Subroutine
_build_rs_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 15 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 16 | 0 | my $my_name = ref $self; | |||||||||||||||||||||||||||
| 17 | 0 | $my_name =~ /::(\w+)$/; | |||||||||||||||||||||||||||
| 18 | 0 | return $1; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | has record_actions => ( | ||||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||
| 23 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 24 | 24 | ▶ | default => sub { { view => 1, 'delete' => 1, edit => 1 } } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | has primary_columns => ( | |||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||
| 29 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | 24 | ▶ | sub _build_primary_columns { | ||||||||||||||||||||||||||
|
Subroutine
_build_primary_columns: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 24 | my $source = $self->app->schema->source( $self->rs_name ); | |||||||||||||||||||||||||||
| 34 | 24 | return [ $source->primary_columns ]; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | my $FULLPATH; | |||||||||||||||||||||||||||
| 38 | 1 | ▶ | BEGIN { use Cwd (); $FULLPATH = Cwd::abs_path(__FILE__) } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 14 | ▶ | sub template_search_path { | ||||||||||||||||||||||||||
|
Subroutine
template_search_path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 42 | 14 | my $mydir = $FULLPATH; | |||||||||||||||||||||||||||
| 43 | 14 | $mydir =~ s/.pm$//; | |||||||||||||||||||||||||||
| 44 | 14 | return [ catdir( $mydir, 'templates' ) ]; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 5 | ▶ | sub columns { | ||||||||||||||||||||||||||
|
Subroutine
columns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | 5 | my $source = $self->app->schema->source( $self->rs_name ); | |||||||||||||||||||||||||||
| 50 | 5 | return [ $source->columns ]; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 24 | ▶ | sub _get_parts { | ||||||||||||||||||||||||||
|
Subroutine
_get_parts: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 55 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | 24 | my @args = @{ $self->path }; | |||||||||||||||||||||||||||
| 57 | 24 | my @pks = @{ $self->primary_columns }; | |||||||||||||||||||||||||||
| 58 | 24 | my @ids; | |||||||||||||||||||||||||||
| 59 | 24 | for my $i ( 0 .. $#pks ){ | |||||||||||||||||||||||||||
| 60 | 24 | ▶ | if( defined $args[$i] && $args[$i] =~ /^\d+$/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args[$i] and $args[$i] =~ /^\d+$/
MC/DC100%2/2
defined $args[$i] and $args[$i] =~ /^\d+$/
defined $args[$i]
$args[$i] =~ /^\d+$/
Truth table100%3/3
A and B
A= defined $args[$i]B= $args[$i] =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 61 | 15 | push @ids, $args[$i]; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | else{ | ||||||||||||||||||||||||||||
| 64 | 9 | return; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 15 | ▶ | my $method = $args[ $#pks + 1 ] || 'view'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args[$#pks + 1] || 'view'
MC/DC0%0/1
$args[$#pks + 1] || 'view'
$args[$#pks + 1]
Truth table50%1/2
A
A= $args[$#pks + 1]
| |||||||||||||||||||||||||||||
| 68 | 15 | my $method_reg = join '|', keys %{ $self->record_actions }; | |||||||||||||||||||||||||||
| 69 | 15 | ▶ | return if $method !~ /$method_reg/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | return { | ||||||||||||||||||||||||||||
| 71 | 15 | ▶ | ids => \@ids, | ||||||||||||||||||||||||||
|
Condition50%1/2
$args[$#pks + 1] || 'view'
MC/DC0%0/1
$args[$#pks + 1] || 'view'
$args[$#pks + 1]
Truth table50%1/2
A
A= $args[$#pks + 1]
| |||||||||||||||||||||||||||||
| 72 | method => $args[ $#pks + 1 ] || 'view', | ||||||||||||||||||||||||||||
| 73 | args => [ @args[ $#pks + 2, $#args ] ], | ||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 24 | ▶ | around 'local_dispatch' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 78 | 24 | my( $orig, $self ) = @_; | |||||||||||||||||||||||||||
| 79 | 24 | ▶ | if( my $parsed = $self->_get_parts() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 15 | my $rs = $self->app->schema->resultset( $self->rs_name ); | |||||||||||||||||||||||||||
| 81 | 15 | my $record = $rs->find( @{ $parsed->{ids} } ); | |||||||||||||||||||||||||||
| 82 | 15 | ▶ | if( ! $record ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2 | my $res = $self->req->new_response(404); | |||||||||||||||||||||||||||
| 84 | 2 | $res->content_type('text/plain'); | |||||||||||||||||||||||||||
| 85 | 2 | $res->body( 'No record with ids: ' . join ' ', @{ $parsed->{ids} } ); | |||||||||||||||||||||||||||
| 86 | 2 | return $res; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 13 | my $method = $parsed->{method}; | |||||||||||||||||||||||||||
| 89 | 13 | return $self->$method( $record, @{ $parsed->{args} } ); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 9 | return $self->$orig(); | |||||||||||||||||||||||||||
| 92 | 1 | }; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub index_action { shift->list_action( @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
index_action: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 3 | ▶ | sub list_action { | ||||||||||||||||||||||||||
|
Subroutine
list_action: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 3 | my( $self ) = @_; | |||||||||||||||||||||||||||
| 98 | 3 | my $rs = $self->app->schema->resultset( $self->rs_name ); | |||||||||||||||||||||||||||
| 99 | 3 | return $self->render( template => 'list.tt', items => [ $rs->search ] ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 6 | ▶ | sub after_POST { | ||||||||||||||||||||||||||
|
Subroutine
after_POST: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 6 | my( $self, @ids ) = @_; | |||||||||||||||||||||||||||
| 104 | 6 | return $self->self_url . join( '/', @ids ) . '/view'; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 4 | ▶ | sub create_action { | ||||||||||||||||||||||||||
|
Subroutine
create_action: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 4 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 109 | 4 | my $req = $self->req; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 4 | my $form_class = $self->form_class; | |||||||||||||||||||||||||||
| 112 | 4 | Class::MOP::load_class( $form_class ); | |||||||||||||||||||||||||||
| 113 | 4 | my $item = $self->app->schema->resultset( $self->rs_name )->new_result( {} ); | |||||||||||||||||||||||||||
| 114 | 4 | my $params = $req->parameters->as_hashref_mixed; | |||||||||||||||||||||||||||
| 115 | 4 | my $form = $form_class->new( | |||||||||||||||||||||||||||
| 116 | params => $params, | ||||||||||||||||||||||||||||
| 117 | schema => $self->app->schema, | ||||||||||||||||||||||||||||
| 118 | item => $item, | ||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||
| 120 | 4 | ▶ | if( $req->method eq 'POST' && $form->process() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$req->method eq "POST" and $form->process
MC/DC50%1/2
$req->method eq "POST" and $form->process
$req->method eq "POST"
$form->process
Truth table66%2/3
A and B
A= $req->method eq "POST"B= $form->process
| |||||||||||||||||||||||||||||
| 121 | 2 | my $record = $form->item; | |||||||||||||||||||||||||||
| 122 | 2 | my $res = $req->new_response(); | |||||||||||||||||||||||||||
| 123 | 2 | $res->redirect( $self->after_POST( $record->id ) ); | |||||||||||||||||||||||||||
| 124 | 2 | return $res; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 2 | $form->field( 'submit' )->value( 'Create' ); | |||||||||||||||||||||||||||
| 127 | 2 | return $self->render( template => 'edit.tt', form => $form->render ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 5 | ▶ | sub view { | ||||||||||||||||||||||||||
|
Subroutine
view: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 5 | my ( $self, $record ) = @_; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 5 | return $self->render( template => 'record.tt', record => $record ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 0 | my ( $self, $record ) = @_; | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | if( $self->req->method eq 'GET' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | return $self->render( template => 'delete.tt', record => $record ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | else{ | ||||||||||||||||||||||||||||
| 143 | 0 | $record->delete; | |||||||||||||||||||||||||||
| 144 | 0 | my $res = $self->req->new_response(); | |||||||||||||||||||||||||||
| 145 | 0 | $res->redirect( $self->self_url ); | |||||||||||||||||||||||||||
| 146 | 0 | return $res; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 8 | ▶ | sub edit { | ||||||||||||||||||||||||||
|
Subroutine
edit: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 8 | my ( $self, $record ) = @_; | |||||||||||||||||||||||||||
| 152 | 8 | my $req = $self->req; | |||||||||||||||||||||||||||
| 153 | 8 | my $form_class = $self->form_class; | |||||||||||||||||||||||||||
| 154 | 8 | Class::MOP::load_class( $form_class ); | |||||||||||||||||||||||||||
| 155 | 8 | my $params = $req->parameters->as_hashref_mixed; | |||||||||||||||||||||||||||
| 156 | 8 | my $form = $form_class->new( | |||||||||||||||||||||||||||
| 157 | item => $record, | ||||||||||||||||||||||||||||
| 158 | params => $params, | ||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||
| 160 | 8 | ▶ | if( $req->method eq 'POST' && $form->process() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$req->method eq "POST" and $form->process
MC/DC100%2/2
$req->method eq "POST" and $form->process
$req->method eq "POST"
$form->process
Truth table100%3/3
A and B
A= $req->method eq "POST"B= $form->process
| |||||||||||||||||||||||||||||
| 161 | 4 | my $res = $req->new_response(); | |||||||||||||||||||||||||||
| 162 | 4 | $res->redirect( $self->after_POST( $record->id ) ); | |||||||||||||||||||||||||||
| 163 | 4 | return $res; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 4 | $form->field( 'submit' )->value( 'Update' ); | |||||||||||||||||||||||||||
| 166 | 4 | return $self->render( template => 'edit.tt', form => $form->render ); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 1 | 1; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =pod | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 NAME | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | WebNano::Controller::CRUD | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 VERSION | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | version 0.007 | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | use base 'WebNano::Controller::CRUD'; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | This is experimental Template Tookit and DBIx::Class based CRUD controller for L<WebNano> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 form_class | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head2 rs_name | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 record_actions | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head1 METHODS | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head2 create_action | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 delete | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head2 edit | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head2 index_action | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head2 list_action | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head2 parse_path | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head2 view | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head2 columns | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head2 after_POST | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Zbigniew Lukasiak <zby@cpan.org> | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | This software is Copyright (c) 2010 by Zbigniew Lukasiak <zby@cpan.org>. | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||||||||||