listview ported bar pager
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / GridView / Role / Order.pm
1 package Reaction::UI::ViewPort::GridView::Role::Order;
2
3 use Reaction::Role;
4
5 role Order, which {
6
7   has order_by      => (isa => 'Str', is => 'rw', trigger_adopt('order_by'));
8   has order_by_desc => (isa => 'Int', is => 'rw', trigger_adopt('order_by'), lazy_build => 1);
9
10   before order_by => sub {
11     if (@_ > 1) {
12       my ($self, $val) = @_;
13       confess "invalid column name for order_by"
14         unless grep { $val eq $_ } $self->collection
15                                         ->_source_resultset
16                                         ->result_source
17                                         ->columns;
18     }
19   };
20
21   implements _build_order_by_desc => as { 0 };
22
23   implements adopt_order_by => as {
24     shift->clear_current_collection;
25   };
26
27   around _build_current_collection => sub {
28     my $orig = shift;
29     my ($self) = @_;
30     my $collection = $orig->(@_);
31     my %attrs;
32
33     #XXX DBICism that needs to be fixed
34     if ($self->has_order_by) {
35       $attrs{order_by} = $self->order_by;
36       $attrs{order_by} .= ' DESC' if ($self->order_by_desc);
37     }
38
39     return $collection->where(undef, \%attrs);
40   };
41
42   around accept_events => sub { ('order_by', 'order_by_desc', shift->(@_)); };
43
44 };
45
46 1;