File Coverage

blib/lib/AproJo.pm
Criterion Covered Total %
statement 53 83 63.8
branch 2 18 11.1
condition 0 9 0.0
subroutine 9 15 60.0
pod 1 1 100.0
total 65 126 51.5


line stmt bran cond sub pod time code
1             package AproJo;
2 2     2   10754 use Mojo::Base 'Mojolicious';
  2         11739  
  2         8  
3              
4 2     2   477830 use Data::Dumper;
  2         5  
  2         104  
5              
6             our $VERSION = '0.010';
7              
8 2     2   8 use File::Basename 'dirname';
  2         12  
  2         79  
9 2     2   8 use File::Spec::Functions qw'rel2abs catdir';
  2         2  
  2         77  
10 2     2   748 use File::ShareDir 'dist_dir';
  2         7658  
  2         98  
11 2     2   9 use Cwd;
  2         2  
  2         1664  
12              
13             has db => sub {
14             my $self = shift;
15             my $schema_class = $self->config->{db_schema}
16             or die "Unknown DB Schema Class";
17             eval "require $schema_class"
18             or die "Could not load Schema Class ($schema_class), $@";
19              
20             my $db_connect = $self->config->{db_connect}
21             or die "No DBI connection string provided";
22             my @db_connect = ref $db_connect ? @$db_connect : ($db_connect);
23              
24             my $schema = $schema_class->connect(@db_connect)
25             or die "Could not connect to $schema_class using $db_connect[0]";
26              
27             return $schema;
28             };
29              
30             has app_debug => 0;
31              
32             has home_path => sub {
33             my $path = $ENV{MOJO_HOME} || getcwd;
34             return File::Spec->rel2abs($path);
35             };
36              
37             has config_file => sub {
38             my $self = shift;
39             return $ENV{APROJO_CONFIG} if $ENV{APROJO_CONFIG};
40              
41             return rel2abs('aprojo.conf', $self->home_path);
42             };
43              
44             sub startup {
45 1     1 1 35781 my $app = shift;
46              
47 1         3 $app->plugin(
48             Config => {
49             file => $app->config_file,
50             default => {
51             'db_connect' => [
52             'dbi:SQLite:dbname=' . $app->home->rel_file('aprojo.db'),
53             undef,
54             undef,
55             {'sqlite_unicode' => 1}
56             ],
57             'db_schema' => 'AproJo::DB::Schema',
58             'secret' => '47110815'
59             },
60             }
61             );
62              
63 1         1108 $app->plugin('I18N');
64 1         5622 $app->plugin('Mojolicious::Plugin::ServerInfo');
65 1         1212 $app->plugin('Mojolicious::Plugin::DBInfo');
66              
67 1         1031 $app->plugin('Mojolicious::Plugin::Form');
68            
69             {
70             # use content from directories under share/files or using File::ShareDir
71 1         1009 my $lib_base = catdir(dirname(rel2abs(__FILE__)), '..', 'share','files');
  1         4  
72              
73 1         51 my $public = catdir($lib_base, 'public');
74 1 50       17 $app->static->paths->[0] = -d $public ? $public : catdir(dist_dir('AproJo'), 'files','public');
75 1         72 my $static_path = $app->static->paths->[0];
76             #print STDERR '$static_path: ',$static_path,"\n";
77              
78 1         7 my $templates = catdir($lib_base, 'templates');
79 1 50       10 $app->renderer->paths->[0] = -d $templates ? $templates : catdir(dist_dir('AproJo'), 'files', 'templates');
80             }
81              
82 1         55 push @{$app->commands->namespaces}, 'AproJo::Command';
  1         5  
83              
84             #DEPRECATED: $app->secret( $app->config->{secret} );
85 1         34 $app->secrets([$app->config->{secret}]);
86              
87 1     3   17 $app->helper(schema => sub { shift->app->db });
  3         125  
88              
89 1     1   17 $app->helper('home_page' => sub {'/'});
  1         30  
90              
91             $app->helper(
92             'auth_fail' => sub {
93 0     0   0 my $self = shift;
94 0   0     0 my $message = shift || "Not Authorized";
95 0         0 $self->flash(onload_message => $message);
96 0         0 $self->redirect_to($self->home_page);
97 0         0 return 0;
98             }
99 1         16 );
100              
101             $app->helper(
102             'source_id' => sub {
103 0     0   0 my ($self, $source) = @_;
104 0 0       0 return undef unless $source;
105 0         0 my @columns = $self->schema->source($source)->columns;
106 0         0 my $table_name = $self->schema->class($source)->table;
107 0         0 my $source_id = $table_name . '_id';
108 0 0       0 return $source_id if (grep {/$source_id/} @columns);
  0         0  
109 0 0       0 return $columns[0] if (scalar @columns);
110             }
111 1         16 );
112              
113             $app->helper(
114             'get_user' => sub {
115 0     0   0 my ($self, $name) = @_;
116 0 0       0 unless ($name) {
117 0         0 $name = $self->session->{username};
118             }
119 0 0       0 return undef unless $name;
120             #say STDERR 'get_user: ', $name if $self->app->app_debug;
121 0         0 return $self->schema->resultset('User')->single({name => $name});
122             }
123 1         14 );
124            
125             $app->helper(
126             'has_role' => sub {
127 0     0   0 my $self = shift;
128 0   0     0 my $user_string = shift || '';
129 0   0     0 my $role_string = shift || '';
130 0         0 my $user = $self->get_user($user_string);
131 0 0       0 return undef unless $user;
132 0         0 my $role = $user->roles()->single({name => $role_string});
133             #say STDERR 'has_role: ', $role->name if $self->app->app_debug;
134 0   0     0 return ($role && $role->name eq $role_string);
135             }
136 1         16 );
137             $app->helper(
138             'is_admin' => sub {
139 0     0   0 my ($self,$user) = @_;
140 0         0 return $self->has_role($user,'admin');
141             }
142 1         14 );
143              
144 1         14 my $routes = $app->routes;
145              
146 1         8 $routes->get('/')->to('front#index');
147 1         112 $routes->get('/front/*name')->to('front#page');
148 1         175 $routes->post('/save')->to('front#save');
149 1         142 $routes->post('/login')->to('user#login');
150 1         146 $routes->any('/logout')->to('user#logout');
151              
152             my $if_admin = $routes->under(
153             sub {
154 0     0   0 my $self = shift;
155 0 0       0 return $self->auth_fail unless $self->is_admin;
156 0         0 return 1;
157             }
158 1         151 );
159              
160 1         79 $if_admin->post('/admin/save/:table')->to('admin#save');
161              
162 1         190 $if_admin->get('/admin/change/:table/:id')->to('admin#change');
163 1         205 $if_admin->get('/admin/show/:table')->to('admin#show');
164              
165             }
166              
167             1;
168              
169             __END__