Skip to content

Commit

Permalink
Merge pull request #718 from metacpan/mickey/controller_release_find_…
Browse files Browse the repository at this point in the history
…refactor

Controller::Release::find - move data handling to Document module
  • Loading branch information
oalders committed Jul 10, 2017
2 parents 2f74d54 + 9fde389 commit 64a64da
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
7 changes: 6 additions & 1 deletion lib/MetaCPAN/Document/Release.pm
Expand Up @@ -318,14 +318,19 @@ sub aggregate_status_by_author {

sub find {
my ( $self, $name ) = @_;
return $self->filter(
my $file = $self->filter(
{
and => [
{ term => { distribution => $name } },
{ term => { status => 'latest' } }
]
}
)->sort( [ { date => 'desc' } ] )->first;
return unless $file;

my $data = $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} );
return $data;
}

sub predecessor {
Expand Down
3 changes: 1 addition & 2 deletions lib/MetaCPAN/Server/Controller/Changes.pm
Expand Up @@ -107,8 +107,7 @@ sub get : Chained('index') : PathPart('') : Args(2) {

sub find : Chained('index') : PathPart('') : Args(1) {
my ( $self, $c, $name ) = @_;
my $release
= eval { $c->model('CPAN::Release')->raw->find($name)->{_source}; }
my $release = eval { $c->model('CPAN::Release')->raw->find($name); }
or $c->detach( '/not_found', [] );

$c->forward( 'get', [ @$release{qw( author name )} ] );
Expand Down
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Server/Controller/Diff.pm
Expand Up @@ -34,7 +34,7 @@ sub release : Chained('index') : PathPart('release') : Args(1) {
my ( $latest, $previous );
try {
$latest
= $c->model('CPAN::Release')->inflate(0)->find($name)->{_source};
= $c->model('CPAN::Release')->inflate(0)->find($name);
$previous
= $c->model('CPAN::Release')->inflate(0)->predecessor($name)
->{_source};
Expand Down
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Server/Controller/Package.pm
Expand Up @@ -13,7 +13,7 @@ sub modules : Path('modules') : Args(1) {
my $last = $c->model('CPAN::Release')->raw->find($dist);
return unless $last;
my $data
= $self->model($c)->get_modules( $dist, $last->{_source}{version} );
= $self->model($c)->get_modules( $dist, $last->{version} );
$c->stash($data);
}

Expand Down
9 changes: 2 additions & 7 deletions lib/MetaCPAN/Server/Controller/Release.pm
Expand Up @@ -22,13 +22,8 @@ __PACKAGE__->config(
sub find : Path('') : Args(1) {
my ( $self, $c, $name ) = @_;
my $file = $self->model($c)->raw->find($name);
if ( !defined $file ) {
$c->detach( '/not_found', [] );
}
$c->stash( $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} ) )
|| $c->detach( '/not_found',
['The requested field(s) could not be found'] );
$c->detach( '/not_found', [] ) unless $file;
$c->stash($file);
}

sub get : Path('') : Args(2) {
Expand Down

0 comments on commit 64a64da

Please sign in to comment.