Skip to content

Commit

Permalink
Check that there's only one file before redirecting
Browse files Browse the repository at this point in the history
it was catching when there was only one dist.

fixes #1226.
  • Loading branch information
rwstauner committed Jun 15, 2014
1 parent f58bfe7 commit 4a22c0a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/MetaCPAN/Web/Controller/Search.pm
Expand Up @@ -55,11 +55,25 @@ sub index : Path {
my $authors = $c->model('API::Author')->search( $query, $from );
( $results, $authors ) = ( $results->recv, $authors->recv );

# The "total" is actually "total distributions".
if ( $results->{total} == 1 ) {
my $module_name
= $results->{results}->[0]->[0]->{module}->[0]->{name};
$c->res->redirect("/pod/$module_name");
$c->detach;
my $dist_files = $results->{results}->[0];

# There may be more than one file per dist.
if ( @$dist_files == 1 ) {

# FIXME: What's the right incantation for this?
# Is module name better than documentation?
# We may need to check indexed (in which case we need to make
# sure it's actually a boolean).
my $module_name = $dist_files->[0]->{module}->[0]->{name}

This comment has been minimized.

Copy link
@oalders

oalders Jun 15, 2014

Member

Doesn't this make the assumption that the first package declaration in the file is the one we're looking for?

This comment has been minimized.

Copy link
@rwstauner

rwstauner Jun 15, 2014

Author Contributor

Yeah, I think so, since it doesn't check that there's only one package.
Should we just use documentation? Or is there a better check?

This comment has been minimized.

Copy link
@rwstauner

rwstauner Jun 15, 2014

Author Contributor

I just realized that the prior search is already filtering by indexed/authorized, etc, so I think that means everything should be usable with /pod/X, however that won't always be the case (when we add dev releases).

|| $dist_files->[0]->{documentation};

if ($module_name) {
$c->res->redirect("/pod/$module_name");
$c->detach;
}
}
}
elsif ( !$results->{total} && !$authors->{total} ) {
my $suggest = $query;
Expand Down
7 changes: 7 additions & 0 deletions t/controller/search.t
Expand Up @@ -30,6 +30,13 @@ test_psgi app, sub {
is( $res->headers->{location},
'/pod/Catalyst::Test', 'get new location to module page' );

ok( $res = $cb->( GET "/search?q=perlhacktips" ),
'GET /search?q=perlhacktips' );
is( $res->code, 200,
'perlhacktips should be 200 not 302 because other files match' );

# TODO: Test something that has only one result but isn't indexed (/pod/X won't work).

ok( $res = $cb->( GET "/search?q=moose" ), 'GET /search?q=moose' );
is( $res->code, 200, 'code 200' );

Expand Down

0 comments on commit 4a22c0a

Please sign in to comment.