Skip to content

Commit

Permalink
ordering added
Browse files Browse the repository at this point in the history
  • Loading branch information
Zbigniew Lukasiak committed Oct 6, 2009
1 parent 3daa931 commit a51ef5d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
41 changes: 37 additions & 4 deletions lib/CPANHQ/Controller/Package.pm
Expand Up @@ -66,17 +66,40 @@ sub index : Private {
for my $key ( keys %$s_params ){
delete $s_params->{$key} if ! defined $s_params->{$key};
}
$s_params->{order} //= 'me.release_date';
$c->stash->{ packages } = $c->model( 'DB::Package' )->xsearch(
$s_params,
{
page => $page,
rows => 50,
order_by => 'me.release_date',
}
);
}
$c->stash( form => $form );
$c->stash( template => 'package/list.tt' );
$c->stash(
form => $form,
template => 'package/list.tt',
order_by_link => sub {
my $col = shift;
my $current = $params->{order};
my @parts = split /\s*,\s*/, $current;
my $done;
for my $part (@parts){
if( $part eq $col ){
$part = "$col desc";
$done = 1;
}
elsif( $part eq "$col desc"){
$part = $col;
$done = 1;
}
}
if( !$done ){
push @parts, $col;
}
my $order = join ', ', @parts;
return $c->req->uri_with( { order => $order, page => 1 } );
}
);
}

{
Expand All @@ -89,8 +112,18 @@ sub index : Private {
has '+http_method' => ( default => 'GET' );
has_field 'query';
has_field 'author';
has_field 'younger_than' => ( type => 'Date', format => "yy-mm-dd" );
has_field 'younger_than' => ( label => 'Newer than', type => 'Date', format => "yy-mm-dd" );
# has_field 'include_dev' => ( type => 'Checkbox' );
has_field 'order' => (
type => 'Text',
apply => [
{
check => qr/^(|me\.(release_date|name)( desc)?(, me\.(release_date|name)( desc)?)?)\z/,
# check => qr/^aaa$/,
message => 'Allowed ordering only by columns: "me.release_date" and "me.name" (with optional "desc" modifier).',
}
]
);
has_field 'submit' => ( type => 'Submit', value => 'Search' );
}

Expand Down
6 changes: 6 additions & 0 deletions lib/CPANHQ/Storage/ResultSet/Package.pm
Expand Up @@ -20,6 +20,12 @@ sub search_for_author {
return $self;
}

sub search_for_order {
my ( $self, $params ) = @_;
$self = $self->search( { }, { order_by => $params->{order} } );
return $self;
}

sub search_for_younger_than {
my ( $self, $params ) = @_;
$self = $self->search( { 'me.release_date' => { '>' => $params->{younger_than} } } );
Expand Down
4 changes: 2 additions & 2 deletions root/package/list.tt
Expand Up @@ -2,10 +2,10 @@
[% form.render %]
<table>
<tr>
<th>Name</dh>
<th><a href="[% order_by_link('me.name') %]">Name</a></dh>
<th>Author</dh>
<th>Abstract</dh>
<th>Release Date</dh>
<th><a href="[% order_by_link( 'me.release_date' ) %]">Release Date</a></dh>
</tr>
[%- WHILE ( package = packages.next ) -%]
<tr>
Expand Down
2 changes: 1 addition & 1 deletion root/wrapper.tt
Expand Up @@ -38,7 +38,7 @@ Not logged in (<a href="[% c.uri_for('/login') %]">Login</a>)
<div id=content>
[% content %]
</div>
[% INSERT "static/tweets.html-portion" %]
[%# INSERT "static/tweets.html-portion" %]
<div id=footer>
<ul id=botmenu>
<li class=first><a href="[% c.uri_for('/about') %]">About CPANHQ</a></li>
Expand Down

0 comments on commit a51ef5d

Please sign in to comment.