Skip to content

Instantly share code, notes, and snippets.

@vti
Created October 12, 2010 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vti/ab696946e5372dce56a9 to your computer and use it in GitHub Desktop.
Save vti/ab696946e5372dce56a9 to your computer and use it in GitHub Desktop.
package Bootylicious::Plugin::Search;
use strict;
use warnings;
use base 'Mojolicious::Plugin';
use Mojo::ByteStream 'b';
sub register {
my ($self, $app, $conf) = @_;
$conf ||= {};
$conf->{'before_context'} ||= 20;
$conf->{'after_context'} ||= 20;
$conf->{'min_length'} ||= 2;
$conf->{'max_length'} ||= 256;
$app->routes->route('/search')->via('GET')
->to(cb => sub { my $c = shift; _search($app, $c, $conf) })
->name('search');
}
sub _search {
my $app = shift;
my $c = shift;
my $conf = shift;
my $q = $c->req->param('q');
my $results;
$c->stash(error => '');
if (defined $q && length($q) < $conf->{min_length}) {
$c->stash(error => 'Has to be '
. $conf->{min_length}
. ' characters minimal');
}
elsif (defined $q && length($q) > $conf->{max_length}) {
$c->stash(error => 'Has to be '
. $conf->{max_length}
. ' characters maximal');
}
elsif (defined $q) {
$results = _query_articles($app, $conf, $q);
}
$c->stash(articles => $results, template_class => __PACKAGE__);
}
sub _query_articles {
my ($app, $conf, $q) = @_;
#$q = b($q)->xml_escape;
my $before_context = $conf->{before_context};
my $after_context = $conf->{after_context};
return $app->get_articles_by_query($q);
}
1;
__DATA__
@@ search.html.ep
% stash template_class => 'main', title => 'Search';
<div style="text-align:center;padding:2em">
<form method="get">
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment