ObjectView converted
[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   implements _build_order_by_desc => as { 0 };
11
12   implements adopt_order_by => as {
13     shift->clear_current_collection;
14   };
15
16   around _build_current_collection => sub {
17     my $orig = shift;
18     my ($self) = @_;
19     my $collection = $orig->(@_);
20     my %attrs;
21
22     #XXX DBICism that needs to be fixed
23     if ($self->has_order_by) {
24       $attrs{order_by} = $self->order_by;
25       $attrs{order_by} .= ' DESC' if ($self->order_by_desc);
26     }
27
28     return $collection->where(undef, \%attrs);
29   };
30
31   around accept_events => sub { ('order_by', 'order_by_desc', shift->(@_)); };
32
33 };
34
35 1;