Skip to content

Commit

Permalink
Merge pull request #67 from jmacdotorg/issue-60
Browse files Browse the repository at this point in the history
Issue 60: Add sort_title attribute and use it to sort entries instead of raw title (Fixes #60)
  • Loading branch information
jmacdotorg committed Oct 7, 2016
2 parents 562cadc + 65cd8cb commit e66de85
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
21 changes: 11 additions & 10 deletions IFComp/lib/IFComp/Controller/Ballot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,34 @@ sub root :Chained('/') :PathPart('ballot') :CaptureArgs(0) {
return;
}

my $order_by;
my @entries;
if ( $c->req->params->{ shuffle } ) {
$c->stash->{ is_shuffled } = 1;
my $seed = '';
if ( $c->user && $c->req->params->{ personalize } ) {
$seed = $c->user->get_object->id;
$c->stash->{ is_personalized } = 1;
}
$order_by = "rand($seed)";
my $order_by = "rand($seed)";
@entries = $current_comp->entries(
{},
{
order_by => $order_by,
}
);
}
else {
$order_by = 'title asc';
@entries = sort { $a->sort_title cmp $b->sort_title }
$current_comp->entries();
}

$c->stash->{ entries_rs } = $current_comp->entries(
{},
{
order_by => $order_by,
}
);
$c->stash->{ entries } = \@entries;

my $user_is_author = 0;
if ( $c->user && $c->user->get_object->current_comp_entries ) {
$user_is_author = 1;
}
$c->stash->{ user_is_author } = $user_is_author;

}

sub index :Chained('root') :PathPart('') :Args(0) {
Expand Down
21 changes: 20 additions & 1 deletion IFComp/lib/IFComp/Schema/Result/Entry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ Readonly my @DEFAULT_INFORM_CONTENT => qw(
index.html
);

has 'sort_title' => (
is => 'ro',
isa => 'Maybe[Str]',
lazy_build => 1,
clearer => 'clear_sort_title',
predicate => 'has_sort_title',
);

has 'directory' => (
is => 'ro',
isa => 'Path::Class::Dir',
Expand Down Expand Up @@ -571,10 +579,21 @@ around update => sub {
}
}

return $self->$orig( @_ );
if ( $self->is_column_changed( 'title' ) && $self->has_sort_title ) {
$self->clear_sort_title;
}

return $self->$orig( @_ );
};

sub _build_sort_title {
my $self = shift;
my $title = $self->title;
# for right now, just remove initial articles
$title =~ s/^(?:the|a|an) //i;
return $title;
}

sub _build_directory_name {
my $self = shift;

Expand Down
2 changes: 1 addition & 1 deletion IFComp/root/src/ballot/index.tt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</p>
[% END %]

[% WHILE (entry = entries_rs.next) %]
[% FOR entry IN entries %]
[% IF entry.is_qualified %]
<div class="well" id="entry-[% entry.id %]">
[% INCLUDE _entry_title.tt include_permalink = 1 %]
Expand Down
2 changes: 1 addition & 1 deletion IFComp/root/src/ballot/vote.tt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ We'd love to have you join us as a judge! It's as easy as logging in, creating a
[% END %]

<table class="table">
[% WHILE (entry = entries_rs.next) %]
[% FOR entry IN entries %]
<tr>
[% IF entry.is_qualified %]
<td><strong><a href="/ballot#entry-[% entry.id %]">[% entry.title | html %]</a></strong> by [% INCLUDE author_name.tt %]</td>
Expand Down

0 comments on commit e66de85

Please sign in to comment.