Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1776 from metacpan/mickey/move_code_from_templates
author page: moved logic from template to module
  • Loading branch information
oalders committed Sep 27, 2016
2 parents ddfa006 + 965590c commit 01cab45
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
40 changes: 33 additions & 7 deletions lib/MetaCPAN/Web/Controller/Author.pm
Expand Up @@ -73,15 +73,18 @@ sub index : Chained('root') PathPart('') Args(0) {
@$releases;
$c->res->last_modified($date) if $date;

my ( $aggregated, $latest ) = @{ $self->_calc_aggregated($releases) };

$c->stash(
{
author => $author,
faves => $faves,
releases => $releases,
show_author => 1,
template => 'author.html',
took => $took,
total => $data->{hits}->{total},
aggregated => $aggregated,
author => $author,
faves => $faves,
latest => $latest,
releases => $releases,
template => 'author.html',
took => $took,
total => $data->{hits}->{total},
}
);

Expand Down Expand Up @@ -129,6 +132,29 @@ sub releases : Chained('root') PathPart Args(0) {
);
}

sub _calc_aggregated {
my ( $self, $releases ) = @_;

my @aggregated;
my $latest = $releases->[0];
my $last;

for my $rel ( @{$releases} ) {
my ( $canon_rel, $canon_lat ) = map {
DateTime::Format::ISO8601->parse_datetime($_)
->strftime("%Y%m%d%H%M%S")
} ( $rel->{date}, $latest->{date} );
$latest = $rel if $canon_rel > $canon_lat;

next if $last and $last eq $rel->{distribution};
$last = $rel->{distribution};
next unless $rel->{name};
push @aggregated, $rel;
}

return [ \@aggregated, $latest ];
}

__PACKAGE__->meta->make_immutable;

1;
12 changes: 0 additions & 12 deletions lib/MetaCPAN/Web/View/HTML.pm
Expand Up @@ -60,15 +60,6 @@ sub format_datetime {
);
}

sub canonical_datetime {
my $date = shift;
my $dt = parse_datetime($date);
return int(
sprintf( '%04d%02d%02d%02d%02d%02d',
@$dt{qw(year month day hour minute second)} )
);
}

# format just the date consistent with W3CDTF / ISO 8601 / RFC 3339
sub common_date_format {
my $date = shift;
Expand All @@ -80,9 +71,6 @@ Template::Alloy->define_vmethod( 'text', dt => \&parse_datetime );

Template::Alloy->define_vmethod( 'text', dt_http => \&format_datetime );

Template::Alloy->define_vmethod( 'text',
dt_canonical => \&canonical_datetime );

Template::Alloy->define_vmethod( 'text',
dt_date_common => \&common_date_format );

Expand Down
18 changes: 0 additions & 18 deletions root/author.html
Expand Up @@ -93,24 +93,6 @@
</a>
</li>
<% END; END; END %>
<%
i = 0;
aggregated = [];
latest = releases[0];
FOREACH release IN releases;
IF release.date.dt_canonical > latest.date.dt_canonical;
latest = release;
END;
IF last == release.distribution;
NEXT;
ELSE;
last = release.distribution;
END;
UNLESS release.name;
NEXT;
END;
aggregated.push(release);
END %>
<li class="nav-header">Activity</li>
<li><% INCLUDE inc/activity.html query = 'author=' _ author.pauseid %></li>
<% INCLUDE inc/release-latest.html %>
Expand Down

0 comments on commit 01cab45

Please sign in to comment.