blib/lib/Bootylicious/Plugin/Search.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 60 CC 4 SCAR 23.0
CC
4
Cov
0%
_search
20.0
register
2.0
BEGIN
2.0
CRAP 10.0
1 package Bootylicious::Plugin::Search;
2  
3 use strict;
4 use warnings;
5  
6 use base 'Mojolicious::Plugin';
7  
8 use Mojo::ByteStream 'b';
9  
10 sub register {
11 my ($self, $app, $conf) = @_;
12  
13 $conf ||= {};
14  
15 $conf->{'before_context'} ||= 20;
16 $conf->{'after_context'} ||= 20;
17 $conf->{'min_length'} ||= 2;
18 $conf->{'max_length'} ||= 256;
19  
20 $app->routes->route('/search')->via('GET')
21 ->to(cb => sub { my $c = shift; _search($app, $c, $conf) })
22 ->name('search');
23 }
24  
25 sub _search {
26 my $app = shift;
27 my $c = shift;
28 my $conf = shift;
29  
30 $c->stash(template_class => __PACKAGE__);
31  
32 my $q = $c->req->param('q');
33 return unless defined $q;
34  
35 my $min_length = $conf->{min_length};
36 my $max_length = $conf->{max_length};
37  
38 if (length($q) < $min_length) {
39 $c->stash(error => qq/Query must be $min_length characters minimum/);
40 }
41 elsif (length($q) > $max_length) {
42 $c->stash(error => qq/Query must be $max_length characters maximum/);
43 }
44 else {
45 my $articles = $c->get_articles_by_query(
46 $q,
47 before_context => $conf->{before_context},
48 after_context => $conf->{after_context}
49 );
50  
51 my $pager = $c->get_pager($articles, timestamp => $c->param('timestamp'));
52  
53 $c->stash(
54 total => $articles->size,
55 articles => $pager->articles,
56 pager => $pager
57 );
58 }
59 }
60  
61 1;
62 __DATA__