diff --git a/lib/MetaCPAN/Web/Controller/Search.pm b/lib/MetaCPAN/Web/Controller/Search.pm index 4646d550d7..c04f326146 100644 --- a/lib/MetaCPAN/Web/Controller/Search.pm +++ b/lib/MetaCPAN/Web/Controller/Search.pm @@ -54,6 +54,27 @@ sub index : Path { my $authors = $c->model('API::Author')->search( $query, $from ); ( $results, $authors ) = ( $results->recv, $authors->recv ); + + if ( $results->{total} == 1 ) { + my $module_name + = $results->{results}->[0]->[0]->{module}->[0]->{name}; + $c->res->redirect("/pod/$module_name"); + $c->detach; + } + elsif ( !$results->{total} && !$authors->{total} ) { + my $suggest = $query; + $suggest =~ s/:+/::/g; + if ( $suggest ne $query ) { + $c->stash( + { + suggest => $suggest, + } + ); + } + $c->stash( template => 'no_result.html' ); + $c->detach; + } + $c->stash( { %$results, diff --git a/root/left_sidebar.html b/root/left_sidebar.html new file mode 100644 index 0000000000..e578ba53f5 --- /dev/null +++ b/root/left_sidebar.html @@ -0,0 +1,20 @@ + \ No newline at end of file diff --git a/root/no_result.html b/root/no_result.html new file mode 100644 index 0000000000..8e3f9b7d8b --- /dev/null +++ b/root/no_result.html @@ -0,0 +1,24 @@ +<%- query = req.param('q').join(' '); title = 'Search for "' _ query _ '"' %> +<% IF suggest %> + <% INCLUDE left_sidebar.html %> +
+
+ Did you mean : + <% suggest %> +
+
+<% ELSE %> +
+ +

No result.

+

Sorry, we didn't find a match, maybe look at Task::Kensho,
which is a list of recommended modules for Enlightened Perl development. +

+

+ + Learn more + +

+
+<% END %> diff --git a/root/search.html b/root/search.html index fa6129ca34..0204bd0486 100644 --- a/root/search.html +++ b/root/search.html @@ -1,24 +1,6 @@ <%- query = req.param('q').join(' '); title = 'Search for "' _ query _ '"' %> - + +<% INCLUDE left_sidebar.html %>
<% IF authors.total %> diff --git a/root/static/css/style.css b/root/static/css/style.css index cc87e2a61a..c96a2f8230 100644 --- a/root/static/css/style.css +++ b/root/static/css/style.css @@ -288,3 +288,7 @@ div.qtip-github table th { #contributors .contributor * { vertical-align: top; } +.smaller { + font-size: 1.0em; + line-height: 2.5em; +} diff --git a/root/static/less/responsive.less b/root/static/less/responsive.less index 5e5b49947c..d2ef0d8881 100644 --- a/root/static/less/responsive.less +++ b/root/static/less/responsive.less @@ -116,7 +116,7 @@ height: 15px; } - .search-results, .pod, .content { + .search-results, .pod, .content, .no-results { margin-left: 0px; border-left-width: 0px; padding: 0px; diff --git a/root/static/less/search.less b/root/static/less/search.less index 553284c756..ba7e553a69 100644 --- a/root/static/less/search.less +++ b/root/static/less/search.less @@ -11,6 +11,13 @@ list-style: none; } } +.no-results { + margin-left: 175px; + border-left: 1px solid #e9e9e9; + padding-left: 15px; + margin-top: 15px; +} + .search-bar .search-type { font-size: 1.1em @@ -41,3 +48,13 @@ text-shadow: 0px 1px 1px rgba(255,255,255,1); line-height: 1; } + +.hero-unit .nav-header { + padding-left: 0px; +} + +.hero-unit .nav-header a { + text-transform: none; + font-size: 1.2em; + font-weight: normal; +} \ No newline at end of file diff --git a/root/static/less/style.less b/root/static/less/style.less index d651227d49..1bbda48304 100644 --- a/root/static/less/style.less +++ b/root/static/less/style.less @@ -216,7 +216,8 @@ body { } .smaller { - font-size: 0.8em; + font-size: 1.0em; + line-height: 2.5em; } .btn.btn-slidepanel { diff --git a/t/controller/search.t b/t/controller/search.t index f1728a060e..61e6a3fc44 100644 --- a/t/controller/search.t +++ b/t/controller/search.t @@ -20,10 +20,15 @@ test_psgi app, sub { ok( $res = $cb->( GET "/search?q=moose\">" ), 'GET /search?q=moose">' ); is( $res->code, 200, 'code 200' ); - ok( - $res->content =~ /0\s+results/, - '0 results for an invalid search term' - ); + ok( $res->content =~ /Task::Kensho/, + 'get recommendation about Task::Kensho on No result page' ); + + ok( $res = $cb->( GET "/search?q=ctx_request" ), + 'GET /search?q=ctx_request' ); + is( $res->code, 302, + 'code 302 get redirected to the module if there is 1 result' ); + is( $res->headers->{location}, + '/pod/Catalyst::Test', 'get new location to module page' ); ok( $res = $cb->( GET "/search?q=moose" ), 'GET /search?q=moose' ); is( $res->code, 200, 'code 200' ); diff --git a/t/controller/search/suggestion.t b/t/controller/search/suggestion.t new file mode 100644 index 0000000000..da77d60cbc --- /dev/null +++ b/t/controller/search/suggestion.t @@ -0,0 +1,25 @@ +use strict; +use warnings; +use Test::More; +use MetaCPAN::Web::Test; + +my %tests = ( + 'DBIx:Class:::ResultSet' => 'DBIx::Class::ResultSet', + 'DBIx::Class:ResultSet' => 'DBIx::Class::ResultSet', + 'DBIx:Class' => 'DBIx::Class', +); + +test_psgi app, sub { + my $cb = shift; + while ( my ( $k, $v ) = each %tests ) { + ok( my $res = $cb->( GET "/search?q=$k" ), 'search for ' . $k ); + my $tx = tx($res); + my $module + = $tx->find_value( + '//div[@class="no-results"]//div[@class="alert alert-error"]//a[1]' + ); + is( $module, $v, "get no result page with suggestion" ); + } +}; + +done_testing;