Commit | Line | Data |
e22de101 |
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 | |
aa8c0c90 |
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 | |
89939ff9 |
21 | implements _build_order_by_desc => as { 0 }; |
b8faba69 |
22 | |
23 | implements adopt_order_by => as { |
24 | shift->clear_current_collection; |
25 | }; |
26 | |
89939ff9 |
27 | around _build_current_collection => sub { |
e22de101 |
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; |