AproJo.pm

filter: Statementstmt 93.0% 94 / 101 Branchbran 46.6% 14 / 30 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 94.7% 18 / 19 Podpod 100% 1 / 1 totaltotal 80.8% 135 / 167 CC 20 SCAR 21.8
CC
20
Cov
81%
__ANON__
42.0
__ANON__
5.0
__ANON__
4.0
CRAP 8.8
« AproJo/Admin.pm ↑ Summary AproJo/Command/setup.pm »
1 package AproJo;
2 2 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.019';
7  
8 2 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Spec::Functions qw'rel2abs catdir';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Mojo::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶has db => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
17 0 my $self = shift;
18 my $schema_class = $self->config->{db_schema}
19 0 ▶ or die "Unknown DB Schema Class";
Branch: 1 branch
BranchTrueFalseTotal
my $schema_class = $self->config->{"db_schema"} or die "Unknown DB Schema Class" 0 0 0
20 0 ▶ eval "require $schema_class"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $schema_class" or die "Could not load Schema Class ($schema_class), $@\n" 0 0 0
21 or die "Could not load Schema Class ($schema_class), $@\n";
22  
23 my $db_connect = $self->config->{db_connect}
24 0 ▶ or die "No DBI connection string provided";
Branch: 1 branch
BranchTrueFalseTotal
my $db_connect = $self->config->{"db_connect"} or die "No DBI connection string provided" 0 0 0
25 0 ▶ my @db_connect = ref $db_connect ? @$db_connect : ($db_connect);
Branch: 1 branch
BranchTrueFalseTotal
ref $db_connect ? : 0 0 0
26  
27 0 ▶ my $schema = $schema_class->connect(@db_connect)
Branch: 1 branch
BranchTrueFalseTotal
my $schema = $schema_class->connect(@db_connect) or die "Could not connect to $schema_class using $db_connect[0]" 0 0 0
28 or die "Could not connect to $schema_class using $db_connect[0]";
29  
30 0 return $schema;
31 2 };
32  
33 2 has app_debug => 0;
34  
35 1 ▶has home => sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
36 1 ▶ my $path = $ENV{MOJO_HOME} || getcwd;
Condition33%1/3
$ENV{'MOJO_HOME'} || getcwd()
l!l&&r!l&&!r
010
MC/DC0%0/2
$ENV{'MOJO_HOME'} || getcwd()
$ENV{'MOJO_HOME'} getcwd()
Truth table33%1/3
A || B
A= $ENV{'MOJO_HOME'}B= getcwd()
ABresult
1X1
011
000
37 1 return Mojo::Home->new(File::Spec->rel2abs($path));
38 2 };
39  
40 1 ▶has config_file => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
41 1 my $self = shift;
42 1 ▶ return $ENV{APROJO_CONFIG} if $ENV{APROJO_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"APROJO_CONFIG"} 0 1 1
43  
44 1 return $self->home->rel_file('aprojo.conf');
45 2 };
46  
47 1 ▶sub startup {
Subroutine startup: called CC=3 SCAR=11.0
Pod documented
48 1 my $app = shift;
49  
50 1 $app->plugin(
51 Config => {
52 file => $app->config_file,
53 default => {
54 'db_connect' => [
55 'dbi:SQLite:dbname=' . $app->home->rel_file('aprojo.db'),
56 undef,
57 undef,
58 {'sqlite_unicode' => 1}
59 ],
60 'db_schema' => 'AproJo::DB::Schema',
61 'secret' => '47110815'
62 },
63 }
64 );
65  
66 {
67 # use content from directories under share/files or using File::ShareDir
68 1 my $lib_base = catdir(dirname(rel2abs(__FILE__)), '..', 'share','files');
69  
70 1 my $public = catdir($lib_base, 'public');
71 1 ▶ $app->static->paths->[0] = -d $public ? $public : catdir(dist_dir('AproJo'), 'files','public');
Branch: 1 branch
BranchTrueFalseTotal
-d $public ? : 0 1 1
72 1 my $static_path = $app->static->paths->[0];
73 #print STDERR '$static_path: ',$static_path,"\n";
74  
75 1 my $templates = catdir($lib_base, 'templates');
76 1 ▶ $app->renderer->paths->[0] = -d $templates ? $templates : catdir(dist_dir('AproJo'), 'files', 'templates');
Branch: 1 branch
BranchTrueFalseTotal
-d $templates ? : 0 1 1
77 }
78  
79 1 $app->plugin('I18N');
80 1 $app->plugin('Mojolicious::Plugin::ServerInfo');
81 1 $app->plugin('Mojolicious::Plugin::DBInfo');
82  
83 1 $app->plugin('Mojolicious::Plugin::Form');
84  
85 1 push @{$app->commands->namespaces}, 'AproJo::Command';
86  
87 #DEPRECATED: $app->secret( $app->config->{secret} );
88 1 $app->secrets([$app->config->{secret}]);
89  
90 1 ▶ $app->helper(schema => sub { shift->app->db });
Subroutine __ANON__: called CC=1 SCAR=0.0
91  
92 1 ▶ $app->helper('home_page' => sub {'/'});
Subroutine __ANON__: called CC=1 SCAR=0.0
93  
94 $app->helper(
95 2 ▶ 'source_id' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
96 2 my ($self, $source) = @_;
97 2 ▶ return undef unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 1 1 2
98 1 my @columns = $self->schema->source($source)->columns;
99 1 my $table_name = $self->schema->class($source)->table;
100 1 my $source_id = $table_name . '_id';
101 1 ▶ return $source_id if (grep {/$source_id/} @columns);
Branch: 1 branch
BranchTrueFalseTotal
if grep {/$source_id/u;} @columns 0 1 1
102 1 ▶ return $columns[0] if (scalar @columns);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @columns 1 0 1
103 }
104 1 );
105  
106 $app->helper(
107 15 ▶ 'get_user' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
108 15 my ($self, $name) = @_;
109 15 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 6 9 15
110 6 $name = $self->session->{username};
111 }
112 15 ▶ return undef unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 14 15
113 #say STDERR 'get_user: ', $name if $self->app->app_debug;
114 14 return $self->schema->resultset('User')->single({name => $name});
115 }
116 1 );
117  
118 $app->helper(
119 10 ▶ 'has_role' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
120 10 my $self = shift;
121 10 ▶ my $user_string = shift || '';
Condition100%2/2
shift() || ''
l!l
46
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
122 10 ▶ my $role_string = shift || '';
Condition100%2/2
shift() || ''
l!l
73
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
123 10 my $user = $self->get_user($user_string);
124 10 ▶ return undef unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 4 6 10
125 6 my $role = $user->roles()->single({name => $role_string});
126 #say STDERR 'has_role: ', $role->name if $self->app->app_debug;
127 6 ▶ return ($role && $role->name eq $role_string);
Condition33%1/3
$role && $role->name eq $role_string
!ll&&!rl&&r
006
MC/DC0%0/2
$role && $role->name eq $role_string
$role $role->name eq $role_string
Truth table33%1/3
A && B
A= $roleB= $role->name eq $role_string
ABresult
0X0
100
111
128 }
129 1 );
130 $app->helper(
131 5 ▶ 'is_admin' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 5 my ($self,$user) = @_;
133 5 return $self->has_role($user,'admin');
134 }
135 1 );
136  
137 1 my $routes = $app->routes;
138  
139 1 $routes->get('/')->to('front#index');
140 1 $routes->get('/front/*name')->to('front#page');
141 1 $routes->post('/save')->to('front#save');
142 1 $routes->post('/login')->to('user#login');
143 1 $routes->any('/logout')->to('user#logout');
144  
145 my $if_admin = $routes->under(
146 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
147 5 my $self = shift;
148 5 ▶ return $self->auth_fail unless $self->is_admin;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_admin 0 5 5
149 5 return 1;
150 }
151 1 );
152  
153 1 $if_admin->post('/admin/save/:table')->to('admin#save');
154  
155 1 $if_admin->get('/admin/change/:table/:id')->to('admin#change');
156 1 $if_admin->get('/admin/show/:table')->to('admin#show');
157  
158 }
159  
160 2 1;
161  
162 __END__
« AproJo/Admin.pm ↑ Summary AproJo/Command/setup.pm »