File Coverage

blib/lib/Ado/Plugin/AdoHelpers.pm
Criterion Covered Total %
statement 56 60 93.3
branch 18 20 90.0
condition 2 3 66.6
subroutine 12 14 85.7
pod 2 2 100.0
total 90 99 90.9


line stmt bran cond sub pod time code
1             package Ado::Plugin::AdoHelpers;
2 23     23   14984 use Mojo::Base 'Ado::Plugin';
  23         47  
  23         120  
3 23     23   2714 use Mojo::Util qw(decode);
  23         47  
  23         937  
4 23     23   130 use List::Util qw(first);
  23         33  
  23         1140  
5 23     23   128 use Mojo::File 'path';
  23         39  
  23         19373  
6              
7             # allow plugins to process SQL scripts while loading
8             sub do_sql_file {
9 3     3 1 2114 my ($app, $sql_file) = @_;
10 3         35 my $dbh = $app->dbix->dbh;
11              
12             #$app->log->debug('do_sql_file:' . $sql_file) if $Ado::Control::DEV_MODE;
13              
14 3         10117 my $SQL = decode('UTF-8', path($sql_file)->slurp());
15              
16             #Remove multi-line comments
17 2         314 $SQL =~ s|/\*+.+?\*/\s+?||gsmx;
18              
19             #$app->log->debug('$SQL:' . $SQL) if $Ado::Control::DEV_MODE;
20 2         52 local $dbh->{RaiseError} = 1;
21 2         47 my $last_statement = '';
22             return eval {
23             $dbh->begin_work;
24             for my $st (split /;/smx, $SQL) {
25             $last_statement = $st;
26             $dbh->do($st) if ($st =~ /\S+/smx);
27             }
28             $dbh->commit;
29 2   66     8 } || do {
30             my $e = "\nError in statement:$last_statement\n$@";
31             my $e2;
32             eval { $dbh->rollback } || ($e2 = $/ . 'Additionally we have a rollback error:' . $@);
33             $app->log->error($e . ($e2 ? $e2 : ''));
34             Carp::croak($e . ($e2 ? $e2 : ''));
35             };
36             }
37              
38             sub register {
39 24     24 1 1626 my ($self, $app, $conf) = shift->initialise(@_);
40              
41             # Add helpers
42 24     58   207 $app->helper(user => sub { shift->user(@_) });
  58         13396  
43              
44             # http://irclog.perlgeek.de/mojo/2014-10-03#i_9453021
45 24     1   616 $app->helper(to_json => sub { Mojo::JSON::to_json($_[1]) });
  1         18  
46 24         380 Mojo::Util::monkey_patch(ref($app), do_sql_file => \&Ado::Plugin::AdoHelpers::do_sql_file);
47              
48 24         621 $app->helper('head_css' => \&_head_css);
49 24         528 $app->helper('head_javascript' => \&_head_javascript);
50              
51 24         409 return $self;
52             } #end of register
53              
54             my $file_re = qr/\w+\.\w+(\?.*)?$/;
55              
56             sub _head_css {
57 442     442   1251300 my ($c, $assets) = @_;
58 442         1395 my $assets_list = $c->stash('head_css');
59              
60             #append
61 442 100       4283 if ($assets) {
62 362 100       1023 $assets = [$assets] unless ref($assets) eq 'ARRAY';
63 362         724 foreach my $a (@$assets) {
64 1295 100   12782   4028 push @$assets_list, $a unless first { $_ eq $a } @$assets_list;
  12782         16556  
65             }
66 362         875 return;
67             }
68              
69             # render
70 80         184 my $css = '';
71              
72             #everything in separate stylesheet begin end block or
73 80         206 foreach my $a (@$assets_list) {
74 1312 100       23029 if ($a =~ $file_re) { # a file
    50          
75 1129         2521 $css .= qq|\n|;
76             }
77             elsif (ref $a eq 'CODE') { # a code
78 183         1167 $css .= $c->stylesheet($a) . $/;
79             }
80             else { # a string
81 0     0   0 $css .= $c->stylesheet(sub {$a}) . $/;
  0         0  
82             }
83             }
84 80         4178 return $css;
85             }
86              
87             sub _head_javascript {
88 391     391   3781 my ($c, $assets) = @_;
89 391         901 my $assets_list = $c->stash('head_javascript');
90              
91             #append
92 391 100       3526 if ($assets) {
93 311 100       912 $assets = [$assets] unless ref($assets) eq 'ARRAY';
94 311         608 foreach my $a (@$assets) {
95 445 100   2650   1381 push @$assets_list, $a unless first { $_ eq $a } @$assets_list;
  2650         3845  
96             }
97 311         727 return;
98             }
99              
100             # render
101 80         164 my $js = '';
102              
103             #everything in separate javascript begin/end block or \n|;
107             }
108             elsif (ref $a eq 'CODE') { # a code
109 183         995 $js .= $c->javascript($a) . $/;
110             }
111             else { # a string
112 0     0   0 $js .= $c->javascript(sub {$a}) . $/;
  0         0  
113             }
114             }
115 80         4040 return $js;
116              
117             }
118              
119             1;
120              
121             =encoding utf8
122              
123             =head1 NAME
124              
125             Ado::Plugin::AdoHelpers - Default Ado helpers plugin
126              
127             =head1 SYNOPSIS
128              
129             # Ado
130             $self->plugin('AdoHelpers');
131              
132             # Mojolicious::Lite
133             plugin 'AdoHelpers';
134              
135             =head1 DESCRIPTION
136              
137             L is a collection of renderer helpers for
138             L.
139              
140             This is a core plugin, that means it is always enabled and its code a good
141             example for learning to build new plugins, you're welcome to fork it.
142              
143             See L for a list of plugins that are available
144             by default.
145              
146             =head1 HELPERS
147              
148             L implements the following helpers.
149              
150             =head2 do_sql_file
151              
152             Your plugin may need to add some new tables, add columns to already
153             existing tables or insert some data. This method allows you to do that.
154             See the source code of L for example.
155             The SQL file will be slurped, multiline comments will be removed.
156             The content will be split into C<';'> and each statement will be executed
157             using L.
158              
159             # in a plugin somewhere in register
160             $app->do_sql_file(catfile($self->config_dir, $sql_file));
161             $app->do_sql_file($conf->{vest_data_sql_file});
162              
163             # on the command line
164             $ ado eval 'app->do_sql_file(shift)' some_file.sql
165              
166             # elsewhere in an application
167             $app->do_sql_file($sql_file)
168              
169             =head2 head_css, head_javascript
170              
171             Minimalist asset management for the CheadE> section. Appends and
172             later renders assets (links to files and code-snippets) to
173             C<$c-Estash('head_css')> and Cstash('head_javascript')>. The new
174             assets are only appended if they are not already present in the corresponding
175             list of assets. The defaults are populated in C. See also:
176             L; L.
177              
178             #in a template:
179             #append
180             <%
181             head_css([
182             'vendor/SemanticUI/components/popup.min.css'
183             '#myid { font-size:xx-small }'
184             ]);
185             head_javascript([
186             'vendor/SemanticUI/components/popup.min.js'
187             'jQuery( function($){ $('#ado-img').popup() })'
188             ]);
189             %>
190            
191             # or
192             % head_javascript begin
193             jQuery( function($){ $('#ado-img').popup() });
194             % end;
195              
196             # render in templates/partials/head.html.ep
197             %== head_css;
198            
199            
200             rel='stylesheet' type='text/css' />
201             %== head_javascript;
202              
203             =head2 to_json
204              
205             Suitable for preparing JavaScript
206             objects from Perl references that will be used from stash and in templates.
207              
208             my $chars = $c->to_json({name =>'Петър',id=>2});
209             $c->stash(user_as_js => $chars);
210             # in a javascript chunk of a template
211             var user = <%== $user_as_js %>;
212             var user_group_names = <%== to_json([user->ingroup]) %>;
213              
214             =head2 user
215              
216             Returns the current user. This is the user C for not authenticated users.
217             This helper is a wrapper for L.
218              
219             $c->user(Ado::Model::Users->query("SELECT * from users WHERE login_name='guest'"));
220             #in a controller action:
221             my $current_user = $c->user;
222             #in a template:
223            

Hello, <%=user->name%>!

224              
225             =head1 METHODS
226              
227             L inherits all methods from
228             L and implements the following new ones.
229              
230             =head2 register
231              
232             $plugin->register(Ado->new);
233              
234             Register helpers in L application.
235              
236             =head1 SEE ALSO
237              
238             L, L, L,
239              
240              
241             =head1 AUTHOR
242              
243             Красимир Беров (Krasimir Berov)
244              
245             =head1 COPYRIGHT AND LICENSE
246              
247             Copyright 2013-2015 Красимир Беров (Krasimir Berov).
248              
249             This program is free software, you can redistribute it and/or
250             modify it under the terms of the
251             GNU Lesser General Public License v3 (LGPL-3.0).
252             You may copy, distribute and modify the software provided that
253             modifications are open source. However, software that includes
254             the license may release under a different license.
255              
256             See http://opensource.org/licenses/lgpl-3.0.html for more information.
257              
258             =cut