using value/related_object for related objects
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Collection / Role / Order.pm
1 package Reaction::UI::ViewPort::Collection::Role::Order;
2
3 use Reaction::Role;
4
5 use namespace::clean -except => [ qw(meta) ];
6
7
8 has order_by      => (isa => 'Str', is => 'rw', trigger_adopt('order_by'));
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 };
14
15 around _build_current_collection => sub {
16   my $orig = shift;
17   my ($self) = @_;
18   my $collection = $orig->(@_);
19   my %attrs;
20
21   #XXX DBICism that needs to be fixed
22   if ($self->has_order_by) {
23     $attrs{order_by} = $self->order_by;
24     $attrs{order_by} .= ' DESC' if ($self->order_by_desc);
25   }
26
27   return $collection->where(undef, \%attrs);
28 };
29
30 around accept_events => sub { ('order_by', 'order_by_desc', shift->(@_)); };
31
32
33
34 1;