Commit | Line | Data |
ddccc6a2 |
1 | package Reaction::UI::ViewPort::Collection::Role::Order; |
e22de101 |
2 | |
3 | use Reaction::Role; |
4 | |
81393881 |
5 | use namespace::clean -except => [ qw(meta) ]; |
e22de101 |
6 | |
e22de101 |
7 | |
cf6fb234 |
8 | has order_by => (isa => 'Str', is => 'rw', trigger_adopt('order_by'), clearer => 'clear_order_by'); |
81393881 |
9 | has order_by_desc => (isa => 'Int', is => 'rw', trigger_adopt('order_by'), lazy_build => 1); |
10 | sub _build_order_by_desc { 0 }; |
11 | sub adopt_order_by { |
12 | shift->clear_current_collection; |
13 | }; |
b8faba69 |
14 | |
cf6fb234 |
15 | after clear_order_by => sub { |
16 | my ($self) = @_; |
17 | $self->order_by_desc(0); |
18 | $self->clear_current_collection; |
19 | }; |
20 | |
81393881 |
21 | around _build_current_collection => sub { |
22 | my $orig = shift; |
23 | my ($self) = @_; |
24 | my $collection = $orig->(@_); |
25 | my %attrs; |
b8faba69 |
26 | |
81393881 |
27 | #XXX DBICism that needs to be fixed |
28 | if ($self->has_order_by) { |
29 | $attrs{order_by} = $self->order_by; |
30 | $attrs{order_by} .= ' DESC' if ($self->order_by_desc); |
31 | } |
e22de101 |
32 | |
81393881 |
33 | return $collection->where(undef, \%attrs); |
34 | }; |
e22de101 |
35 | |
81393881 |
36 | around accept_events => sub { ('order_by', 'order_by_desc', shift->(@_)); }; |
e22de101 |
37 | |
e22de101 |
38 | |
e22de101 |
39 | |
40 | 1; |