added action_filter
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Collection / Role / Order.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Collection::Role::Order;
e22de101 2
3use Reaction::Role;
4
81393881 5use namespace::clean -except => [ qw(meta) ];
7b5e71ad 6use MooseX::Types::Moose qw/Int HashRef Str ArrayRef/;
7has enable_order_by => (is => 'rw', isa => ArrayRef);
8has coerce_order_by => (is => 'rw', isa => HashRef);
c1b16a7d 9
10has order_by => (
7b5e71ad 11 isa => Str,
c1b16a7d 12 is => 'rw',
13 trigger_adopt('order_by'),
14 clearer => 'clear_order_by'
15);
16
17has order_by_desc => (
7b5e71ad 18 isa => Int,
c1b16a7d 19 is => 'rw',
20 trigger_adopt('order_by'),
21 lazy_build => 1
22);
23
24sub _build_order_by_desc { 0 }
e22de101 25
81393881 26sub adopt_order_by {
27 shift->clear_current_collection;
c1b16a7d 28}
29
30sub can_order_by {
31 my ($self,$order_by) = @_;
32 return 1 unless $self->has_enable_order_by;
33 return scalar grep { $order_by eq $_ } @{ $self->enable_order_by };
34}
35
36sub _order_search_attrs {
37 my $self = shift;
38 my %attrs;
39 if ($self->has_order_by) {
40 my $order_by = $self->order_by;
41 if( $self->has_coerce_order_by ){
42 $order_by = $self->coerce_order_by->{$order_by}
43 if exists $self->coerce_order_by->{$order_by};
44 }
45 my $key = $self->order_by_desc ? '-desc' : '-asc';
46 $attrs{order_by} = { $key => $order_by };
47 }
48 return \%attrs;
49}
b8faba69 50
cf6fb234 51after clear_order_by => sub {
52 my ($self) = @_;
53 $self->order_by_desc(0);
54 $self->clear_current_collection;
55};
56
81393881 57around _build_current_collection => sub {
58 my $orig = shift;
59 my ($self) = @_;
60 my $collection = $orig->(@_);
c1b16a7d 61 return $collection->where(undef, $self->_order_search_attrs);
81393881 62};
e22de101 63
81393881 64around accept_events => sub { ('order_by', 'order_by_desc', shift->(@_)); };
e22de101 65
1d97ac81 661;
e22de101 67
1d97ac81 68__END__;
e22de101 69
1d97ac81 70=head1 NAME
71
72Reaction::UI::ViewPort::Collection::Role::Order - Order support for collections
73
74=head1 DESCRIPTION
75
76Role to add order support to collection viewports.
77
78=head1 ATTRIBUTES
79
80=head2 enable_order_by
81
82Re-writable array reference. Optionally use this to manually specify a list of
83fields that support ordering, instead of the default of all fields. This is
84useful to exclude computed values or non-indexed columns from being sortable.
85
86=head2 coerce_order_by
87
88Re-writeable hash reference. Optionally use this to manually specify the way in
89which a field should be ordered. This is useful when the field name and the
90query to sort it differ. E.g. for a belongs_to item:
91
92 coerce_order_by => { foo => ['foo.last_name', 'foo.first_name'] },
93
94=head2 order_by
95
96Re-writeable string. Optionally set it to dictate which field to use when
97sorting.
98
99=head2 order_by_desc
100
101Re-writeable boolean. Optionally use descending order when sorting. Defaults to false.
102
103=head1 METHODS
104
105=head2 can_order_by $field_name
106
107Returns true if sorting by that field is supported, false otherwise.
108
109=head1 AUTHORS
110
111See L<Reaction::Class> for authors.
112
113=head1 LICENSE
114
115See L<Reaction::Class> for the license.
116
117=cut